“Database Management Systems”
CS-222
“Transaction Control Language (TCL)”
By: Dr. Muhammad Muneer Umar
Lecturer in Computer Science
Institute of Computing
KUST, Pakistan
Topic: TCL
SQL
DQL DML DCL DDL
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
SQL
DQL DML DCL DDL TCL
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
Contents
• Transaction Control Language
• COMMIT
• ROLLBACK
• SAVEPOINT
• EXAMPLE CODE
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
Transaction Control Language
• Transaction Control Language(TCL) commands are used to manage
transactions in the database.
• These are used to manage the changes made to the data in a table by
DML statements.
• It also allows statements to be grouped together into logical
transactions
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
Transaction Control Language
• Transaction for a purchase operation on some sample tables:
customer table products table purchases
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
COMMIT
• COMMIT command is used to permanently save any transaction into
the database.
• The changes made by DML commands are not permanent, until the
current session is closed, the changes made by these commands can be
rolled back.
• To avoid that, we use the COMMIT command to mark the changes as
permanent.
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
COMMIT
• Following is commit command's syntax,
COMMIT;
[STATEMENT 1]
[STATEMENT 2]
[STATEMENT 3]
COMMIT;
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
ROLLBACK
• This command restores the database to last committed state.
• It is also used with SAVEPOINT command to jump to a savepoint in an
ongoing transaction.
• If we have used the UPDATE command to make some changes into the
database, and realise that those changes were not required, then we
can use the ROLLBACK command to rollback those changes, if they
were not committed using the COMMIT command.
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
ROLLBACK
Following is rollback command's syntax,
• ROLLBACK TO savepoint_name;
[STATEMENT 1]
[STATEMENT 2]
SAVEPOINT A;
[STATEMENT 3]
…
ROLLBACK TO A;
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
SAVEPOINT
• SAVEPOINT command is used to temporarily save a transaction so that
we can rollback to that point whenever required.
• In short, using this command we can name the different states of our
data in any table and then rollback to that state using
the ROLLBACK command whenever required.
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
SAVEPINT
Following is savepoint command's syntax,
• SAVEPOINT savepoint_name;
[STATEMENT 1]
[STATEMENT 2]
SAVEPOINT A;
[STATEMENT 3]
…
ROLLBACK TO A;
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
EXAMPLE 1
class
id name
1 Laleen Umar
2 Nazeer Umar
4 Arshan Umar
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
EXAMPLE
• INSERT INTO class VALUES(5, ‘Shaheer Khan’);
• COMMIT;
• UPDATE class SET name = ‘Ali Khan' WHERE id = '5’;
• SAVEPOINT A;
• INSERT INTO class VALUES(6, ‘Muhammad’); id name
• SAVEPOINT B; 1 Laleen Umar
• INSERT INTO class VALUES(7, 'Bravo’); 2 Nazeer Umar
• SAVEPOINT C; 4 Arshan Umar
• SELECT * FROM class;
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
EXAMPLE
• INSERT INTO class VALUES(5, ‘Shaheer Khan’);
• COMMIT;
• UPDATE class SET name = ‘Ali Khan' WHERE id = '5’;
• SAVEPOINT A; id name
• INSERT INTO class VALUES(6, ‘Muhammad’); 1 Laleen Umar
• SAVEPOINT B; 2 Nazeer Umar
• INSERT INTO class VALUES(7, 'Bravo’); 4 Arshan Umar
5 Ali Khan
• SAVEPOINT C;
6 Muhammad
• SELECT * FROM class;
7 Bravo
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
Example
• Now let's use the ROLLBACK command to roll
back the state of data to the savepoint B.
• ROLLBACK TO B;
• SELECT * FROM class;
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
Example
• Now let's use the ROLLBACK command to roll
back the state of data to the savepoint B.
id name
1 Laleen Umar
2 Nazeer Umar
• ROLLBACK TO B; SELECT * FROM class; 4 Arshan Umar
5 Ali Khan
6 Muhammad
7 Bravo
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
Example
• Now let's use the ROLLBACK command to roll
back the state of data to the savepoint B.
id name
1 Laleen Umar
2 Nazeer Umar
• ROLLBACK TO B; SELECT * FROM class; 4 Arshan Umar
5 Ali Khan
6 Muhammad
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
Topic: TCL
EXAMPLE 2
• We will insert some values into a table.
• A stored procedure is used for the transaction.
• Upon successful execution of the insert queries, the transaction will
COMMIT all the changes.
• Incase of any failure/error, the ROLLBACK will be executed
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: [email protected]
EXAMPLE 2 Topic: TCL
• CREATE DEFINER=`root`@`localhost` PROCEDURE `test`()
• BEGIN
• DECLARE `_rollback` BOOL DEFAULT 0;
• DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET `_rollback` = 1;
• START TRANSACTION;
• INSERT INTO customer VALUES(10,'XYZ',1000);
• INSERT INTO customer VALUES(1,'AAA',200); -- ERROR HERE
• SELECT * FROM customer; -- THIS WILL SHOW THE CHANGE BEFORE ROLLBACK
• IF `_rollback` THEN
• ROLLBACK;
• ELSE
• COMMIT;
• END IF;
• END
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email:
[email protected]ACKNOWLEDGEMENT
THESE SLIDES ARE PREPARED FROM
• https://www.studytonight.com/dbms/tcl-command.php
THANKS
Course: Database Management Systems (CS222)
Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST
Email: [email protected]