Data Control Language (DCL) in SQL is used to manage access permissions and control privileges in a database. DCL ensures that only authorized users can access specific resources or perform particular actions on the database. The two main DCL commands are GRANT and REVOKE.
The GRANT command is used to give permissions to users to perform specific tasks on the database.
Syntax:
GRANT privilege_name ON object_name TO user_name;
Example: Grant SELECT permission on the Employees table to a user John:
GRANT SELECT ON Employees TO John;
Employees Table:
| EmployeeID | Name | Position | Salary |
|---|---|---|---|
| 1 | Alice | Manager | 100000 |
| 2 | Bob | Developer | 80000 |
| 3 | Charlie | Tester | 60000 |
The REVOKE command is used to remove permissions that were previously granted to a user.
Syntax:
REVOKE privilege_name ON object_name FROM user_name;
Example: Revoke SELECT permission on the Employees table from John:
REVOKE SELECT ON Employees FROM John;
Suppose you are managing a database for a company. Different departments (e.g., HR, Finance) need access to specific data:
Grant Permissions to HR:
GRANT SELECT, UPDATE ON Employees TO HR;
Grant Permissions to Finance:
GRANT SELECT (Salary) ON Employees TO Finance;
Revoke Permissions if Needed:
REVOKE UPDATE ON Employees FROM HR;