Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Sometimes errors happen in the middle of a transaction. How do you recover from that? This video shows you how!
SQL Used
To reset the state of the database to before the begining of the transaction:
ROLLBACK;
See all of the SQL used in Modifying Data With SQL in the Modifying Data With SQL Cheatsheet.
-
0:00
Now that you've seen the benefit of using transactions in database save files,
-
0:04
you may be tempted to use them on your own for a series of complex updates.
-
0:09
Now, what happens in the middle of a transaction when you make a typo or
-
0:13
you forget something, and
-
0:14
you just want to get rid of the changes that haven't been committed yet?
-
0:18
This is where the rollback statement comes in.
-
0:22
Let's say you begin your transaction and start inserting rows.
-
0:27
You can even select to see them added, but they're not really added.
-
0:34
No one else can see them if they run the same query.
-
0:38
It's just for you to review.
-
0:40
[SOUND] Then you realize you've done a typo in all of your statements and
-
0:46
you feel like starting over.
-
0:48
That's where rollback comes in.
-
0:52
To undo it, we issue the rollback statement.
-
0:55
When you select from the table now,
-
0:57
the entries you tried to insert aren't there anymore.
-
1:00
That's because they weren't there in the first place.
-
1:03
You didn't commit your changes.
-
1:06
You may have issued multiple code operations in one go.
-
1:09
And issuing them by hand can be problematic.
-
1:12
But rolling back is always an option.
You need to sign up for Treehouse in order to download course files.
Sign up