ROLLBACK
- SQL course
- Intermediate
- Lesson 102/115
- 4 min
Undo on error with ROLLBACK.
ROLLBACK
ROLLBACK undoes a transaction. All changes before COMMIT are discarded.
BEGIN;
DELETE FROM orders WHERE id = 1;
ROLLBACK;The delete is undone.
ROLLBACK — details
ROLLBACK preserves data on error. It undoes mistaken deletes or updates.
For example, if you accidentally ran a DELETE without WHERE and the transaction is still open, ROLLBACK brings back every deleted row.
Practice ROLLBACK
Write a DELETE and undo it with ROLLBACK.
Starter code
-- undo
BEGIN;
DELETE FROM orders WHERE id = 1Sample solution
BEGIN;
DELETE FROM orders WHERE id = 1;
ROLLBACK;Expected output
Transaction geri alındı
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 ROLLBACK: match the terms
| Term | Meaning |
|---|---|
ROLLBACK |
undo |
COMMIT |
commit |
undo |
recovery |
error |
protection |
2 Add ROLLBACK
BEGIN;
DELETE FROM orders WHERE id = 1;
____;
BEGIN;
DELETE FROM orders WHERE id = 1;
ROLLBACK;
Explanation: ROLLBACK is the right answer.
3 What does ROLLBACK do?
- Undoes the changes Correct answer
- Makes them permanent
- Starts a transaction
- Deletes them
Explanation: ROLLBACK undoes changes.