What is a Transaction?

Transactions

A transaction is a group of SQL statements that must succeed or fail together. The classic example is a bank transfer: taking money from one account and adding it to another must both happen, or neither.

TCL (Transaction Control Language) commands control transactions: BEGIN/START TRANSACTION, COMMIT, ROLLBACK and SAVEPOINT.

Life of a Transaction

  1. BEGIN (or START TRANSACTION) starts it.
  2. Run one or more INSERT / UPDATE / DELETE statements. Other sessions do not see the changes yet.
  3. COMMIT makes all changes permanent, or ROLLBACK undoes all of them.

Note: Most databases run in auto-commit mode: every statement outside an explicit transaction is committed immediately. Oracle is the exception; there you must COMMIT yourself.