ACID Transactions
In SQL databases, a transaction
is a sequence of operations that should be treated as a single unit.
To maintain data reliability and integrity, every transaction follows four core principles, collectively known as ACID
.
What does ACID stand for?
ACID
stands for the four core principles:
1. Atomicity
- All operations in a transaction succeed or fail together.
- If one part fails, the entire transaction is rolled back.
2. Consistency
- The database transitions from one valid state to another.
- It enforces all data rules and constraints, even if multiple changes happen at once.
3. Isolation
- Transactions don't interfere with each other.
- Until a transaction is complete, its intermediate steps are invisible to others.
4. Durability
- Once a transaction is committed, the changes are permanent.
- They won't be lost even if the system crashes immediately afterward.
Example: Transfer money
Imagine users can transfer money to each other.
accounts
user_id | name | balance |
---|---|---|
1 | Alice | 500 |
2 | Bob | 300 |
To move 100 coins from Alice to Bob safely, you can use a transaction as follows:
ACID-compliant transfer
BEGIN; UPDATE accounts SET balance = balance - 100 WHERE name = 'Alice'; UPDATE accounts SET balance = balance + 100 WHERE name = 'Bob'; COMMIT;
- If both updates succeed → changes are saved.
- If either fails → you can roll back with
ROLLBACK
.
Why it matters
Without ACID guarantees, you risk:
- Users losing money during a failed transfer
- Inconsistent or corrupted data
- Race conditions when multiple users interact with the same data
ACID ensures your application behaves safely and predictably.
Quiz
0 / 1
In SQL databases, the 'Durability' property of ACID transactions ensures that once a transaction is committed, it can be lost if the system crashes immediately afterward.
True
False
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Code Editor
Run
Generate
Tables
Execution Result