The transfer that must not half-happen
After this: Wrap two changes in BEGIN and COMMIT, and undo them with ROLLBACK.
On this page · 7 sections · 3 checks
Ana sends Ben 30. For the bank, that is two changes: take 30 from Ana's account, and add 30 to Ben's. Each change on its own is an UPDATE from lesson 2. The trouble starts when the program making them stops between the two: the power fails, the program crashes, the network drops. This lesson shows what is left behind, and how to make sure it is all or nothing.
The bank table
This lesson and the next use a small table of bank accounts instead of the school. It has two rows, and a rule that a balance can never go below 0:
Ana has 100 and Ben has 50, so the bank holds 150 in all. After any transfer between them, the total must still be 150.
Stopping halfway
The transfer is two statements:
UPDATE accounts SET balance = balance - 30 WHERE id = 1;UPDATE accounts SET balance = balance + 30 WHERE id = 2;balance = balance - 30 means "the new balance is the old balance minus 30". Now suppose the program stops after the first statement. The block below runs only the first UPDATE, as if the program had died there, then reads the table:
Ana has 70 and Ben has 50: a total of 120. Thirty has vanished. Each statement was all-or-nothing on its own, as lesson 2 showed, but the transfer as a whole was not.
A transaction
The fix is to tell the database that the two statements belong together. A is a group of statements that takes effect completely or not at all. You mark it with two words:
BEGIN;starts the transaction.- The statements run. Their changes are visible inside the transaction, but they are not final yet.
- ends the transaction and makes every change in it final, together.
The list shows each statement in order: BEGIN, two UPDATEs with "1 row changed" each, COMMIT, then the SELECT. Ana has 70 and Ben has 80: still 150.
Stopping halfway, inside a transaction
Now stop after the first UPDATE again, but this time inside a transaction. First, look from inside, before anything is final:
Inside the transaction, Ana reads 70. The change is there, but it is not final. Each Run's database is thrown away when the Run ends, so this block cannot show what comes next. The next block ends the transaction on purpose with , which undoes every change since BEGIN:
Ana has 100 again, and Ben 50. The total is 150, as if the transfer had never started. Nothing vanished.
What if the program crashes instead, and never sends ROLLBACK? A database kept in a file on disk does the same thing for you. SQLite keeps enough on disk to undo an unfinished transaction. When the file is opened again after a crash between BEGIN and COMMIT, the unfinished change is undone: Ana reads 100. A transaction that was never committed never happened.
The rule
You have now seen three endings of the same transfer:
| How it ends | Ana | Ben | Total |
|---|---|---|---|
| Two UPDATEs, no transaction, program stops after the first | 70 | 50 | 120 |
| BEGIN, two UPDATEs, COMMIT | 70 | 80 | 150 |
| BEGIN, one UPDATE, then ROLLBACK or a crash | 100 | 50 | 150 |
When several changes must happen together, put them between BEGIN and COMMIT. Then the database holds either all of them or none of them, and never a half.
Check yourself
Ana has 100 and Ben 50. There is no BEGIN, and the program stops after the first UPDATE. What does the SELECT show?
UPDATE accounts SET balance = balance - 30 WHERE id = 1;-- the program stops here: the second UPDATE never runs
SELECT * FROM accounts;This time the first UPDATE runs inside a transaction, and then the program sends ROLLBACK. What does the SELECT show?
BEGIN;UPDATE accounts SET balance = balance - 30 WHERE id = 1;ROLLBACK;
SELECT * FROM accounts;With no BEGIN, the bank takes a fee of 60 from every account at once. Ben has only 50, so the statement fails with CHECK constraint failed: balance >= 0. What is Ana's balance now?
UPDATE accounts SET balance = balance - 60;Honesty notes
- Say why a transfer is two changes, and what stopping between them leaves without a transaction: Ana 70, Ben 50, 30 gone.
- Group statements in a transaction with BEGIN and COMMIT, so that all of them take effect together.
- Undo every change since BEGIN with ROLLBACK, and say that a crash before COMMIT has the same effect on a database file.
- Say that changes inside an open transaction are visible inside it but are not final until COMMIT.