Lecture 26 (Week 15) -SQL TCL
Lecture 26 (Week 15) -SQL TCL
CS-222
SQL
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: muneer.umar@kust.edu.pk
Topic: TCL
SQL
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: muneer.umar@kust.edu.pk
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: muneer.umar@kust.edu.pk
Topic: TCL
• These are used to manage the changes made to the data in a table by
DML statements.
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: muneer.umar@kust.edu.pk
Topic: TCL
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: muneer.umar@kust.edu.pk
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.
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: muneer.umar@kust.edu.pk
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: muneer.umar@kust.edu.pk
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: muneer.umar@kust.edu.pk
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: muneer.umar@kust.edu.pk
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: muneer.umar@kust.edu.pk
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: muneer.umar@kust.edu.pk
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: muneer.umar@kust.edu.pk
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
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: muneer.umar@kust.edu.pk
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
Course: Database Management Systems (CS222) || Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST || Email: muneer.umar@kust.edu.pk
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: muneer.umar@kust.edu.pk
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: muneer.umar@kust.edu.pk
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: muneer.umar@kust.edu.pk
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: muneer.umar@kust.edu.pk
EXAMPLE 2 Topic: TCL
• https://www.studytonight.com/dbms/tcl-command.php
THANKS
Course: Database Management Systems (CS222)
Instructor: Dr. Muneer Umar, Lecturer. Institute of Computing, KUST
Email: muneer.umar@kust.edu.pk