Lesson 7 of 8 · Structured Query Language (SQL)

Controlling Access with DCL

How to control who can do what with GRANT and REVOKE: users, system and object privileges, roles, and checking who has access.

Beginner 9 sections 5 min read

What DCL does

DCL (Data Control Language) decides who can do what in the database. It has just two commands: GRANT gives a permission, and REVOKE takes it away.

Controlling access is one of a DBA's core jobs. Every user should have exactly the permissions they need, and nothing more. This is called the principle of least privilege, and it limits the damage a mistake, a bug, or a stolen password can do.

Users

Everyone who connects to the database does so as a user. A new user can't do anything at all, not even log in:

CREATE USER kofi IDENTIFIED BY "Str0ng_Passw0rd_26";

User created.

$ sqlplus kofi@//dbserver:1521/salespdb
ORA-01045: user KOFI lacks CREATE SESSION privilege; logon denied

Every permission has to be granted. That's on purpose: nothing is allowed unless someone decides it should be.

Two kinds of privileges

A privilege is the right to do one specific thing. Oracle has two kinds:

System privileges
The right to do something in the database as a whole, such as CREATE SESSION (log in), CREATE TABLE, or CREATE VIEW. Powerful ones include the word ANY, as in SELECT ANY TABLE, which reads every table in the database.
Object privileges
The right to use one specific object, such as SELECT, INSERT, UPDATE, or DELETE on one table, or EXECUTE on one procedure. The object's owner has all of them automatically.

GRANT: giving privileges

-- system privilege: let Kofi log in
GRANT CREATE SESSION TO kofi;

-- object privilege: let Kofi read the orders table
GRANT SELECT ON sales.orders TO kofi;

-- several privileges at once
GRANT SELECT, INSERT, UPDATE ON sales.orders TO ama;

-- only one column: shipping clerks can update the status and nothing else
GRANT UPDATE (status) ON sales.orders TO shipping_clerk;

Grant succeeded.

Privileges take effect right away. When a user tries something they're not allowed to do, they get one of two errors:

-- Kofi can read orders, but not customers
SELECT * FROM sales.customers;
ORA-00942: table or view does not exist

-- Kofi can read orders, but not change them
DELETE FROM sales.orders WHERE order_id = 101;
ORA-01031: insufficient privileges

Roles: privileges in bundles

Granting the same privileges to every user one by one is slow and easy to get wrong. A role is a named bundle of privileges: grant the privileges to the role once, then grant the role to each user who needs it.

USERS ROLE OBJECTS kofi ama yaw sales_reader a role SELECT SELECT sales.customers sales.orders Grant privileges to the role once, then give the role to each user who needs it.
CREATE ROLE sales_reader;
GRANT SELECT ON sales.customers TO sales_reader;
GRANT SELECT ON sales.orders    TO sales_reader;

GRANT sales_reader TO kofi, ama, yaw;

When a new analyst joins, one GRANT sales_reader TO … gives them everything they need. When the role changes, every user who has it changes too.

Oracle comes with some predefined roles. You'll see these often:

RoleGives
CONNECTJust CREATE SESSION: the right to log in
RESOURCEThe right to create your own tables, sequences, procedures, and triggers. Views need a separate CREATE VIEW grant.
DBANearly every system privilege. Only DBAs should have it, never applications.

REVOKE: taking privileges away

REVOKE mirrors GRANT, with FROM instead of TO:

REVOKE SELECT ON sales.orders FROM kofi;
REVOKE sales_reader FROM ama;

Revoke succeeded.

Like a grant, a revoke takes effect immediately. In Oracle, GRANT and REVOKE are DDL, so they also commit the current transaction.

Letting users pass privileges on

Normally, a user can't share the privileges they've been given. Two options change that:

  • WITH GRANT OPTION on an object privilege lets the user grant it to others. If you later revoke it from them, it's also revoked from everyone they gave it to.
  • WITH ADMIN OPTION on a system privilege or a role does the same, except that revoking it doesn't affect the people they passed it on to.
GRANT SELECT ON sales.orders TO ama WITH GRANT OPTION;

Checking who has what

Users can see their own privileges and roles:

SELECT * FROM session_privs;   -- system privileges I have right now
SELECT * FROM session_roles;   -- roles I have right now

DBAs can see everyone's, in the data dictionary:

ViewShows
DBA_SYS_PRIVSSystem privileges granted to each user and role
DBA_TAB_PRIVSObject privileges, such as SELECT on a table
DBA_ROLE_PRIVSWhich users and roles have which roles
-- who has been granted privileges on the orders table?
SELECT grantee, privilege
FROM   dba_tab_privs
WHERE  owner = 'SALES' AND table_name = 'ORDERS';

GRANTEE         PRIVILEGE
--------------- ----------
SALES_READER    SELECT
AMA             SELECT
AMA             INSERT
AMA             UPDATE

Roles appear by name, such as SALES_READER. To see which users have a role, check DBA_ROLE_PRIVS. Column-level grants, such as the shipping clerk's, are listed separately in DBA_COL_PRIVS.

Reviewing these lists regularly, and removing access people no longer need, is a routine part of keeping a database secure and passing audits.

Key takeaways

  • DCL has two commands: GRANT gives privileges, and REVOKE takes them away. Both take effect immediately.
  • A new user can do nothing until granted privileges, starting with CREATE SESSION to log in.
  • System privileges cover the whole database; object privileges cover one object, such as SELECT on one table.
  • Roles bundle privileges, so you grant them once and give the role to each user.
  • ORA-00942 can mean "no permission", not just "no table".
  • Follow least privilege: grant only what's needed, avoid PUBLIC and the DBA role, and review access regularly.