How transactions protect changes

Start a transaction

A transaction creates one logical unit of work. In SQLite, BEGIN starts it.

BEGIN;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
UPDATE accounts SET balance = balance + 50 WHERE id = 2;

Commit success

COMMIT makes the transaction's changes permanent and visible according to the database's isolation rules.

COMMIT;

Roll back mistakes

ROLLBACK cancels uncommitted changes. It cannot undo work that has already been committed.

ROLLBACK;

Handle errors in applications

Database libraries often start or manage transactions differently. On any failure, roll back and report the error. PostgreSQL/MySQL support richer isolation and locking options; SQLite coordinates writes at the database-file level.