Transactions
Transactions commit or roll back a set of writes.
What it is
Transactions commit or roll back a set of writes.
When to use it
Keep local multi-row operations atomic.
Syntax
BEGIN;
-- writes
COMMIT;
-- On failure: ROLLBACK;Example
BEGIN;
UPDATE orders SET status = 'paid' WHERE id = 42 AND status = 'pending';
INSERT INTO audit_logs (actor_id, action, subject_id, created_at)
VALUES (7, 'order.paid', 42, CURRENT_TIMESTAMP);
COMMIT;
-- Application must check affected rows and ROLLBACK on any failure.Common mistakes
A script does not automatically roll back every statement error; external systems are outside the transaction.