lesson 62 Database engineering · advanced
Transactions & savepoint recovery
Commit changes together or recover with savepoints.
the idea
A transaction groups changes so they succeed or fail together. BEGIN starts it, COMMIT keeps the changes and ROLLBACK undoes everything since BEGIN. A SAVEPOINT marks a point inside the transaction; ROLLBACK TO that savepoint undoes only the later changes.
Dataset: Finance / Payments
The tables the challenges use, with their first 6 rows. Under each column name: the kind of value it holds, and which table it links to.
accounts7 columns · 2,109 rows
| account_idnumber · unique id | customer_idnumber · links to customers | account_typetext | opened_atdate | closed_attext | credit_limit_centsnumber | opening_balance_centsnumber |
|---|---|---|---|---|---|---|
| 1 | 1 | checking | 2022-02-24 | NULL | NULL | 43125 |
| 2 | 2 | checking | 2025-06-11 | NULL | NULL | 0 |
| 3 | 3 | checking | 2024-07-07 | NULL | NULL | 216925 |
| 4 | 3 | savings | 2024-10-07 | NULL | NULL | 475941 |
| 5 | 3 | credit_card | 2024-09-26 | NULL | 1020600 | -339148 |
| 6 | 4 | checking | 2023-07-18 | NULL | NULL | 155995 |
- account_idnumber · unique id
- 1
- customer_idnumber · links to customers
- 1
- account_typetext
- checking
- opened_atdate
- 2022-02-24
- closed_attext
- NULL
- credit_limit_centsnumber
- NULL
- opening_balance_centsnumber
- 43125
- account_idnumber · unique id
- 2
- customer_idnumber · links to customers
- 2
- account_typetext
- checking
- opened_atdate
- 2025-06-11
- closed_attext
- NULL
- credit_limit_centsnumber
- NULL
- opening_balance_centsnumber
- 0
- account_idnumber · unique id
- 3
- customer_idnumber · links to customers
- 3
- account_typetext
- checking
- opened_atdate
- 2024-07-07
- closed_attext
- NULL
- credit_limit_centsnumber
- NULL
- opening_balance_centsnumber
- 216925
- account_idnumber · unique id
- 4
- customer_idnumber · links to customers
- 3
- account_typetext
- savings
- opened_atdate
- 2024-10-07
- closed_attext
- NULL
- credit_limit_centsnumber
- NULL
- opening_balance_centsnumber
- 475941
- account_idnumber · unique id
- 5
- customer_idnumber · links to customers
- 3
- account_typetext
- credit_card
- opened_atdate
- 2024-09-26
- closed_attext
- NULL
- credit_limit_centsnumber
- 1020600
- opening_balance_centsnumber
- -339148
- account_idnumber · unique id
- 6
- customer_idnumber · links to customers
- 4
- account_typetext
- checking
- opened_atdate
- 2023-07-18
- closed_attext
- NULL
- credit_limit_centsnumber
- NULL
- opening_balance_centsnumber
- 155995
first 6 of 2,109 rows · your queries use all of them
worked example
Run these steps in order to see the idea before you write your own query. The step marked “the pattern” is what the challenges ask for. Running the example does not count toward progress.
Roll back an entire transaction. BEGIN adds 100 cents to account 1, then ROLLBACK throws that change away—you should see the original opening_balance_cents again.the pattern
example.sqlBEGIN; UPDATE accounts SET opening_balance_cents = opening_balance_cents + 100 WHERE account_id = 1; ROLLBACK; SELECT account_id, opening_balance_cents FROM accounts WHERE account_id = 1;
Undo only after a savepoint. The first UPDATE on account 3 stays; ROLLBACK TO the savepoint drops the zero-out, and COMMIT keeps the earlier change.
step-2.sqlBEGIN; UPDATE accounts SET opening_balance_cents = opening_balance_cents + 1 WHERE account_id = 3; SAVEPOINT s; UPDATE accounts SET opening_balance_cents = 0 WHERE account_id = 3; ROLLBACK TO s; COMMIT; SELECT account_id, opening_balance_cents FROM accounts WHERE account_id = 3;
common mistake
ROLLBACK TO a savepoint does not end the transaction; you still need COMMIT to keep the earlier changes.