Skip to content

Transaction

Medium — good to knowDatabase

ELI5 — The Vibe Check

A transaction groups multiple database operations into one all-or-nothing bundle. Either ALL of them succeed, or NONE of them happen. Transferring money: subtract from one account AND add to another. If either fails, both are rolled back. No partial disasters.

Real Talk

A transaction is a unit of work that groups one or more SQL operations into an atomic sequence. Transactions follow ACID properties. They begin with BEGIN (or START TRANSACTION), and end with COMMIT (save changes) or ROLLBACK (undo changes). Savepoints allow partial rollbacks within a transaction.

Show Me The Code

BEGIN;
  UPDATE accounts SET balance = balance - 100 WHERE id = 1;
  UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;  -- or ROLLBACK if something goes wrong

When You'll Hear This

"Wrap the payment logic in a transaction to prevent partial updates." / "The transaction was rolled back due to a constraint violation."

Made with passive-aggressive love by manoga.digital. Powered by Claude.