Transactions
DraftA transaction groups statements into one unit of work. You either commit the whole change or roll it back.
Commit a transfer
COMMIT makes every statement in the transaction durable together.
Loaded Database:transactions.database-init
BEGIN;
UPDATE accounts SET balance_cents = balance_cents - 1000 WHERE name = 'Ada';
UPDATE accounts SET balance_cents = balance_cents + 1000 WHERE name = 'Grace';
COMMIT;
SELECT name, balance_cents FROM accounts ORDER BY name;| name | balance_cents |
|---|---|
| Ada | 4000 |
| Grace | 4000 |
Unavailable for a multi-statement SQL example.Rollback a transfer
ROLLBACK throws away the changes made inside the transaction.
Loaded Database:transactions.database-init
BEGIN;
UPDATE accounts SET balance_cents = balance_cents - 1000 WHERE name = 'Ada';
UPDATE accounts SET balance_cents = balance_cents + 1000 WHERE name = 'Grace';
ROLLBACK;
SELECT name, balance_cents FROM accounts ORDER BY name;| name | balance_cents |
|---|---|
| Ada | 5000 |
| Grace | 3000 |
Unavailable for a multi-statement SQL example.