Skip to content

Transaction

In one line

A transaction is a sequence of database operations that are treated as one unit — either all succeed or all are rolled back.

In simple words

A transaction is a group of database operations that are executed as a single unit of work. The key property is "all or nothing" — if any operation in the transaction fails, the entire transaction is rolled back and the database is unchanged. If all operations succeed, the transaction is committed and the changes become permanent.

Transactions are essential when multiple operations must happen together. For example, a bank transfer: deduct from account A and add to account B. If the second operation fails, the first must be undone — otherwise money disappears.

In SQL, you start a transaction with BEGIN, make changes, then either COMMIT (save) or ROLLBACK (undo). Transactions follow the ACID properties: Atomicity, Consistency, Isolation, Durability.

Example

sql
BEGIN;

-- Transfer $100 from account 1 to account 2
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;

-- If both succeed, save the changes
COMMIT;
-- If anything fails: ROLLBACK;

Both updates are in one transaction — either both happen or neither does.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary