SQL GRANT and REVOKE Command

The Data Control Language (or DCL) is typically used to control privileges in a Database. In other to perform a certain operation in the database, like creating tables, sequences or views; a user needs privileges. 

There are two types of privileges, they include;

  • System: This type of privilege consists of permissions for creating a session, table, etc, and all other system privileges.
  • Object: The object privilege consists of permissions for any command or query to perform any operation on the database tables.

However, in Data Control Language DCL, we have two types of commands,

  • GRANT: This type of command is used in providing any user access privileges or other privileges for the database.
  • REVOKE: The revoke command is used to take back permissions from any user.

 

Allow a User to create session

When a user is created in SQL, it is not permitted to log in and creates a session except when proper permissions/privileges are granted to the user.

The command described below can be used to grant the session creating privileges.

GRANT CREATE SESSION TO username;

 

Allow a User to create table

In other to allow a user to create tables in the database, the command below can be used.

GRANT CREATE TABLE TO username;

 

Provide user with space on tablespace to store table

Giving a user permission to create a table is not enough to store data in that table. The user must be provided with privileges to use the available tablespace for their table and data.

ALTER USER username QUOTA UNLIMITED ON SYSTEM;

The command above can alter the user details, as well as provide it access to unlimited tablespace on the system.

Note:  An unlimited quota is provided to Admin users generally.

 

Grant all privilege to a User

The ‘sysdba’ is a set of privileges that has all the permissions embedded in it. Therefore, if you want to provide all the privileges to any user, you can simply grant them the ‘sysdba’ permission.

GRANT sysdba TO username

 

Grant permission to create any table

In some instances, a user may be restricted from creating some tables with names that are reserved for system tables. However, privileges can be granted to a user to create any table using the below command.

GRANT CREATE ANY TABLE TO username

 

Grant permission to drop any table

In this case, when you want to allow a user to drop any table from the database, you can grant this privilege to the user.

GRANT DROP ANY TABLE TO username

 

To take back Permissions

More so, when you want to take back the privileges from any user, you can make use of the ‘revoke’ command.

REVOKE CREATE TABLE FROM username