Sub topic 13
Transactions and ACID Guarantees
Assume you know how tables, primary keys, foreign keys, and basic SQL work. This chapter explains how a database keeps a group of related changes safe when operations succeed, fail, or run at the same time.
Why transactions matter
A transaction is a group of database operations treated as one unit. For example, transferring money requires two changes:
- Subtract money from the sender.
- Add money to the receiver.
If the first change succeeds but the second fails, the data becomes incorrect. A transaction prevents the database from leaving only half of the transfer applied.
Most databases use these commands:
BEGINstarts a transaction.COMMITmakes its changes permanent.ROLLBACKcancels its changes.
Some database drivers use autocommit mode by default. In that mode, each individual statement is committed automatically unless you explicitly start a transaction.
CREATE TABLE accounts (
account_id INTEGER PRIMARY KEY,
owner TEXT NOT NULL,
balance DECIMAL(10, 2) NOT NULL CHECK (balance >= 0)
);
INSERT INTO accounts (account_id, owner, balance)
VALUES
(1, 'Ada', 500.00),
(2, 'Grace', 100.00);
BEGIN;
UPDATE accounts
SET balance = balance - 100.00
WHERE account_id = 1;
UPDATE accounts
SET balance = balance + 100.00
WHERE account_id = 2;
COMMIT;
SELECT account_id, owner, balance
FROM accounts
ORDER BY account_id;If an application detects an error before COMMIT, it should execute ROLLBACK. If the process or database connection fails before the commit completes, the database can discard the unfinished transaction.
The ACID properties
ACID is a set of guarantees that describe reliable transactions.
| Property | Meaning | Example |
|---|---|---|
| Atomicity | All transaction changes happen, or none happen | Both sides of a transfer are applied |
| Consistency | A committed transaction leaves data obeying its rules | A balance cannot become negative when a `CHECK` constraint forbids it |
| Isolation | Concurrent transactions do not see unsafe intermediate states | Another request does not read a transfer halfway through |
| Durability | Committed changes survive expected failures | A committed order remains after a restart |
Atomicity
Atomicity protects against partial work. A transaction can contain many statements, but other users should observe the result as one completed change. A failed transaction must be rolled back rather than committed.
Atomicity does not mean every operation automatically succeeds. A transaction can fail because of a constraint, a deadlock, a connection error, or an application decision.
Consistency
Consistency means the database moves from one valid state to another valid state. Constraints such as PRIMARY KEY, FOREIGN KEY, NOT NULL, UNIQUE, and CHECK enforce many rules.
The database cannot infer every business rule. For example, it may need application logic to ensure that a customer cannot place more than five active orders. The application and database must work together to preserve those rules.
Isolation
Isolation controls what concurrent transactions can observe. Stronger isolation reduces anomalies such as:
- A dirty read of data another transaction has not committed.
- A non-repeatable read where the same query returns different values.
- A phantom read where new matching rows appear during a transaction.
Isolation can reduce performance because transactions may lock rows or wait for one another. Database systems offer isolation levels with different trade-offs.
Durability
Durability means a successful commit is preserved by the database's recovery system. Databases usually write transaction records to durable storage before reporting success. Hardware failure, storage configuration, and backup policy still affect how much data an organization can recover.
Exercise
A transfer debits one account successfully, but the credit operation fails. Which ACID property requires the debit to be undone, and what command should the application use?
Show answer
Atomicity requires the related changes to be all-or-nothing. The application should issue ROLLBACK before ending the transaction.