Understanding ACID Properties with MySQL Transactions
📖 Scenario: You are managing a small online bookstore database. You want to ensure that when a customer places an order, the database updates the stock and records the order correctly without errors or data loss.
🎯 Goal: Build a simple MySQL transaction that demonstrates the ACID properties by updating stock and inserting an order record safely.
📋 What You'll Learn
Create a table called books with columns id, title, and stock
Create a table called orders with columns order_id, book_id, and quantity
Set a variable @order_quantity to 2
Write a transaction that decreases the stock of a book with id = 1 by @order_quantity
Insert a new order record with book_id = 1 and quantity = @order_quantity
Commit the transaction to save changes
💡 Why This Matters
🌍 Real World
Online stores and inventory systems use transactions to keep data accurate and consistent when multiple changes happen together.
💼 Career
Understanding ACID transactions is essential for database administrators and backend developers to ensure reliable data operations.
Progress0 / 4 steps
1
Create the initial tables
Create a table called books with columns id (integer primary key), title (varchar 100), and stock (integer). Also create a table called orders with columns order_id (integer primary key auto_increment), book_id (integer), and quantity (integer).
MySQL
Hint
Use CREATE TABLE statements with the exact column names and types.
2
Set the order quantity variable
Set a MySQL user-defined variable called @order_quantity to the value 2.
MySQL
Hint
Use SET @order_quantity = 2; to create the variable.
3
Write the transaction to update stock and insert order
Start a transaction with START TRANSACTION;. Then write an UPDATE statement to reduce the stock of the book with id = 1 by @order_quantity. Next, write an INSERT statement to add a new row into orders with book_id = 1 and quantity = @order_quantity.
MySQL
Hint
Use START TRANSACTION; then UPDATE and INSERT statements as described.
4
Commit the transaction
Add the statement COMMIT; to save the changes made in the transaction.
MySQL
Hint
Use COMMIT; to finalize the transaction.
Practice
(1/5)
1. Which of the following is NOT one of the ACID properties in databases?
easy
A. Durability and Speed
B. Consistency
C. Isolation
D. Atomicity
Solution
Step 1: Recall the ACID properties
ACID stands for Atomicity, Consistency, Isolation, and Durability.
Step 2: Identify the incorrect option
Durability and Speed adds "Speed" which is not part of ACID properties.
2. Which SQL statement correctly starts a transaction in MySQL?
easy
A. BEGIN TRANSACTION;
B. START TRANSACTION;
C. OPEN TRANSACTION;
D. INIT TRANSACTION;
Solution
Step 1: Recall MySQL transaction syntax
MySQL uses START TRANSACTION; to begin a transaction.
Step 2: Check other options
Options A, B, and D are not valid MySQL commands for starting transactions.
Final Answer:
START TRANSACTION; -> Option B
Quick Check:
MySQL uses START TRANSACTION [OK]
Hint: Use START TRANSACTION to begin in MySQL [OK]
Common Mistakes:
Using BEGIN TRANSACTION which is not valid in MySQL
Confusing with other SQL dialects
Typing incorrect keywords like OPEN or INIT
3. Consider this sequence in MySQL:
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
What ACID property does this sequence mainly demonstrate?
medium
A. Consistency - data types are checked
B. Isolation - changes are visible immediately
C. Durability - changes are temporary
D. Atomicity - all changes happen or none
Solution
Step 1: Understand the transaction sequence
The two updates are grouped inside a transaction with COMMIT at the end.
Step 2: Identify the ACID property
Atomicity ensures either both updates happen or none do, preventing partial changes.
Final Answer:
Atomicity - all changes happen or none -> Option D
Quick Check:
Transaction groups changes atomically [OK]
Hint: Atomicity means all or nothing in transactions [OK]
Common Mistakes:
Confusing Isolation with Atomicity
Thinking Durability means temporary changes
Assuming Consistency only checks data types
4. You wrote this MySQL transaction:
START TRANSACTION;
UPDATE inventory SET quantity = quantity - 5 WHERE product_id = 10;
COMMIT;
But sometimes the quantity becomes negative. What ACID property is likely violated?
medium
A. Consistency
B. Isolation
C. Durability
D. Atomicity
Solution
Step 1: Analyze the problem
Quantity becoming negative means data rules are broken.
Step 2: Identify violated ACID property
Consistency ensures data stays valid and follows rules; here it is violated.
Final Answer:
Consistency -> Option A
Quick Check:
Consistency keeps data valid [OK]
Hint: Consistency means data must follow rules always [OK]
Common Mistakes:
Thinking Atomicity prevents negative values
Confusing Isolation with data validity
Assuming Durability fixes data rules
5. In a multi-user MySQL environment, two transactions run:
Transaction 1:
START TRANSACTION;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
-- waits before COMMIT
Transaction 2:
START TRANSACTION;
SELECT balance FROM accounts WHERE id = 1;
COMMIT;
Which ACID property ensures Transaction 2 does not see the uncommitted balance change from Transaction 1?
hard
A. Atomicity
B. Durability
C. Isolation
D. Consistency
Solution
Step 1: Understand concurrent transactions
Transaction 1 updates but delays commit; Transaction 2 reads the same data.