✏️ Explanatory Question
Level: Advanced — These are the core Transaction Control Language (TCL) commands.
These three commands control the outcome of a transaction — whether its changes are saved, undone, or partially rolled back.
ROLLBACK TO savepoint_name, while keeping the earlier work intact.
| Command | Action | Reversible? |
|---|---|---|
| COMMIT | Save all changes permanently | No |
| ROLLBACK | Undo all changes | N/A |
| SAVEPOINT | Set a checkpoint | N/A |
| ROLLBACK TO sp | Undo to a savepoint | Partial undo |
| RELEASE SAVEPOINT | Delete a savepoint | N/A |
START TRANSACTION;
INSERT INTO accounts (acc_id, balance) VALUES (3, 5000);
SAVEPOINT after_insert; -- checkpoint 1
UPDATE accounts SET balance = balance - 2000 WHERE acc_id = 3;
SAVEPOINT after_update; -- checkpoint 2
DELETE FROM accounts WHERE acc_id = 3; -- oops, mistake!
-- Undo ONLY the delete, keep the insert & update
ROLLBACK TO after_update;
-- Save the remaining valid changes permanently
COMMIT;
START TRANSACTION;
UPDATE accounts SET balance = balance - 1000 WHERE acc_id = 1;
UPDATE accounts SET balance = balance + 1000 WHERE acc_id = 999; -- invalid acc
-- Since the second update affected nothing / failed logic,
-- undo the entire transaction
ROLLBACK; -- account 1 is restored, no money lost
CREATE, ALTER, DROP, TRUNCATE) cause an implicit commit — they cannot be rolled back. Only DML operations within a transaction are reversible.