Transactions and ACID Properties in SQL
SQL transactions and ACID properties tutorial with examples for beginners
A transaction in SQL is a sequence of one or more SQL operations that are executed as a single logical unit of work. Transactions are crucial for maintaining data integrity, especially in systems where multiple users access or modify data at the same time.
In this beginner-friendly SQL tutorial, you will learn:
What a transaction is
Why transactions are important
SQL commands for transactions
What ACID properties are
Real-world examples
Best practices
A transaction is a group of operations that are treated as a single unit.
A transaction must follow this rule:
Either all operations succeed or none of them are applied.
Example:
Transfer money from Account A to B
Debit from A & credit to B must both succeed
Without transactions, concurrent operations could lead to race conditions, dirty reads, lost updates, or phantom data—issues that erode trust and cause financial or operational damage. In 2026, with microservices, real-time analytics, and AI-driven apps pushing concurrency to new heights, transactions remain essential for scalable, reliable data integrity.
SQL provides the following commands:
| Command | Description |
|---|---|
| BEGIN / START TRANSACTION | Start a new transaction |
| COMMIT | Save changes permanently |
| ROLLBACK | Undo changes |
| SAVEPOINT | Set intermediate checkpoints |
Start a new transaction.
START TRANSACTION;
Saves all changes.
COMMIT;
Reverts all changes since the last COMMIT.
ROLLBACK;
Creates a point inside a transaction to roll back to.
SAVEPOINT sp1;
Rollback to that point:
ROLLBACK TO sp1;
The classic real-world example that demonstrates why database transactions are essential is a simple bank money transfer—often called the "debit-credit" or "account-to-account transfer" scenario. This example perfectly illustrates how transactions prevent financial disasters by ensuring operations happen reliably together.
Imagine you're using a banking app to transfer $500 from your Checking Account (Account A) to your Savings Account (Account B). Without transactions, a failure could lead to money disappearing or being duplicated. With proper transactions, the system guarantees safety.
START TRANSACTION;
UPDATE accounts SET balance = balance - 500 WHERE id = 1;
UPDATE accounts SET balance = balance + 500 WHERE id = 2;
COMMIT;
If any step fails:
ROLLBACK;
START TRANSACTION;
UPDATE products SET stock = stock - 1 WHERE id = 10;
SAVEPOINT stock_updated;
UPDATE orders SET status = 'Confirmed' WHERE id = 201;
-- Error occurs here
ROLLBACK TO stock_updated;
COMMIT;
Transactions follow ACID properties to ensure reliability.
ACID stands for:
A – Atomicity
C – Consistency
I – Isolation
D – Durability
"All or nothing."
A transaction is fully completed or completely rolled back.
Example: Payment process—either money is debited & credited, or nothing happens.
The database must remain valid and follow all rules before and after the transaction.
Example:
Balance cannot go negative if constraints prevent it.
Multiple transactions running simultaneously should not interfere with each other.
Example:
Two users booking the same ticket should not conflict.
Once a transaction is committed, data must be permanently saved—even during power failure.
Example:
Bank transaction committed → money is safe.
SQL provides multiple isolation levels:
READ UNCOMMITTED
READ COMMITTED
REPEATABLE READ
SERIALIZABLE
These control how transaction interference is handled.
Database transactions are essential in scenarios where multiple related operations must succeed or fail together to maintain data integrity, prevent financial loss, or avoid business errors. The most common and critical examples include:
Banking system (money transfer)
Debit from one account + credit to another must be atomic. Partial failure (e.g., debit succeeds but credit fails) would cause money to disappear.
E-commerce order placement
Check stock availability → reserve/decrement inventory → create order → process payment. If payment fails, inventory must be restored to avoid overselling.
ATM withdrawals
Verify sufficient balance → deduct amount → dispense cash → update account. Transaction rollback prevents dispensing cash without debiting the account (or vice versa).
Inventory deduction after purchase
In warehouses/retail: decrement stock quantity only if the order is confirmed and paid. Prevents negative stock or selling items that no longer exist.
Ticket booking / reservation systems (flights, hotels, events, trains)
Check seat/availability → hold/reserve → process payment → confirm booking. Rollback frees the seat if payment fails, avoiding double-booking or orphaned holds.
In this SQL Transactions and ACID Properties tutorial, you learned:
What transactions are and why they matter
How COMMIT, ROLLBACK, and SAVEPOINT work
The four ACID properties
Real-world examples and best practices
Transactions ensure reliability, consistency, and safety in modern database systems.