Bird
0
0

Consider this SQL transaction:

medium📝 query result Q4 of 15
SQL - Transactions and Data Integrity
Consider this SQL transaction:
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

What happens if the second UPDATE fails?
AOnly the second update fails, first update commits
BThe first update remains, second update is skipped
CBoth updates are rolled back, no balance changes occur
DThe transaction commits partially with errors
Step-by-Step Solution
Solution:
  1. Step 1: Understand transaction atomicity

    Transactions are atomic, meaning all operations succeed or none do.
  2. Step 2: Analyze failure impact

    If the second update fails, the entire transaction rolls back to keep data consistent.
  3. Final Answer:

    Both updates are rolled back, no balance changes occur -> Option C
  4. Quick Check:

    Transaction atomicity means rollback on failure = C [OK]
Quick Trick: If one step fails, transaction rolls back all changes [OK]
Common Mistakes:
  • Assuming partial commits happen on failure
  • Believing first update stays after failure
  • Thinking transaction commits with errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes