SQL chapters

Chapter 13 of 17

Transactions and rollback

Try this in our SQLite Compiler →

The idea

A transaction groups changes so you can commit them together or roll them back. BEGIN starts one; COMMIT keeps changes and ROLLBACK cancels them. A failed statement does not mean every database engine automatically rolls back the whole transaction. Inspect the actual error and transaction state.

Worked example

DROP TABLE IF EXISTS points;
CREATE TABLE points (name TEXT, total INTEGER);
INSERT INTO points VALUES ('Asha', 10);
BEGIN;
UPDATE points SET total = 20;
ROLLBACK;
SELECT total FROM points;

What to check

The final total remains 10. This example deliberately reverses the change rather than simulating an error.

Practice task

Replace ROLLBACK with COMMIT in the disposable exercise and predict the new total. Never try this on important production data.

Check your understanding

Explain the result in your own words before changing the example. Change one thing at a time, compare the result with your prediction, and keep the version that demonstrates the concept clearly.

Try it yourself

Output
Run your project to see output here.
Ctrl/Cmd+Enter runs · Ctrl/Cmd+S downloads the project · Local auto-save is always on