A transaction is a group of database operations treated as one logical unit.

Example

Money transfer:

  1. Debit sender.
  2. 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