-- whats the use of DCL and TCL commands in SQL? Provide examples for each.
DCL (Data Control Language) and TCL (Transaction Control Language) are two categories of SQL commands used to manage database access and transactions, respectively.
DCL (Data Control Language):
DCL commands are used to control access to data in the database. The two primary DCL commands are GRANT and REVOKE.
- GRANT: This command is used to give users access privileges to the database. Example:
GRANT SELECT, INSERT ON Employees TO user1;
This command grants user1 the privileges to SELECT and INSERT data into the Employees table.
-
REVOKE: This command is used to remove access privileges from users.
Example:
REVOKE INSERT ON Employees FROM user1;This command revokes the INSERT privilege from user1 on the Employees table.
TCL (Transaction Control Language):
TCL commands are used to manage transactions in the database. The primary TCL commands are COMMIT, ROLLBACK, and SAVEPOINT. COMMIT: This command is used to save all changes made during the current transaction.
Example:
BEGIN TRANSACTION;
UPDATE Employees SET Salary = Salary * 1.1 WHERE Department = 'Sales';
COMMIT;
This sequence of commands starts a transaction, updates the salaries of employees in the Sales department, and then commits the changes to the database.
- ROLLBACK: This command is used to undo changes made during the current transaction. Example:
BEGIN TRANSACTION;
UPDATE Employees SET Salary = Salary * 1.1 WHERE Department = 'Sales';
ROLLBACK;
In this case, the salary update is undone, and no changes are made to the database.
-
SAVEPOINT: This command is used to set a point within a transaction to which you can later roll back.
Example:
BEGIN TRANSACTION; UPDATE Employees SET Salary = Salary * 1.1 WHERE Department = 'Sales'; SAVEPOINT SalesUpdate; UPDATE Employees SET Salary = Salary * 1.2 WHERE Department = 'Marketing'; ROLLBACK TO SalesUpdate; COMMIT;Here, a savepoint named SalesUpdate is created after updating the Sales department. If needed, you can roll back to this savepoint,
undoing only the changes made after it (the Marketing update), and then commit the remaining changes.
Top comments (0)