Transactions group SQL statements into one atomic unit. Use BEGIN, your statements, then COMMIT to save or ROLLBACK to undo everything. This ensures data stays consistent even if an error occurs mid-operation.
Transactions
BEGIN;
UPDATE accounts SET balance = balance - 500 WHERE id = 1;
UPDATE accounts SET balance = balance + 500 WHERE id = 2;
COMMIT;
-- Rollback on error
BEGIN;
DELETE FROM orders WHERE id = 99;
ROLLBACK; -- undo everything
Transactions guarantee all-or-nothing execution — critical for financial operations.