Introduction
In the world of databases, reliability and consistency are of utmost importance. When dealing with critical and sensitive data, organizations must ensure that their database systems maintain data integrity, even in the face of failures or concurrent transactions. This is where the concept of ACID properties comes into play.

ACID
ACID stands for Atomicity, Consistency, Isolation, and Durability, and it represents a set of principles that ensure the reliability and robustness of transactions in a database management system, such as SQL Server. In this article, we'll explore each of the ACID properties with examples to better understand their significance.
Atomicity
Atomicity refers to the indivisibility or all-or-nothing nature of a transaction. It means that a transaction should be treated as a single unit of work, and either all the operations within the transaction are executed successfully or none of them are. If any part of the transaction fails, the entire transaction is rolled back, and the database returns to its original state.
To better understand atomicity, let's consider an example.
Suppose we have a database with two tables: "Customers" and "Orders." The "Customers" table contains information about the customers, and the "Orders" table contains information about orders placed by those customers. We want to update both tables in a single transaction.
Let's say we want to perform the following two operations within a transaction:
- Insert a new customer into the "Customers" table.
- Insert a new order for that customer into the "Orders" table.
The SQL code for the transaction might look like this.
BEGIN TRANSACTION;
-- Step 1: Insert a new customer into the "Customers" table
INSERT INTO Customers (CustomerID, FirstName, LastName, Email)
VALUES ('C001', 'Amit', 'Mohanty', '[email protected]');
-- Step 2: Insert a new order for the customer into the "Orders" table
INSERT INTO Orders (OrderID, CustomerID, OrderDate, TotalAmount)
VALUES ('O001', 'C001', '2023-07-21', 100.00);
COMMIT TRANSACTION;
In this example, the transaction is started using the BEGIN TRANSACTION; statement. Within the transaction, we perform two operations: inserting a new customer and inserting a new order for that customer. Once both operations have been executed without any errors, we commit the transaction using COMMIT TRANSACTION.
Now, suppose an error occurs after inserting the new customer (Step 1), and the second operation (Step 2) is not executed. In this case, because of atomicity, the transaction will be rolled back, and no changes will be made to the database. This ensures that the database remains in a consistent state.
Atomicity guarantees that either all the steps within the transaction are successful or the database remains unchanged. There is no partial completion of the transaction, which helps maintain data integrity and consistency in the database.
Consistency
The consistency property ensures that a database transitions from one valid state to another valid state after a transaction is executed. It means that a transaction must maintain the integrity of the data and not violate any defined rules or constraints.
To explain consistency, let's consider an example involving a simple banking database. We have two tables: "Accounts" to store account information and "Transactions" to store transaction records.
The "Accounts" table structure.
border="1" cellspacing="1" width="100%"> AccountNumber AccountHolder Balance 101 Amit 1000.00 102 Deepak 1500.00 103 Swarup 2000.00
The "Transactions" table structure.
| TransactionID | AccountNumber | Amount | TransactionType | TransactionDate |
| 1 | 101 | -200.00 | Withdrawal | 2023-07-15 10:00:00.000 |
| 2 | 102 | 300.00 | Deposit | 2023-07-15 10:30:00.000 |
| 3 | 103 | -100.00 | Withdrawal | 2023-07-15 11:00:00.000 |
Let's say we have two transactions happening concurrently.
Transaction 1
BEGIN TRANSACTION;
UPDATE Accounts SET Balance = Balance - 100 WHERE AccountNumber = 101;
INSERT INTO Transactions (AccountNumber, Amount, TransactionType, TransactionDate)
VALUES (101, -100, 'Withdrawal', GETDATE());
COMMIT;
Transaction 2
BEGIN TRANSACTION;
UPDATE Accounts SET Balance = Balance + 150 WHERE AccountNumber = 102;
INSERT INTO Transactions (AccountNumber, Amount, TransactionType, TransactionDate)
VALUES (102, 150, 'Deposit', GETDATE());
COMMIT;
These transactions are trying to withdraw 100 from Amit's account and deposit 150 to Deepak's account concurrently.
Now, let's discuss different consistency levels.
- Read Uncommitted: This level allows dirty reads, meaning a transaction can read uncommitted changes made by other transactions.
Example-- Session 1 BEGIN TRANSACTION UPDATE Employees SET Salary = Salary + 1000 WHERE Department = 'IT' -- Session 2 SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED SELECT SUM(Salary) FROM Employees WHERE Department = 'IT' -- The result may include the uncommitted changes made by Session 1 - Read Committed: This level prevents dirty reads. A transaction can only read committed changes made by other transactions.
Example-- Session 1 BEGIN TRANSACTION UPDATE Employees SET Salary = Salary + 1000 WHERE Department = 'HR' -- Session 2 SET TRANSACTION ISOLATION LEVEL READ COMMITTED SELECT SUM(Salary) FROM Employees WHERE Department = 'HR' -- The result will not include the uncommitted changes made by Session 1 - Repeatable Read: This level ensures that if a transaction reads a certain set of data, it will get the same data if it reads again during the same transaction. However, it allows non-repeatable reads.
Example-- Session 1 SET TRANSACTION ISOLATION LEVEL REPEATABLE READ BEGIN TRANSACTION SELECT SUM(Salary) FROM Employees WHERE Department = 'Finance' -- Session 2 -- Inserts new rows into the Finance department INSERT INTO Employees (Name, Department, Salary) VALUES ('Abhilash Mohanty', 'Finance', 5000) -- The SELECT statement in Session 1 may return a different result in subsequent reads - Serializable: This level provides the highest consistency. It ensures that a transaction cannot read or modify data that has been read or modified by another transaction until the first transaction is completed.
Example-- Session 1 SET TRANSACTION ISOLATION LEVEL SERIALIZABLE BEGIN TRANSACTION SELECT SUM(Salary) FROM Employees WHERE Department = 'Sales' -- Session 2 -- Inserts new rows into the Sales department INSERT INTO Employees (Name, Department, Salary) VALUES ('Deepak Mohapatra', 'Sales', 4500) -- Session 2 will be blocked until Session 1 completes its transaction

Join the conversation! Your thoughts help the community grow.