MySQL + SQL · Lesson 84
COMMIT, ROLLBACK and SAVEPOINT
Transaction Control Commands
| Command | Does |
|---|---|
| COMMIT | permanently save all changes |
| ROLLBACK | undo changes since the transaction began |
| SAVEPOINT | mark a point to roll back to partially |
Using SAVEPOINT
START TRANSACTION;
UPDATE students SET marks = 90 WHERE roll_no = 1;
SAVEPOINT sp1;
UPDATE students SET marks = 0 WHERE roll_no = 2; -- mistake!
ROLLBACK TO sp1; -- undo only the second update
COMMIT; -- keep the first updateSAVEPOINT lets you undo part of a transaction without losing everything.
Summary
- COMMIT saves; ROLLBACK undoes; SAVEPOINT marks a partial undo point.
- ROLLBACK TO savepoint undoes only changes after that point.