SQL Fundamentals

Basic Transactions: Real-World

A bank transfer moves $100 from Account A to Account B. This requires two operations: (1) Subtract $100 from A, (2) Add $100 to B. If operation 1 succeeds but 2

A bank transfer moves $100 from Account A to Account B. This requires two operations: (1) Subtract $100 from A, (2) Add $100 to B. If operation 1 succeeds but 2 fails, money disappears! Transactions ensure both operations succeed together or both fail - no partial updates.

Atomic account transfer

Moving funds requires the debit and credit to succeed together so a failure cannot remove money from only one account.

The ledger remains balanced even when an intermediate operation fails.

Transfer funds as one unit

BEGIN; UPDATE accounts SET balance = balance - 100 WHERE account_id = 1; UPDATE accounts SET balance = balance + 100 WHERE account_id = 2; COMMIT;

All