๐ง Introduction
In relational databases, ACID stands for:
Atomicity
Consistency
Isolation
Durability
These four properties guarantee reliable transactions โ ensuring that your data remains accurate even in case of errors or crashes.
In this tutorial, weโll demonstrate each ACID property step by step using SQL commands.
๐๏ธ Step 1: Create a Sample Table
Weโll create an Accounts table and insert sample records.
CREATE TABLE Accounts (
acc_no INT PRIMARY KEY,
name VARCHAR(50),
balance INT CHECK (balance >= 0)
);
๐ธ Screenshot: Accounts table created successfully.
๐งฎ Insert Sample Data
INSERT INTO Accounts VALUES
(101, 'Arun Kumar', 50000),
(102, 'Meena Devi', 30000),
(103, 'Vishal', 70000);
๐ธ Screenshot: Data inserted successfully.
โ Current Data:
acc_no name balance
101 Arun Kumar 50000
102 Meena Devi 30000
103 Vishal 70000
โ๏ธ A โ Atomicity
A transaction is all or nothing.
Either all steps succeed, or none are applied.
๐ฏ Example: Money Transfer (with Rollback)
BEGIN TRANSACTION;
UPDATE Accounts SET balance = balance - 5000 WHERE acc_no = 101;
UPDATE Accounts SET balance = balance + 5000 WHERE acc_no = 102;
-- Simulate an error
ROLLBACK;
SELECT * FROM Accounts;
๐ Explanation:
We start a transaction.
We try to transfer โน5000 from Arun to Meena.
Then we issue a ROLLBACK, which undoes both updates.
โ
Expected Result:
Balances remain unchanged.
๐งฉ C โ Consistency
The database must always move from one valid state to another.
Invalid data (e.g., negative balances) should not be allowed.
๐ฏ Example: Insert Invalid Record
INSERT INTO Accounts VALUES (104, 'Karthik', -2000);
โ Expected Output:
๐ Explanation:
The CHECK (balance >= 0) constraint ensures consistency of data.
You can never have an account with a negative balance.
๐ I โ Isolation
Multiple transactions running at the same time should not interfere with each other.
๐ฏ Example: Two Sessions
๐ช Session 1:
BEGIN TRANSACTION;
UPDATE Accounts SET balance = balance - 10000 WHERE acc_no = 103;
-- (Donโt commit yet)
๐ช Session 2:
SELECT * FROM Accounts WHERE acc_no = 103;
โ Observation:
If your isolation level is READ COMMITTED,
Session 2 wonโt see the updated balance until Session 1 commits.
Once Session 1 executes COMMIT;, Session 2 will see the new balance.
๐ Explanation:
This demonstrates Isolation โ one transactionโs changes remain invisible to others until committed.
๐ฝ D โ Durability
Once a transaction is committed, it remains permanent โ even if the system crashes.
๐ฏ Example: Commit and Restart
BEGIN TRANSACTION;
UPDATE Accounts SET balance = balance + 2000 WHERE acc_no = 101;
COMMIT;
โ Now restart your database service, then run:
SELECT * FROM Accounts WHERE acc_no = 101;
๐ Expected Output:
The updated balance remains in place even after the restart.
โ This demonstrates Durability โ committed data survives crashes or restarts.
๐ Conclusion
Youโve just implemented and tested all four ACID properties in SQL:
โ
Atomicity โ Rollback works perfectly
โ
Consistency โ Data validation enforced
โ
Isolation โ Parallel sessions protected
โ
Durability โ Changes survive restarts
With this, youโve mastered one of the most important foundations of Database Reliability! ๐ง ๐พ
๐ฌ Next Step:
Try experimenting with different isolation levels using:
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
and observe how concurrent transactions behave differently!
Top comments (0)