Bird
0
0

Given this SQL code:

medium📝 query result Q4 of 15
SQL - Transactions and Data Integrity
Given this SQL code:
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
COMMIT;

What happens if the UPDATE statement fails?
AThe database shuts down to prevent errors
BThe transaction commits anyway, saving partial changes
CThe transaction remains open until manually rolled back
DThe transaction is rolled back automatically, no changes saved
Step-by-Step Solution
Solution:
  1. Step 1: Understand transaction failure behavior

    If an operation inside a transaction fails, the transaction typically enters a failed or aborted state depending on the DBMS.
  2. Step 2: Analyze the code flow

    The UPDATE fails, so the transaction is rolled back automatically or no changes are saved depending on DBMS behavior.
  3. Final Answer:

    The transaction is rolled back automatically, no changes saved -> Option D
  4. Quick Check:

    Failed operation inside transaction = automatic rollback or no commit [OK]
Quick Trick: Failed statements inside transactions cause rollback or no commit [OK]
Common Mistakes:
  • Assuming partial changes are saved on failure
  • Thinking COMMIT runs despite errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes