SQL - Transactions and Data Integrity
You want to transfer $200 from account A (id=1) to account B (id=2) safely. Which SQL transaction block correctly ensures the transfer is atomic and consistent?
BEGIN TRANSACTION; UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT;uses
BEGIN TRANSACTION and COMMIT to group both updates safely. UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2;lacks transaction, risking partial update.
BEGIN TRANSACTION; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT;only credits without debit.
BEGIN; UPDATE accounts SET balance = balance - 200 WHERE id = 1; ROLLBACK;rolls back, so no change occurs.
BEGIN TRANSACTION; UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT;-> Option A
15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions