MySQL + SQL · Lesson 81
Transactions in MySQL
What is a Transaction?
A transaction is a group of SQL statements treated as ONE unit — either all succeed (COMMIT) or none do (ROLLBACK). This keeps data safe during multi-step operations.
Bank Transfer Example
START TRANSACTION;
UPDATE accounts SET balance = balance - 500 WHERE id = 1;
UPDATE accounts SET balance = balance + 500 WHERE id = 2;
COMMIT; -- both updates saved togetherIf the second update failed, ROLLBACK would undo the first — money is never lost.
Key Commands
START TRANSACTION— begin.COMMIT— save all changes.ROLLBACK— undo all changes.
Summary
- A transaction groups statements into one all-or-nothing unit.
- COMMIT saves; ROLLBACK undoes. Essential for money/critical updates.