A transaction is a group of operations that must succeed or fail together, e.g. a money transfer.
| Property | Meaning |
|---|---|
| Atomicity | All or nothing: if one step fails, everything is rolled back |
| Consistency | The database moves from one valid state to another (rules and constraints hold) |
| Isolation | Concurrent transactions do not see each other's half-done work |
| Durability | Once committed, data survives crashes (written to disk / log) |
START TRANSACTION;
UPDATE accounts SET balance = balance - 500 WHERE id = 1;
UPDATE accounts SET balance = balance + 500 WHERE id = 2;
COMMIT; -- or ROLLBACK; if anything went wrong
Isolation levels
| Isolation level | Dirty read | Non-repeatable read | Phantom read |
|---|---|---|---|
| Read Uncommitted | Possible | Possible | Possible |
| Read Committed | No | Possible | Possible |
| Repeatable Read (MySQL default) | No | No | Possible |
| Serializable | No | No | No |