Wednesday 15 February 2017

TCL Commands----- Transaction Control Language

TCL command is used to manage the transaction in the databases.They are used to manage the changes made by the DML commands.
  1. Commit Command
It is used to permanently save any transaction into the databases.
Following is Commit command's syntax,
commit;

2. Rollback Command

This command  restores the databases to the last committed state.
It is also use with savepoint command to jump to a savepoint in a transaction.
Following is Rollback command's syntax,
rollback to savepoint-name;

3. SavePoint Command
SavePoint command is used to temporarily save a transaction so that you can rollback 
to that point whenever necessary.
Following is savepoint command's syntax,
savepoint savepoint-name;

Example of Savepoint and Rollback

Following is the class table,
IDNAME
1abhi
2adam
4alex
Lets use some SQL queries on the above table and see the results.
INSERT into class values(5,'Rahul');
commit;
UPDATE class set name='abhijit' where id='5';
savepoint A;
INSERT into class values(6,'Chris');
savepoint B;
INSERT into class values(7,'Bravo');
savepoint C;
SELECT * from class;
The resultant table will look like,
IDNAME
1abhi
2adam
4alex
5abhijit
6chris
7bravo

Now rollback to savepoint B
rollback to B;
SELECT * from class;
The resultant table will look like
IDNAME
1abhi
2adam
4alex
5abhijit
6chris
Now rollback to savepoint A
rollback to A;
SELECT * from class;
The result table will look like
IDNAME
1abhi
2adam
4alex
5abhijit


No comments:

Post a Comment