How do you use the REVOKE statement to revoke permissions from a user or role?
Posted by CarolTh
Last Updated: June 28, 2024
The REVOKE statement in SQL is used to remove previously granted permissions from a user or role. Depending on the database system being used, the syntax may vary slightly, but the general concept remains the same. Below are the steps and examples for using the REVOKE statement.
Basic Syntax
The basic syntax for the REVOKE statement generally follows this pattern:
REVOKE { permission | ALL [ PRIVILEGES ] }
ON { object_type | ALL TABLES IN SCHEMA schema_name }
FROM { user | role } [ CASCADE | RESTRICT ];
Components
- permission: The specific permission(s) you want to revoke (e.g., SELECT, INSERT, UPDATE, DELETE). - object_type: The type of object you are revoking permissions for (e.g., a table, view, or sequence). - schema_name: The schema in which the object resides. - user: The name of the user from whom you are revoking permissions. - role: The role from which you are revoking permissions. - CASCADE: If other permissions are dependent on the privileges you are revoking, they will be revoked as well. - RESTRICT: Prevents the revocation of permissions if they are dependent on other permissions.
Examples
1. Revoke a specific permission from a user:
REVOKE SELECT ON employees FROM john_doe;
In this example, the SELECT permission on the employees table is being revoked from the user john_doe. 2. Revoke all permissions from a user:
REVOKE ALL PRIVILEGES ON employees FROM john_doe;
This statement revokes all permissions on the employees table from the user john_doe. 3. Revoke a specific permission from a role:
REVOKE INSERT ON orders FROM sales_team;
In this case, the INSERT permission on the orders table is being revoked from the role sales_team. 4. Revoke all permissions from a role:
REVOKE ALL PRIVILEGES ON products FROM sales_team;
This statement revokes all permissions on the products table from the sales_team role. 5. Revoke permissions from all users in a schema:
REVOKE SELECT ON ALL TABLES IN SCHEMA public FROM public;
In this example, the SELECT permission is revoked from all users on all tables in the public schema.
Notes
- Be cautious when using the REVOKE statement; ensure that you are not inadvertently removing permissions that are required for users or roles to perform their tasks. - The specific capabilities and behavior of the REVOKE statement may vary by SQL database system (e.g., PostgreSQL, MySQL, Oracle, SQL Server), so refer to the documentation for your specific database system for more details. - Always make sure you have the necessary privileges to execute the REVOKE statement, as typically only database administrators (DBAs) or users with specific high-level privileges can revoke permissions from others.