Bird
0
0

Consider this SQL code:

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

What is the effect of this transaction?
AThe transaction will cause an error
BOnly the first update is saved
CBoth updates happen together or not at all
DUpdates happen immediately without grouping
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the transaction block

    The two updates are inside a transaction started by BEGIN TRANSACTION; and ended by COMMIT;.
  2. Step 2: Understand transaction behavior

    Both updates will be saved together if no errors occur; if an error happens, none will be saved.
  3. Final Answer:

    Both updates happen together or not at all -> Option C
  4. Quick Check:

    Transaction = all or nothing [OK]
Quick Trick: Transactions group changes to save or cancel together [OK]
Common Mistakes:
  • Thinking updates save separately
  • Assuming immediate save without commit
  • Believing transaction causes error here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes