TCL command is used to manage the transaction in the databases.They are used to manage the changes made by the DML commands.
- 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,
ID | NAME |
---|---|
1 | abhi |
2 | adam |
4 | alex |
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,
ID | NAME |
---|---|
1 | abhi |
2 | adam |
4 | alex |
5 | abhijit |
6 | chris |
7 | bravo |
Now rollback to savepoint B
rollback to B; SELECT * from class;
The resultant table will look like
ID | NAME |
---|---|
1 | abhi |
2 | adam |
4 | alex |
5 | abhijit |
6 | chris |
Now rollback to savepoint A
rollback to A; SELECT * from class;
The result table will look like
ID | NAME |
---|---|
1 | abhi |
2 | adam |
4 | alex |
5 | abhijit |
No comments:
Post a Comment