Difference between COMMIT and ROLLBACK in SQL

1. COMMIT :
COMMIT in SQL is a transaction control language which is used to permanently save the changes done in the transaction in tables/databases. The database cannot regain its previous state after the execution of it.

Consider the following STAFF table with records:

STAFF



Example:

sql> 
SELECT *
FROM Staff
WHERE Allowance = 400;

sql> COMMIT; 

Output:

So, the SELECT statement produced the output consisting of three rows.

2. ROLLBACK :
ROLLBACK in SQL is a transactional control language which is used to undo the transactions that have not been saved in database. The command is only be used to undo changes since the last COMMIT.

Consider the following STAFF table with records:

STAFF

Example:



sql> 
SELECT *
FROM EMPLOYEES
WHERE ALLOWANCE = 400;

sql> ROLLBACK; 

Output:

So, the SELECT statement produced the same output with ROLLBACK command.

Difference between COMMIT and ROLLBACK :

COMMIT ROLLBACK
COMMIT permanently saves the changes made by current transaction. ROLLBACK undo the changes made by current transaction.
Transaction can not undo changes after COMMIT execution. Transaction reaches its previous state after ROLLBACK.
When transaction is successful, COMMIT is applied. When transaction is aborted, ROLLBACK occurs.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.