Understanding Why Transactions Ensure Data Integrity
📖 Scenario: You are managing a simple bank database where users can transfer money between accounts. It is important that the money is deducted from one account and added to another account correctly, without losing or creating money accidentally.
🎯 Goal: Build a small MySQL transaction that transfers money between two accounts safely, ensuring data integrity by using transactions.
📋 What You'll Learn
Create a table called accounts with columns id (integer primary key) and balance (integer).
Insert two accounts with ids 1 and 2, with balances 1000 and 500 respectively.
Create a variable transfer_amount set to 200.
Write a transaction that deducts transfer_amount from account 1 and adds it to account 2.
Commit the transaction to save changes or rollback if any error occurs.
💡 Why This Matters
🌍 Real World
Banking systems and financial applications use transactions to ensure money transfers are accurate and reliable.
💼 Career
Understanding transactions is essential for database administrators and backend developers to maintain data integrity in multi-step operations.
Progress0 / 4 steps
1
Create the accounts table and insert initial data
Create a table called accounts with columns id as primary key and balance as integer. Insert two rows: (1, 1000) and (2, 500).
MySQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add rows.
2
Set the transfer amount variable
Create a variable called transfer_amount and set it to 200.
MySQL
Hint
Use SET @transfer_amount = 200; to create a user variable in MySQL.
3
Write the transaction to transfer money
Start a transaction with START TRANSACTION;. Deduct @transfer_amount from account with id = 1 and add @transfer_amount to account with id = 2. Use UPDATE accounts SET balance = balance - @transfer_amount WHERE id = 1; and UPDATE accounts SET balance = balance + @transfer_amount WHERE id = 2; inside the transaction.
MySQL
Hint
Use START TRANSACTION; to begin and UPDATE statements to change balances.
4
Commit the transaction to save changes
Add COMMIT; at the end to save the changes made in the transaction.
MySQL
Hint
Use COMMIT; to save all changes made in the transaction.
Practice
(1/5)
1. Why do transactions help maintain data integrity in MySQL?
easy
A. They speed up query execution by caching results.
B. They ensure all steps succeed or none do, avoiding partial updates.
C. They automatically backup the database after each query.
D. They allow multiple users to edit the same data simultaneously without locks.
Solution
Step 1: Understand the purpose of transactions
Transactions group multiple database operations into a single unit that either fully succeeds or fully fails.
Step 2: Explain how this prevents partial updates
If one step fails, the transaction rolls back all changes, preventing inconsistent or partial data states.
Final Answer:
They ensure all steps succeed or none do, avoiding partial updates. -> Option B
Quick Check:
All-or-nothing = data integrity [OK]
Hint: Remember: transactions are all-or-nothing actions [OK]
Hint: Always validate data before committing changes [OK]
Common Mistakes:
Thinking COMMIT causes errors
Believing MySQL disallows transactions on UPDATE
Assuming transaction start failed without evidence
5. You want to transfer $200 from account A (id=1) to account B (id=2) safely using transactions. Which sequence ensures data integrity?
hard
A. START TRANSACTION; UPDATE accounts SET balance = balance - 200 WHERE id = 1; ROLLBACK; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT;
B. UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2;
C. START TRANSACTION; UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT;
D. START TRANSACTION; UPDATE accounts SET balance = balance - 200 WHERE id = 1; COMMIT; UPDATE accounts SET balance = balance + 200 WHERE id = 2;
Solution
Step 1: Understand safe transfer with transactions
Both debit and credit updates must be in one transaction to avoid partial transfer.
Step 2: Analyze each option
START TRANSACTION; UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT; wraps both updates in a transaction and commits together, ensuring atomicity. UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2; lacks transaction, risking partial update. START TRANSACTION; UPDATE accounts SET balance = balance - 200 WHERE id = 1; COMMIT; UPDATE accounts SET balance = balance + 200 WHERE id = 2; commits debit separately, then credits outside transaction, risking partial update. START TRANSACTION; UPDATE accounts SET balance = balance - 200 WHERE id = 1; ROLLBACK; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT; rolls back debit before credit, causing inconsistency.
Final Answer:
START TRANSACTION; UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT; -> Option C
Quick Check:
Atomic debit and credit in one transaction = START TRANSACTION; UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT; [OK]
Hint: Group related updates inside one transaction [OK]
Common Mistakes:
Not using transactions for multi-step updates
Rolling back part of the transaction incorrectly
Updating accounts in wrong order without transaction
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.