Transaction
A transaction is a group of statements that all succeed or all fail. Fee payment: subtract wallet, insert receipt. START TRANSACTION; UPDATE …; INSERT …; COMMIT; If the insert fails, ROLLBACK; — wallet should not stay subtracted.
ACID: Atomic (all or nothing), Consistent (rules kept), Isolated (other sessions don’t see half-work), Durable (after COMMIT, it survives a crash). InnoDB supports this. MyISAM does not.
autocommit is ON by default — each statement is its own transaction. START TRANSACTION (or BEGIN) turns a batch into one unit. COMMIT ends it. ROLLBACK undoes since START.
Trap — no transaction around two related writes. Trap: COMMIT after an error you did not notice.
On the example next to this theory: Transaction: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.
START TRANSACTION
UPDATE wallet
INSERT receipt
│
├── COMMIT → both kept
└── ROLLBACK → both undoneSTART TRANSACTION; two writes; COMMIT; or ROLLBACK;