ROLLBACK
In one line
ROLLBACK is the SQL command that cancels the current transaction and undoes all uncommitted changes.
In simple words
ROLLBACK ends a transaction by undoing all its changes. The database returns to the state it was in before the transaction began. This is how you recover from errors — if something goes wrong mid-transaction, you roll back and no partial changes are saved.
A common pattern is to try a series of operations and roll back if any one fails:
BEGIN;
-- operation 1
-- operation 2 (fails!)
ROLLBACK; -- operation 1 is also undone
ROLLBACK satisfies the Atomicity property of ACID — the transaction is all-or-nothing, and rollback ensures "nothing" when it cannot be "all."
Example
BEGIN;
UPDATE accounts SET balance = balance - 500 WHERE id = 1;
-- Oops, account 2 does not exist!
UPDATE accounts SET balance = balance + 500 WHERE id = 999; -- 0 rows affected
ROLLBACK; -- The first UPDATE is undoneROLLBACK undoes the first UPDATE, so account 1's balance is restored.
Related terms
Related Resources
Related Lessons
- Transactions
Learn COMMIT and ROLLBACK.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.