A transaction is a group of database operations treated as one logical unit.
Example
Money transfer:
- Debit sender.
- Credit receiver.
Both must succeed or both must fail.
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
If something fails:
ROLLBACK;
ACID
| Property | Meaning |
|---|---|
| Atomicity | All or nothing |
| Consistency | Rules remain valid |
| Isolation | Concurrent transactions do not corrupt each other |
| Durability | Committed data survives crashes |
Transaction Lifecycle
Begin
Execute statements
Commit or Rollback
Savepoint
SAVEPOINT before_discount;
ROLLBACK TO before_discount;
Allows partial rollback inside a transaction.
Interview Notes
- Transactions protect multi-step changes.
- Commit makes changes permanent.
- Rollback undoes uncommitted changes.
- ACID is central to relational database reliability.
- Isolation level controls how transactions see each other's work.