What is a transaction, and what does ACID mean?
IntermediateConceptMySQL
Answer
Short answer
A transaction is a group of SQL statements that succeed or fail as one unit. ACID describes the guarantees: Atomicity (all or nothing), Consistency (rules and constraints always hold), Isolation (concurrent transactions don't see each other's half-finished work) and Durability (once committed, the data survives crashes).
Example: a money transfer
START TRANSACTION;
UPDATE accounts SET balance = balance - 500 WHERE id = 1;
UPDATE accounts SET balance = balance + 500 WHERE id = 2;
COMMIT; -- both changes saved together
-- if anything fails before COMMIT: ROLLBACK; → neither change happens
ACID in one line each
| Letter | Meaning | Without it… |
|---|---|---|
| Atomicity | All statements or none | Money leaves one account and never arrives |
| Consistency | Constraints always valid | Orders for customers that don't exist |
| Isolation | Concurrent work doesn't interfere | Two buyers get the last item |
| Durability | Committed = safe on disk | A crash loses confirmed orders |
MySQL specifics
- InnoDB (the default engine) supports transactions; the old MyISAM engine doesn't.
- Default isolation level: REPEATABLE READ.
- Durability is controlled by
innodb_flush_log_at_trx_commit=1(safest).
DevOps tip
Wrap manual production changes in a transaction: run it, check with a SELECT, then COMMIT or ROLLBACK.
What to say in the interview
"A transaction is an all-or-nothing unit of work. ACID means atomic, consistent, isolated and durable. InnoDB provides it with REPEATABLE READ as the default isolation level, and I wrap manual prod changes in a transaction so I can roll back."
More on MySQL
How do you set up MySQL and log in to it?BeginnerHow do you create a database, a table and see data in MySQL?BeginnerHow do you create a MySQL user for an application and give it permissions?BeginnerHow do you back up and restore a MySQL database?BeginnerWhat is a relational database?BeginnerWhat is the difference between a database, a table, a row and a column?Beginner