Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need a Rollback command while making a transaction or tell me a when is it appropriate to use "ROLLBACK"

Tags:

sql

sql-server

In the code below when I am inserting data in a table then what is the purpose of rollback I should not insert it if I want to rollback so what is the appropriate way to use rollback?

BEGIN TRANSACTION Insert into dimCustomr Values('xyz',345435353) ROLLBACK;
like image 747
Warda Avatar asked Oct 14 '25 15:10

Warda


2 Answers

Using the rollback method, you can close the transaction, reverting all associated changes made to the database. Eg:

enter image description here

Above diagram illustrate the three query at a time.Tansaction of query1 & query2 were successful but query3 got error. And when query3 got error all transaction will revert.

like image 103
Biju Maharjan Avatar answered Oct 17 '25 04:10

Biju Maharjan


A Rollback is executed if a transaction aborts. It makes the whole Transaction undone. A transaction could be aborted through several errors that might occour when running the transaction or if you does an unplaned power off of your system.

like image 36
Matthias Neubert Avatar answered Oct 17 '25 06:10

Matthias Neubert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!