Skip to main content
Albero Academy

Transactions

Group statements so they all succeed or all fail.

Data & Schema8 min

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

a safe transfer
BEGIN; -- or START TRANSACTION
UPDATE 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

PropertyMeans
AtomicityAll statements commit together, or none do
ConsistencyThe database moves between valid states
IsolationConcurrent transactions don't corrupt each other
DurabilityOnce committed, it survives a crash

Key takeaways

  1. 01A transaction groups statements so they all succeed (COMMIT) or all fail (ROLLBACK).
  2. 02BEGIN starts it; COMMIT makes changes permanent; ROLLBACK undoes them.
  3. 03Transactions provide ACID: Atomicity, Consistency, Isolation, Durability.
  4. 04Use one whenever multiple changes must be all-or-nothing.

Finished this chapter?

Mark it complete to track your progress through SQL.