SQL TRANSACTIONS
SQL Transaction Control Language (TCL) commands are used to manage database transaction. SQL transaction command use with DML statement for INSERT, UPDATE and DELETE.
DML statement are store into SQL buffer until you execute Transaction commands. Once you execute transaction commands its store permanent to a database.
A transaction is a unit of work that is performed against a database. Transactions are units or sequences of work accomplished in a logical order, whether in a manual fashion by a user or automatically by some sort of a database program.
Properties of Transactions
- Atomicity − ensures that all operations within the work unit are completed successfully. Otherwise, the transaction is aborted at the point of failure and all the previous operations are rolled back to their former state.
- Consistency − ensures that the database properly changes states upon a successfully committed transaction.
- Isolation − enables transactions to operate independently of and transparent to each other.
- Durability − ensures that the result or effect of a committed transaction persists in case of a system failure.
Transaction Control
- COMMIT − to save the changes.
- ROLLBACK − to roll back the changes.
- SAVEPOINT − creates points within the groups of transactions in which to ROLLBACK.
- SET TRANSACTION − Places a name on a transaction.
Difference between Commit and Rollback
BASIS FOR COMPARISON |
COMMIT |
ROLLBACK |
Basic |
COMMIT validates the modifications made by the current transaction. |
ROLLBACK erases the modifications made by the current transaction. |
Effect |
After execution of COMMIT statement, the transaction can not be ROLLBACK. |
Once ROLLBACK is executed database reaches its previous state, i.e. before the execution of the first statement of the transaction. |
Occurrence |
COMMIT occurs when the transaction gets executed successfully. |
ROLLBACK occurs when the transaction is aborted in middle of the execution. |
Syntax |
COMMIT; |
ROLLBACK; |
SQL Implementation of Transaction commands
Commit
The COMMIT command is the transactional command used to save changes invoked by a transaction to the database.
Example
Consider employee table for all queries
delete from employee where age=42;
Commit;
|
Output
Savepoint
A SAVEPOINT is a point in a transaction when you can roll the transaction back to a certain point without rolling back the entire transaction.
Syntax : Savepoint savepoint_name; |
Example :
Rollback
The ROLLBACK command is the transactional command used to undo transactions that have not already been saved to the database. This command can only be used to undo transactions since the last COMMIT or ROLLBACK command was issued.
Note : Rollback command is supported in all previous versions of mysql then 5.5....
For more Uttrakhand Technical University(UTU) CSE-IV Sem Database Management System Lab Experiments Click here