Skip to content

What is a transaction? ​

A transaction groups transactional table changes so that COMMIT retains them and ROLLBACK discards them. This chapter uses InnoDB on MySQL 8.4.11, with autocommit=1 and REPEATABLE READ unless a Scenario sets another level. These are table-write examples, not crash, network, or external-effect tests.

The MySQL 8.4 transaction manual documents these boundaries. Nontransactional tables such as MyISAM cannot have their writes undone by ROLLBACK. DDL can cause an implicit commit, so not every SQL operation belongs to the rollback boundary.

How to read the demos

Each Transcript and timeline comes from its linked Scenario. Demonstrated behavior means the schedule executes and asserts that result. A broader Documented contract needs the cited MySQL 8.4 manual; an Entailed guarantee is marked and explained. One schedule does not prove all schedules. See methodology.

Atomicity, demonstrated ​

A credits bob by 150, then attempts to debit alice, who has only 100. The CHECK error rolls back the failed debit statement. A's own SELECT still sees bob's credit. Only A's explicit ROLLBACK discards that earlier successful change. B's standalone consistent SELECTs see the original balances before and after rollback.

A
B
credit bob +150 (uncommitted)
read bob→ 50 (credit hidden)
debit alice −150← 3819 Check constraint 'accounts_chk_1' is violated.
ROLLBACK; bob's credit gone too
read→ alice 100, bob 50 (untouched)

A transfers 150 from alice to bob. Crediting bob works fine…

A> BEGIN;
Query OK

A> UPDATE accounts SET balance = balance + 150 WHERE owner = 'bob';
Query OK, 1 row affected

B> SELECT balance FROM accounts WHERE owner = 'bob'; -- B can't see A's uncommitted credit
 balance 
---------
      50 
(1 row)

…but debiting alice violates the CHECK constraint; she only has 100.

A> UPDATE accounts SET balance = balance - 150 WHERE owner = 'alice'; -- ER_CHECK_CONSTRAINT_VIOLATED
ERROR 3819 (HY000): Check constraint 'accounts_chk_1' is violated.

A's SELECT still sees bob's successful credit. The explicit ROLLBACK below discards it.

A> SELECT owner, balance FROM accounts ORDER BY id;
 owner | balance 
-------+---------
 alice |     100 
 bob   |     200 
(2 rows)

A> ROLLBACK;
Query OK

B> SELECT owner, balance FROM accounts ORDER BY id;
 owner | balance 
-------+---------
 alice |     100 
 bob   |      50 
(2 rows)

Verified against MySQL 8.4.11 · Run it yourself · Scenario source

The error did not itself undo the transaction. The next lesson distinguishes statement errors from errors that roll back the whole transaction. Visibility also depends on the reader's isolation level and operation; READ UNCOMMITTED can expose another transaction's uncommitted changes.

Further reading ​

MIT Licensed · Every transcript on this site was generated by a real database run against MySQL 8.4.11 and PostgreSQL 18.6 at 8fa27b8; shared YAML has an independent psycopg/PyMySQL check.