When it comes to relational databases, the ACID principles form the bedrock of data reliability. They guarantee that every transaction behaves predictably, maintaining stability and trust in the system even when errors occur.
ACID stands for:
- Atomicity 🧩 – Either all operations occur or none do
- Consistency ⚖️ – Rules and constraints always stay valid
- Isolation 🚧 – Parallel transactions don’t interfere
- Durability 💪 – Once committed, data stays safe forever
We’ll demonstrate these concepts step by step using an Accounts table and simple SQL transactions.
⭐Step 1: Create the Accounts Table and Add Data
To begin, we’ll create a table named Accounts with a primary key and a check constraint to ensure valid balances.
🧑💻 CODE:
CREATE TABLE Accounts (
acc_no INT PRIMARY KEY,
name VARCHAR(50),
balance INT CHECK (balance >= 0));
INSERT INTO Accounts (acc_no, name, balance) VALUES
(101, 'Calindra', 5800),
(102, 'Thalorin', 4200),
(103, 'Veylith', 6900);
SELECT * FROM Accounts;
Explanation:
- acc_no serves as the unique identifier for each account 🔑
- The CHECK constraint blocks any record with a negative balance ❌
- The three sample entries give us a foundation to explore ACID behavior ✅
⭐Step 2: Atomicity 🧩 – All or Nothing
Objective: Ensure that no partial updates occur if a transaction fails midway.
Scenario: Transfer ₹1500 from Calindra to Thalorin, but simulate an interruption before completion.
🧑💻 CODE:
START TRANSACTION;
UPDATE Accounts SET balance = balance - 1500 WHERE acc_no = 101;
UPDATE Accounts SET balance = balance + 1500 WHERE acc_no = 102;
ROLLBACK;
SELECT * FROM Accounts;
Observation:
- After executing ROLLBACK, all account balances remain unchanged 🔄.
- This proves Atomicity, where incomplete or failed operations are completely undone — a must for financial systems💳.
⭐Step 3: Consistency ⚖️ – Data Integrity Always
Goal: Maintain valid data according to defined rules and constraints.
Scenario: Attempt to insert a record with a negative balance.
🧑💻 CODE:
INSERT INTO Accounts (acc_no, name, balance) VALUES (104, 'Elarion', -2000);
Result:The system rejects the insertion immediately ❌
Explanation:
- The CHECK constraint enforces that balances cannot be negative, ensuring the database remains valid.
- Consistency ensures that all business logic and schema rules remain intact — no matter what the transaction tries to do.
💡 Tip: Use constraints, validations, and triggers to ensure that the database always holds meaningful, error-free data.
⭐Step 4: Isolation 🚧 – Transactions Stay Independent
Goal: Ensure that one transaction doesn’t affect another running at the same time.
Scenario: Run two sessions — one updates an account, the other tries to read it before the update commits.
🧑💻 CODE:
START TRANSACTION;
UPDATE Accounts SET balance = balance - 700 WHERE acc_no = 101;
SELECT balance FROM Accounts WHERE acc_no = 101;
COMMIT;
Explanation:
Session 1: Begins the update but doesn’t commit immediately.
Session 2: Reads the balance — it still sees the old value until the first session commits.
Once Session 1 commits, the new balance becomes visible ✅.
💡 Note: Isolation levels like READ COMMITTED, REPEATABLE READ, and SERIALIZABLE determine how strictly this behavior is enforced in concurrent environments.
⭐Step 5: Durability 💪 – Committed Data Stays
Goal: Make sure that once a transaction is committed, the data remains safe even after system failures or restarts.
Scenario: Increase Veylith’s balance by ₹2500 and commit.
🧑💻 CODE:
START TRANSACTION;
UPDATE Accounts SET balance = balance + 2500 WHERE acc_no = 103;
COMMIT;
SELECT acc_no, name, balance FROM Accounts WHERE acc_no = 103;
Observation:
- After restarting the database, the committed change persists 💾.
- That’s Durability — once the system acknowledges a commit, it’s written permanently, usually ensured by write-ahead logs (WAL) and disk persistence mechanisms.
💡 Final Thoughts
Through this hands-on walkthrough, we demonstrated how ACID properties safeguard the integrity and reliability of databases:
- Atomicity: Transactions fully succeed or completely roll back
- Consistency: Data always follows the defined rules
- Isolation: Transactions run without disturbing one another
- Durability: Committed data survives any crash or restart
By mastering these core principles, developers can design systems that are robust, predictable, and secure, whether in banking 💳, healthcare 🏥, or e-commerce 🛒.
✅ Remember: ACID isn’t just a theory — it’s what makes databases trustworthy and dependable at scale.
Top comments (0)