📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PostgreSQL Essentials Transactions

Transactions

6 min read Quiz at the end
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.

Topic Quiz · 5 questions

Test your understanding before moving on

1. Which command saves a transaction?
💡 COMMIT permanently saves all changes made in a transaction.
2. Which command undoes a transaction?
💡 ROLLBACK undoes all changes made since BEGIN.
3. What does BEGIN do?
💡 BEGIN starts a new transaction block.
4. What is a SAVEPOINT?
💡 SAVEPOINT creates a named point within a transaction that you can roll back to.
5. What does "atomic" mean in ACID?
💡 Atomic means a transaction either fully completes or has no effect.