A3.3.6 Transactions, ACID and keeping data correct
HL onlyCommand term: Describe1. 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.
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.
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.
6. 45-second speed round: which TCL command?
Something has just happened. Which transaction control command comes next?
Learning objectives
By the end of this page you should be able to:
- Describe what a transaction is.
- Describe the TCL commands BEGIN TRANSACTION, COMMIT and ROLLBACK.
- Describe atomicity, consistency, isolation and durability (ACID).
- Explain how ACID ensures the reliable processing of transactions.
- 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.
TCL: transaction control language
| Command | Effect |
|---|---|
| BEGIN TRANSACTION | Starts a transaction. The following changes are provisional. (Some systems write START TRANSACTION or BEGIN.) |
| COMMIT | Ends the transaction and makes all its changes permanent and visible to other users. |
| ROLLBACK | Ends 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
| Property | Meaning | How it is achieved | Bank example |
|---|---|---|---|
| Atomicity | All 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. |
| Consistency | A 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. |
| Isolation | Transactions 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). |
| Durability | Once 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]
2. State the purpose of the COMMIT and ROLLBACK commands. [2]
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]
5. Two cinema staff try to sell the last seat at the same moment. Explain how isolation prevents both sales from succeeding. [3]
6. Describe how the ACID properties of transactions maintain data integrity in a database. [6]