When several statements must all succeed together — a money transfer debits one account and credits another — you wrap them in a transaction. COMMIT makes the changes permanent; ROLLBACK undoes them all.
All-or-nothing
BEGIN; -- or START TRANSACTIONUPDATE accounts SET balance = balance - 5000 WHERE id = 1;UPDATE accounts SET balance = balance + 5000 WHERE id = 2;COMMIT; -- both applied together; ROLLBACK would undo both
ACID guarantees
| Property | Means |
|---|---|
| Atomicity | All statements commit together, or none do |
| Consistency | The database moves between valid states |
| Isolation | Concurrent transactions don't corrupt each other |
| Durability | Once committed, it survives a crash |
Key takeaways
- 01A transaction groups statements so they all succeed (COMMIT) or all fail (ROLLBACK).
- 02BEGIN starts it; COMMIT makes changes permanent; ROLLBACK undoes them.
- 03Transactions provide ACID: Atomicity, Consistency, Isolation, Durability.
- 04Use one whenever multiple changes must be all-or-nothing.
