CHAPTER 28
Data integrity — guaranteeing your data is always correct.
🌟 Think of it this way: A bank transfer: deduct ₹5,000 from Account A, add ₹5,000 to Account B. If the server crashes after the deduction but before the credit, the money disappears. A transaction guarantees either BOTH operations succeed or NEITHER does — the money cannot vanish.
Safe money transfer
START TRANSACTION; UPDATE accounts SET balance = balance - 5000 WHERE account_id = 101; UPDATE accounts SET balance = balance + 5000 WHERE account_id = 202; COMMIT; -- only if both succeed -- If anything fails: ROLLBACK; -- reverts both updates completely
| Property | Guarantee | Bank Transfer Example |
|---|---|---|
| Atomicity | All changes commit or none do | Debit + credit both happen or neither |
| Consistency | DB remains valid before and after | Total money in system stays the same |
| Isolation | Concurrent transactions do not interfere | Two simultaneous transfers do not corrupt each other |
| Durability | Committed data survives crashes | After COMMIT, data is on disk even if power cuts |