Bird
Raised Fist0
MySQLquery~10 mins

Why transactions ensure data integrity in MySQL - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a transaction in MySQL.

MySQL
START [1];
Drag options to blanks, or click blank then click option'
ASAVEPOINT
BCOMMIT
CROLLBACK
DTRANSACTION
Attempts:
3 left
💡 Hint
Common Mistakes
Using COMMIT or ROLLBACK to start a transaction.
Using SAVEPOINT to start a transaction.
2fill in blank
medium

Complete the code to save changes permanently in a transaction.

MySQL
[1];
Drag options to blanks, or click blank then click option'
ASTART TRANSACTION
BROLLBACK
CCOMMIT
DSAVEPOINT
Attempts:
3 left
💡 Hint
Common Mistakes
Using START TRANSACTION to save changes.
Using ROLLBACK instead of COMMIT.
3fill in blank
hard

Fix the error in the code to undo changes in a transaction.

MySQL
[1];
Drag options to blanks, or click blank then click option'
ACOMMIT
BROLLBACK
CSTART TRANSACTION
DSAVEPOINT
Attempts:
3 left
💡 Hint
Common Mistakes
Using COMMIT to undo changes.
Using START TRANSACTION to undo changes.
4fill in blank
hard

Fill both blanks to create a savepoint and rollback to it.

MySQL
SAVEPOINT [1];
ROLLBACK TO [2];
Drag options to blanks, or click blank then click option'
Asp1
Btransaction1
Csave1
Drollback1
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for savepoint and rollback.
Using reserved words as savepoint names.
5fill in blank
hard

Fill all three blanks to start a transaction, update a value, and commit the changes.

MySQL
START [1];
UPDATE accounts SET balance = balance [2] 100 WHERE id = 1;
[3];
Drag options to blanks, or click blank then click option'
ATRANSACTION
B+
CCOMMIT
DROLLBACK
Attempts:
3 left
💡 Hint
Common Mistakes
Using ROLLBACK instead of COMMIT to save changes.
Using '-' instead of '+' to add money.

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

  1. Step 1: Understand the purpose of transactions

    Transactions group multiple database operations into a single unit that either fully succeeds or fully fails.
  2. Step 2: Explain how this prevents partial updates

    If one step fails, the transaction rolls back all changes, preventing inconsistent or partial data states.
  3. Final Answer:

    They ensure all steps succeed or none do, avoiding partial updates. -> Option B
  4. Quick Check:

    All-or-nothing = data integrity [OK]
Hint: Remember: transactions are all-or-nothing actions [OK]
Common Mistakes:
  • Thinking transactions speed up queries
  • Believing transactions auto-backup data
  • Assuming transactions allow unsafe simultaneous edits
2. Which of the following is the correct syntax to start a transaction in MySQL?
easy
A. START TRANSACTION;
B. OPEN TRANSACTION;
C. BEGIN TRANSACTION;
D. INIT TRANSACTION;

Solution

  1. Step 1: Recall MySQL transaction start syntax

    MySQL uses START TRANSACTION; to begin a transaction explicitly.
  2. Step 2: Compare options with correct syntax

    BEGIN TRANSACTION;, OPEN TRANSACTION;, and INIT TRANSACTION; are not recognized by MySQL for starting transactions.
  3. Final Answer:

    START TRANSACTION; -> Option A
  4. Quick Check:

    Start transaction command = START TRANSACTION [OK]
Hint: Use START TRANSACTION to begin safely [OK]
Common Mistakes:
  • Using BEGIN TRANSACTION which is not valid in MySQL
  • Confusing transaction commands with other SQL commands
  • Assuming INIT TRANSACTION is correct syntax
3. Consider this sequence of SQL commands in MySQL:
START TRANSACTION;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
ROLLBACK;

What will be the balance of account with id=1 after these commands?
medium
A. NULL
B. 50
C. 0
D. 100

Solution

  1. Step 1: Analyze the transaction commands

    The transaction starts, then subtracts 50 from the balance of account with id=1.
  2. Step 2: Understand the effect of ROLLBACK

    ROLLBACK cancels all changes made in the transaction, so the update is undone.
  3. Final Answer:

    100 -> Option D
  4. Quick Check:

    Rollback cancels changes = balance stays 100 [OK]
Hint: ROLLBACK undoes all changes in the transaction [OK]
Common Mistakes:
  • Assuming update persists after rollback
  • Thinking balance becomes 50 after update
  • Confusing ROLLBACK with COMMIT
4. You wrote this MySQL transaction:
START TRANSACTION;
UPDATE inventory SET quantity = quantity - 5 WHERE product_id = 10;
COMMIT;

But the quantity became negative. What is the likely problem?
medium
A. There was no check to prevent quantity from going below zero.
B. The transaction was not started properly.
C. COMMIT was used instead of ROLLBACK.
D. MySQL does not support transactions on UPDATE statements.

Solution

  1. Step 1: Review the transaction logic

    The transaction updates quantity by subtracting 5 but does not check if quantity is already less than 5.
  2. Step 2: Identify missing validation

    Without a condition to prevent negative values, quantity can become negative despite the transaction.
  3. Final Answer:

    There was no check to prevent quantity from going below zero. -> Option A
  4. Quick Check:

    Validation missing = negative quantity allowed [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

  1. Step 1: Understand safe transfer with transactions

    Both debit and credit updates must be in one transaction to avoid partial transfer.
  2. 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.
  3. 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
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard