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.
💻 Live Code Editor
Is page ki language (MYSQL) mein code likhein aur turant chalाएं — yahीं, bina kuch install kiye.Powered by OneCompiler. Agar editor na khule to yahान naye tab mein kholein.
💻 Live Code Editor
Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें.
Agar load na ho to naye tab mein kholें.