- Home
- Course Detail
regularpython@gmail.com
You are now watching:
SQL Commands / of SQL Data Control Language (DCL)
Understanding Data Control Language (DCL) in SQL: GRANT and REVOKE Explained for Beginners
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.
1. GRANT Command
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 |
2. REVOKE Command
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;
Real-Life Scenario
Suppose you are managing a database for a company. Different departments (e.g., HR, Finance) need access to specific data:
- HR should be able to view and update employee details but not delete them.
- Finance should be able to view employee salaries but not update or delete them.
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;
Important Notes
- Permissions can be granted to individual users or roles (groups of users).
- The database administrator (DBA) usually manages permissions.
- Using GRANT and REVOKE correctly ensures data security by restricting unauthorized access.