Home › Theme A › A3 › A3.3 › A3.3.6

A3.3.6 Transactions, ACID and keeping data correct

HL onlyCommand term: Describe

1. A bank transfer, step by step

Moving 100 from Aisha to Ben needs two UPDATE statements. Press Next to see how a transaction makes them succeed or fail together. Green = committed (permanent). Orange = changed inside the transaction, not yet committed.

2. Transaction sandbox: try to lose money

Run the statements in any order. Try a transfer without BEGIN TRANSACTION and pull the plug halfway. Then do it properly. The bank's total should always be 1000. The CHECK rule is Balance >= 0.

Auto-commit mode: each statement is committed on its own until you BEGIN a transaction.
Transaction log: empty.

3. Isolation lab: two withdrawals at once

One shared account holds 500. Transaction T1 withdraws 100 while T2 withdraws 200 at the same time. The correct final balance is 200. Step through with isolation off, then on.

Isolation:

4. Flip-card deck

Click to flip. Say the answer before you flip.

5. Which ACID property?

Each scenario shows one property protecting the data. Which one? Streaks of 3+ score double.

Round 0/10Score 0Streak 0Best 0
Press start!

6. 45-second speed round: which TCL command?

Something has just happened. Which transaction control command comes next?

Time 45sScore 0Streak 0Best 0
Press start!

Learning objectives

By the end of this page you should be able to:

  1. Describe what a transaction is.
  2. Describe the TCL commands BEGIN TRANSACTION, COMMIT and ROLLBACK.
  3. Describe atomicity, consistency, isolation and durability (ACID).
  4. Explain how ACID ensures the reliable processing of transactions.
  5. Describe how transactions maintain data integrity when an error or failure occurs.

What is a transaction?

A transaction is a group of one or more SQL statements that must be treated as a single unit of work: either all of them take effect, or none do. A bank transfer is the classic example: taking money from one account and adding it to another must both happen, or neither.

BEGIN TRANSACTION; UPDATE Account SET Balance = Balance - 100 WHERE Name = 'Aisha'; UPDATE Account SET Balance = Balance + 100 WHERE Name = 'Ben'; COMMIT; -- make both changes permanent -- or, if anything went wrong: ROLLBACK; -- undo every change since BEGIN TRANSACTION

TCL: transaction control language

CommandEffect
BEGIN TRANSACTIONStarts a transaction. The following changes are provisional. (Some systems write START TRANSACTION or BEGIN.)
COMMITEnds the transaction and makes all its changes permanent and visible to other users.
ROLLBACKEnds the transaction and undoes all its changes, returning the data to how it was at BEGIN.

Without BEGIN TRANSACTION, most systems use auto-commit: each statement is committed on its own. A failure between two related statements can then leave the data half-changed.

ACID

PropertyMeaningHow it is achievedBank example
AtomicityAll or nothing: every statement in the transaction succeeds, or none takes effect.ROLLBACK and the transaction log are used to undo partial work, including after a crash.A debit is never kept without its credit.
ConsistencyA transaction takes the database from one valid state to another; all rules (constraints, keys) still hold.Constraints (CHECK, PK, FK, NOT NULL) are checked; a violating transaction is rolled back.No balance goes below 0; the total money is unchanged by a transfer.
IsolationTransactions running at the same time do not interfere; each behaves as if it ran alone.Locking (or similar) stops others reading or writing data that is being changed.Two withdrawals cannot overwrite each other (no lost update).
DurabilityOnce committed, changes are permanent, even after a power failure or crash.Changes are written to non-volatile storage / the transaction log before COMMIT is confirmed.A confirmed transfer is still there after a restart.

How this maintains data integrity

  • No half-finished updates are left in the database (atomicity).
  • The data always obeys its rules (consistency).
  • Concurrent users cannot corrupt each other's work (isolation), linking to concurrency control in A3.1.1.
  • Confirmed work is never lost (durability).

Use the 中文 button at the top to show or hide the Chinese terms.

Term中文Meaning

Choose an answer. You get instant feedback.

Score: 0 /

Write your answer first, then reveal the markscheme. These are exam-style practice questions written for this site.

1. Define the term transaction in the context of databases. [2]

A group of one or more database operations / SQL statements [1] treated as a single logical unit of work that must either be completed fully or not at all [1].

2. State the purpose of the COMMIT and ROLLBACK commands. [2]

COMMIT makes all changes in the transaction permanent [1]; ROLLBACK undoes all changes made since the transaction began [1].

3. An online shop places an order by inserting an ORDERS record and reducing the stock level in PRODUCT. Construct the SQL, using TCL commands, to make these two changes a single transaction. [4]

BEGIN TRANSACTION; [1] INSERT INTO ORDERS VALUES (...); [1] UPDATE PRODUCT SET Stock = Stock - 1 WHERE ProductID = ...; [1] COMMIT; [1]. Accept a ROLLBACK described for the failure case; accept START TRANSACTION.

4. Describe atomicity and durability, using a bank transfer as an example. [4]

Atomicity: all statements in the transaction succeed or none do [1]; e.g. if the debit from one account succeeds but the credit fails, the debit is rolled back so no money disappears [1]. Durability: once a transaction is committed its changes are permanent [1]; e.g. after the transfer is confirmed, a power failure does not undo it, because it has been written to the log / non-volatile storage [1].

5. Two cinema staff try to sell the last seat at the same moment. Explain how isolation prevents both sales from succeeding. [3]

The first transaction locks the seat record when it reads/updates it [1]; the second transaction must wait until the first commits or rolls back [1]; it then reads the updated record, sees the seat is sold, and cannot sell it again, so there is no double booking / lost update [1].

6. Describe how the ACID properties of transactions maintain data integrity in a database. [6]

Award up to [2] for each property described with its effect on integrity, to a maximum of [6]. Atomicity: all-or-nothing, so partial updates are never left in the database [2]. Consistency: transactions move the database between valid states, so constraints / business rules are never broken [2]. Isolation: concurrent transactions do not interfere, so problems such as lost updates or reading uncommitted data are avoided [2]. Durability: committed changes survive crashes or power loss, so confirmed data is not lost [2].