TCL
TCL
Definition
TCL (Transaction Control Language) is the family of SQL statements that manage transactions — groups of one or more DML statements that must succeed or fail together, as a single atomic unit. The core TCL verbs are BEGIN/START TRANSACTION, COMMIT, ROLLBACK, and SAVEPOINT.
How It Works
sqlBEGIN; UPDATE accounts SET balance = balance - 500 WHERE id = 1; -- debit UPDATE accounts SET balance = balance + 500 WHERE id = 2; -- credit COMMIT;
This is the textbook "bank transfer" example: if only the debit succeeded and the credit failed (a crash, a constraint violation, anything), money would simply vanish unless both statements are wrapped in one transaction. COMMIT makes all changes since BEGIN permanent; ROLLBACK undoes all of them, as if none had ever run. A SAVEPOINT marks an intermediate point inside a longer transaction that you can roll back to, without discarding the entire transaction.
This "all or nothing" property is part of the ACID guarantees (Atomicity, Consistency, Isolation, Durability) that relational databases provide — TCL is specifically how atomicity is expressed and controlled at the SQL level.
Edge Cases and Pitfalls
- Forgetting to
COMMITa transaction leaves changes pending — visible to that same session, but (depending on isolation level) not necessarily to other sessions, and vulnerable to being lost entirely if the connection drops before a commit happens. - Most database clients/drivers default to auto-commit mode, where every individual statement is implicitly wrapped in its own commit — an explicit
BEGINis what turns multiple statements into one atomic unit instead of several independent ones. - A
ROLLBACKafter aDDLstatement may or may not undo that DDL, depending on the engine (see DDL) — assuming TCL's rollback guarantee extends uniformly to every statement type is a portability trap. - Long-running open transactions can hold locks and block other work, even if the transaction itself is just sitting idle waiting for the application to decide to commit or rollback — "forgetting" to close a transaction is a real operational hazard, not just a style issue.
Key Takeaways
- TCL = BEGIN / COMMIT / ROLLBACK / SAVEPOINT — groups DML into all-or-nothing units.
- COMMIT makes changes permanent; ROLLBACK discards everything since the transaction started (or since a SAVEPOINT).
- TCL is how SQL expresses the "Atomicity" in ACID — the classic bank-transfer example is the canonical illustration of why it matters.