Transactions — Complete Guide
Transactions — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of MySQL Tutorial on Toolliyo Academy.
On this page
MySQL Tutorial · Lesson 41 of 100
Transactions
Basics ✓ → Advanced
Advanced · 2 — Production · ~6 min · MySQL — Transactions & Concurrency
What is this?
A transaction groups SQL statements into one atomic unit: all succeed (COMMIT) or all undo (ROLLBACK). InnoDB is transactional by default.
Why should you care?
Checkout must deduct stock and create order together — half-done state loses money or oversells inventory.
See it live — copy this example
Run in MySQL Workbench or the mysql CLI.
START TRANSACTION;
UPDATE products SET stock_qty = stock_qty - 2 WHERE product_id = 10;
INSERT INTO orders (customer_id, order_ref, total_inr) VALUES (1, 'DF-TX-01', 1598.00);
COMMIT;
What happened?
- Stock drops and order inserts in one transaction.
- If INSERT fails, ROLLBACK restores stock.
- COMMIT makes both changes permanent together.
Practice next
- Add stock_qty column to products.
- Run START TRANSACTION; both statements; COMMIT;
- Repeat but ROLLBACK before COMMIT — verify stock unchanged.
- Intentionally violate FK on INSERT then ROLLBACK.
- Use BEGIN alias instead of START TRANSACTION.
Remember
START TRANSACTION begins a unit of work. COMMIT saves; ROLLBACK undoes. Use for multi-step money/inventory changes.
DataFlow checkout
Payment timeout triggers ROLLBACK so stock returns to catalog.
Outcome: No ghost orders or missing inventory.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!