DEV Community

Iniya Iniya
Iniya Iniya

Posted on

Understanding ACID Properties in SQL with Practical Examples

๐Ÿง  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)