SQL transactions: BEGIN, COMMIT and ROLLBACK
Some changes only make sense together: take 30 packs out of the East warehouse and book them into the West one. A transaction bundles those statements into one unit that either happens completely or not at all.
In short: BEGIN; starts a transaction. The changes that follow only count as saved once you run COMMIT;. ROLLBACK; throws away everything since BEGIN instead.
What you should know first: changing data with UPDATE and INSERT, plus primary and foreign keys.
Try it now
Write several statements one below the other and end each with a semicolon. After they run, the exercise shows you the table as it now stands.
Task The East warehouse is sending 30 packs of copy paper (product_id 1) to the West warehouse. Record the transfer in warehouse_stock: 30 fewer in East, 30 more in West. Both changes belong together, so put them in one transaction with BEGIN and COMMIT.
warehouse_stock 8 rows, 3 columns
| warehouse | product_id | quantity |
|---|---|---|
| East | 1 | 260 |
| East | 2 | 200 |
| East | 3 | 120 |
| East | 6 | 40 |
| West | 1 | 160 |
| West | 2 | 110 |
| West | 3 | 65 |
| West | 6 | 24 |
How a transaction works
Without BEGIN, the database saves every change on its own the moment it finishes. That's called autocommit, and it's the default in SQLite, MySQL and PostgreSQL. For a stock transfer that's risky: if the server goes down between the first and the second UPDATE, 30 packs have left East and never arrived in West.
BEGIN;
UPDATE warehouse_stock SET quantity = quantity - 30
WHERE warehouse = 'East' AND product_id = 1;
UPDATE warehouse_stock SET quantity = quantity + 30
WHERE warehouse = 'West' AND product_id = 1;
COMMIT;
Between BEGIN and COMMIT the changes are provisional. COMMIT makes both of them permanent at once. If anything breaks before that, neither change is there after a restart. Afterwards, the copy paper stock looks like this:
| warehouse | product_id | quantity |
|---|---|---|
| East | 1 | 230 |
| West | 1 | 190 |
Together that's still 420 packs, matching the stock in products. A transaction makes sure totals like this are never wrong, not even for a moment.
ROLLBACK: back to square one
Write ROLLBACK instead of COMMIT and the database discards every change since BEGIN, as if nothing had happened. That's handy for a dry run: make the change, check its effect with SELECT, undo it. In these exercises you always see the state after all your statements have run; a SELECT in the middle isn't displayed. So you can still tell what your dry run did, the next exercise counts the rows deleted between BEGIN and ROLLBACK.
Task Raj Patel (customer_id 4) has asked for his data to be erased. Before that happens for real, accounting needs to check whether his orders have to be kept for the records. Do a dry run: start a transaction, delete his line items, his orders and then him from customers, and undo everything at the end with ROLLBACK. Afterwards the exercise shows how many customers, orders and line items there are, and in deleted_in_dry_run how many rows your dry run had deleted.
customers 12 rows, 7 columns
| customer_id | name | city | country | customer_since | segment | phone |
|---|---|---|---|---|---|---|
| 1 | Harbor Fremont Partners | Boston | USA | 2021-03-15 | Business | 617-555-0142 |
| 2 | Emily Carter | Chicago | USA | 2022-07-01 | Consumer | NULL |
| 3 | Riverside County Office | Riverside | USA | 2020-11-20 | Government | 951-555-0110 |
| 4 | Raj Patel | Leeds | UK | 2023-01-09 | Consumer | 0113 496 0123 |
| 5 | Lakeview Hotel Ltd | Manchester | UK | 2022-04-18 | Business | 0161 496 0456 |
| 6 | Olivia Brooks | Austin | USA | 2024-02-27 | Consumer | NULL |
| 7 | Greene & Shaw LLP | London | UK | 2021-09-05 | Business | 020 7946 0321 |
| 8 | Maple Grove High School | Austin | USA | 2023-08-14 | School | 512-555-0187 |
| 9 | Chloe Evans | Boston | USA | 2025-05-30 | Consumer | 617-555-0199 |
| 10 | Northline Printing Co | Toronto | Canada | 2020-06-12 | Business | 416-555-0133 |
| 11 | Daniel Kim | Seattle | USA | 2024-10-03 | Consumer | NULL |
| 12 | Westbrook Dental Clinic | Chicago | USA | 2022-12-01 | Business | 312-555-0164 |
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
order_items 38 rows, 4 columns
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1001 | 1 | 40 | 4.79 |
| 1001 | 3 | 25 | 2.79 |
| 1001 | 6 | 4 | 24.9 |
| 1002 | 2 | 2 | 3.49 |
| 1002 | 10 | 3 | 5.49 |
| 1003 | 1 | 60 | 4.69 |
| 1003 | 11 | 6 | 84.9 |
| 1003 | 3 | 50 | 2.59 |
| 1004 | 4 | 3 | 179 |
| 1004 | 9 | 3 | 269 |
| 1005 | 5 | 1 | 449 |
| 1006 | 1 | 30 | 4.79 |
| 1006 | 7 | 10 | 6.99 |
| 1007 | 8 | 2 | 219 |
| 1007 | 11 | 4 | 89.9 |
| 1007 | 1 | 20 | 4.99 |
| 1008 | 4 | 6 | 175 |
| 1008 | 2 | 5 | 3.49 |
| 1009 | 10 | 120 | 4.99 |
| 1009 | 2 | 40 | 3.19 |
| 1009 | 9 | 2 | 279 |
| 1010 | 7 | 2 | 6.99 |
| 1010 | 2 | 1 | 3.49 |
| 1011 | 12 | 1 | 129 |
| 1011 | 1 | 10 | 4.99 |
| 1011 | 3 | 12 | 2.79 |
| 1012 | 6 | 8 | 23.9 |
| 1012 | 1 | 40 | 4.69 |
| 1013 | 9 | 1 | 279 |
| 1014 | 5 | 2 | 439 |
| 1014 | 4 | 2 | 179 |
| 1015 | 1 | 50 | 4.79 |
| 1015 | 11 | 2 | 89.9 |
| 1016 | 10 | 4 | 5.49 |
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.99 |
| 1018 | 1 | 80 | 4.79 |
| 1018 | 6 | 10 | 24.9 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
What happens when a statement fails
An error inside a transaction doesn't automatically undo everything. When a rule such as a duplicate key or a missing required value is broken, SQLite backs out only the statement that failed. The transaction stays open and the decision is yours: ROLLBACK or carry on. PostgreSQL puts the transaction into an aborted state after an error, and only ROLLBACK or ROLLBACK TO gets you out of it. SQL Server rolls the whole transaction back on any run-time error if SET XACT_ABORT ON is in effect. So don't count on any of it: after an error, issue an explicit ROLLBACK.
SAVEPOINT: undo just part of it
A savepoint is a marker inside a transaction. ROLLBACK TO name undoes only what came after the marker, and the transaction carries on:
BEGIN;
UPDATE products SET price = ROUND(price * 1.05, 2) WHERE category = 'Paper';
SAVEPOINT before_electronics;
UPDATE products SET price = ROUND(price * 1.05, 2) WHERE category = 'Electronics';
ROLLBACK TO before_electronics;
COMMIT;
Afterwards copy paper costs 5.24 instead of 4.99 and the notebook 5.76 instead of 5.49, while electronics keep their old prices.
One more exercise: an order as one unit
A new order touches three tables. Watch the order of your statements: the foreign key is checked on every single statement, even inside a transaction.
Task Olivia Brooks (customer_id 6) orders two monitors (product_id 9) on 2026-06-16. Record the order as one unit: in a single transaction, create order 1020 with status open and no employee, add its line item at the current price from products, and take the two units off the stock.
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
order_items 38 rows, 4 columns
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1001 | 1 | 40 | 4.79 |
| 1001 | 3 | 25 | 2.79 |
| 1001 | 6 | 4 | 24.9 |
| 1002 | 2 | 2 | 3.49 |
| 1002 | 10 | 3 | 5.49 |
| 1003 | 1 | 60 | 4.69 |
| 1003 | 11 | 6 | 84.9 |
| 1003 | 3 | 50 | 2.59 |
| 1004 | 4 | 3 | 179 |
| 1004 | 9 | 3 | 269 |
| 1005 | 5 | 1 | 449 |
| 1006 | 1 | 30 | 4.79 |
| 1006 | 7 | 10 | 6.99 |
| 1007 | 8 | 2 | 219 |
| 1007 | 11 | 4 | 89.9 |
| 1007 | 1 | 20 | 4.99 |
| 1008 | 4 | 6 | 175 |
| 1008 | 2 | 5 | 3.49 |
| 1009 | 10 | 120 | 4.99 |
| 1009 | 2 | 40 | 3.19 |
| 1009 | 9 | 2 | 279 |
| 1010 | 7 | 2 | 6.99 |
| 1010 | 2 | 1 | 3.49 |
| 1011 | 12 | 1 | 129 |
| 1011 | 1 | 10 | 4.99 |
| 1011 | 3 | 12 | 2.79 |
| 1012 | 6 | 8 | 23.9 |
| 1012 | 1 | 40 | 4.69 |
| 1013 | 9 | 1 | 279 |
| 1014 | 5 | 2 | 439 |
| 1014 | 4 | 2 | 179 |
| 1015 | 1 | 50 | 4.79 |
| 1015 | 11 | 2 | 89.9 |
| 1016 | 10 | 4 | 5.49 |
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.99 |
| 1018 | 1 | 80 | 4.79 |
| 1018 | 6 | 10 | 24.9 |
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
ACID in plain English
What a transaction promises is usually summed up in four letters:
- Atomic: all or nothing. Both stock movements, or neither.
- Consistent: once the transaction ends, every rule on the tables holds again: foreign keys, NOT NULL, CHECK.
- Isolated: other people don't see your half-finished changes. In the words of the PostgreSQL docs, updates made by an open transaction stay invisible to other transactions until it completes, and then they all become visible at once.
- Durable: once COMMIT has confirmed it, it stays saved, even if the power cuts out a second later.
Isolation only matters when several people work on the same database at the same time. Every time you run or check an exercise here, it starts from a fresh database, so you can't try that part here. One habit to take to work: keep transactions short. Microsoft warns that in SQL Server, transactions left open for a long time can lock data other sessions are waiting for.
Common mistakes
Forgetting BEGIN or COMMIT
Without BEGIN each UPDATE runs on its own. The result is right, but it isn't a transaction, and the exercise says "Solve this one with BEGIN". Leave out COMMIT and the transaction stays open; the message then names COMMIT.
ROLLBACK or COMMIT without BEGIN
The exercise then says "No transaction is running. COMMIT and ROLLBACK need a BEGIN first." In a real database, the changes before it would already be saved by then.
Deleting or inserting in the wrong order
Delete orders before their line items, or add a line item before its order, and the statement fails on the foreign key; the exercise reports a foreign key error. When inserting, the new row points to nothing; when deleting, other rows still point to the one you're trying to remove. That holds in a dry run before ROLLBACK too.
START TRANSACTION in the exercises
That's the MySQL spelling. SQLite doesn't know it, and the exercise rejects the statement. Use BEGIN.
Typing in fixed values
SET quantity = 230 is only right with today's data. The check also runs on different stock levels and then reports "The result is right with this data but not with other data". Work from the current value: quantity = quantity - 30.
Transactions in MySQL, PostgreSQL, SQL Server and SQLite
| Database | Start | Savepoint | Back to the savepoint |
|---|---|---|---|
| MySQL | START TRANSACTION (BEGIN works too) | SAVEPOINT name | ROLLBACK TO SAVEPOINT name |
| PostgreSQL | BEGIN | SAVEPOINT name | ROLLBACK TO name |
| SQL Server | BEGIN TRANSACTION or BEGIN TRAN | SAVE TRANSACTION name | ROLLBACK TRANSACTION name |
| SQLite (these exercises) | BEGIN | SAVEPOINT name | ROLLBACK TO name |
COMMIT and ROLLBACK are called that everywhere; SQL Server also accepts COMMIT TRANSACTION. In SQLite, END is another name for COMMIT. One MySQL trap: statements such as CREATE TABLE, ALTER TABLE, CREATE VIEW and DROP VIEW quietly end an open transaction with a COMMIT, so a later ROLLBACK can't bring the earlier changes back. MySQL also saves changes to tables in a storage engine without transaction support immediately, even inside a transaction. InnoDB, the usual engine, does support transactions.
Sources: MySQL Reference Manual (START TRANSACTION, COMMIT, and ROLLBACK; SAVEPOINT; Statements That Cause an Implicit Commit), PostgreSQL documentation (Tutorial: Transactions), Microsoft Learn (BEGIN TRANSACTION, SAVE TRANSACTION), sqlite.org (Transaction, Savepoints, ON CONFLICT).
Frequently asked questions
Does a single UPDATE need a transaction?
No. A single statement already runs as its own small transaction: an UPDATE across 100 rows changes all 100 or none. You need BEGIN and COMMIT when several statements belong together.
Can I roll back a CREATE TABLE?
In SQLite, yes: CREATE TABLE and DROP TABLE are part of the transaction there. In MySQL, no, because those statements commit the transaction first.
Can I nest transactions?
SQLite refuses a second BEGIN inside an open transaction. Savepoints are the tool for checkpoints within one. SQL Server does accept an inner BEGIN TRANSACTION, but a ROLLBACK still rolls back the whole outer transaction.