Before continuing: This entry is part of the transactions guide. If you are new to the topic, start with BEGIN / START TRANSACTION.
COMMIT example
BEGIN;
UPDATE orders
SET status = 'shipped'
WHERE id = 102;
SELECT id, status, total
FROM orders
WHERE id = 102;
COMMIT;What to look for: First check order 102 with the SELECT and commit only if the new status is correct. After COMMIT, a ROLLBACK of that transaction no longer undoes the update.
Check the result with small data
We create a small table and write a row inside a transaction.
CREATE TABLE demo_confirm (id INTEGER PRIMARY KEY, status TEXT);
BEGIN;
INSERT INTO demo_confirm VALUES (1, 'shipped');
COMMIT;
SELECT id, status FROM demo_confirm;| id | status |
|---|---|
| 1 | shipped |
After COMMIT, the change is permanent and can be queried. In this example the table only exists for the demo; in a real application you should commit only after checking the operations.
Check your understanding: What would happen if you replaced COMMIT with ROLLBACK before the SELECT?
Show answer
The insert would be undone: the SELECT would return no rows because the table would be empty.
Common mix-ups with COMMIT
Committing an update without checking its scope
BEGIN;
UPDATE orders SET status = 'shipped';
COMMIT;The transaction does not add the missing WHERE. This sequence changes every order and then commits the result. Before running COMMIT, review the condition, the number of rows affected and a sample of the values.
Committing a complete unit of work
In a stock transfer, taking units out of the source warehouse and adding them to the destination warehouse form one unit of work. Committing after the subtraction and doing the addition in another transaction allows an incomplete intermediate state. Commit only after checking both operations and the rules that must hold, such as the source keeping a valid quantity.
Run the check on the same connection and transaction that made the writes; a query from another session may see a different version depending on the isolation level. Check the number of rows affected: an UPDATE that did not find the expected id can finish without an error and still fail to do what you meant to commit.
The commit itself can also fail, for example when the engine checks a deferred constraint. The application must handle that outcome and not report success before it arrives. After a successful commit, undoing it requires a new compensating operation or a planned recovery; a later ROLLBACK does not return to the earlier state. Keep transactions short and avoid waiting for user decisions while holding their locks.
Technical questions: SQLite: transactions (opens in a new tab).