Database Basics

In an account table, a transaction is started, and an UPDATE that subtracts 300 from account A's balance and an UPDATE that adds 300 to account B's balance are executed. Then ROLLBACK is executed instead of COMMIT. What happens to the account table?

  1. AOnly the addition to account B is cancelled; the subtraction from account A remains
  2. BBoth UPDATEs are cancelled, and the table returns to its state before the transaction started
  3. CBecause the UPDATEs have already been executed, neither is cancelled
Show answer and explanation

Answer: B. Both UPDATEs are cancelled, and the table returns to its state before the transaction started

A transaction is a mechanism that treats a series of operations on a database as a single unit. Updates made within a transaction become permanent when COMMIT is executed, and are all cancelled when ROLLBACK is executed, returning the data to its state before the transaction started. In this example, both the subtraction from account A and the addition to account B are cancelled, and the two balances are unchanged. If an error occurs partway through a transfer, rolling back ensures that no half-finished state with only one account updated remains. To cancel only part of the work, you can mark a point with SAVEPOINT and return to it with ROLLBACK TO SAVEPOINT.

  • A. ROLLBACK cancels every update in the transaction, not only the last statement.
  • B. Correct. All uncommitted updates are cancelled, returning to the state before the transaction began.
  • C. Updates are not permanent before COMMIT, so ROLLBACK cancels them.
Practice this topic Back to topic

Related calculators

Books on Database Basics Ad

As an Amazon Associate I earn from qualifying purchases.

More Database Basics questions

  1. Among the ACID properties a transaction should have, which is the property that a transfer debiting account A and crediting account B is either carried out completely or not at all, even if a failure occurs partway through?
  2. The stock count of a product in a stock table is 10. Transaction T1 reads the stock count and writes back the value minus 3, and T2 reads the stock count and writes back the value minus 5. If they run without locks in the order T1 reads, T2 reads, T1 writes, T2 writes, what is the final stock count?
  3. Among the problems that occur when transactions run concurrently, which correctly describes a dirty read?
  4. Transaction T1 ran SELECT COUNT(*) FROM orders WHERE amount >= 1000 and got 8. Immediately afterwards, T2 inserted an order row with an amount of 2000 and committed. When T1 ran the same SELECT statement again, it got 9. What is this phenomenon?
  5. Among the isolation levels defined by the SQL standard, which correctly lists the phenomena that can occur under READ COMMITTED?