ACID Properties in SQL Server ensures Data Integrity during a transaction. The SQL ACID is an acronym for Atomicity, Consistency, Isolation, Durability.
In our previous article, we already explained about the Transaction and Nested Transactions. So, before these ACID Properties in SQL Server, I suggest you refer the same. In this article, Let me define every ACID property in SQL Server:
- Atomicity: The atomicity acid property in SQL. It means either all the operations (insert, update, delete) inside a transaction take place or none. Or you can say, all the statements (insert, update, delete) inside a transaction are either completed or rolled back.
- Consistency: This SQL ACID property ensures database consistency. It means, whatever happens in the middle of the transaction, this property will never leave your database in a half-completed state.
- If the transaction completed successfully, then it will apply all the changes to the database.
- If there is an error in a transaction, then all the changes that already made will be rolled back automatically. It means the database will restore to its state that it had before the transaction started.
- If there is a system failure in the middle of the transaction, then also, all the changes made already will automatically rollback.
- Isolation: Every transaction is individual, and One transaction can’t access the result of other transactions until the transaction completed. Or, you can’t perform the same operation using multiple transactions at the same time. We will explain this SQL acid property in a separate article.
- Durability: Once the transaction completed, then the changes it has made to the database will be permanent. Even if there is a system failure, or any abnormal changes also, this SQL acid property will safeguard the committed data.
ACID Properties in SQL Server Example
We are going to use Dim products and Sales table to explain the Sql Server ACID properties. Below screenshot will show you the data inside DimProduct table
and the data inside a sales table is:
For this SQL Acid properties demonstration, Whenever the Sales happens, then we have to update the Stock Level based on the order Quantity. For example, if A orders ten products (product key = 216), then update the stock level to 4990 and insert a new record in the sales table.
Atomicity in SQL ACID
It means all the statements inside a transaction should either succeed or fail as a unit. To demonstrate this SQL Atomicity Acid property, we are using the one UPDATE and an INSERT statement inside a transaction.
USE [SQLTEST] GO BEGIN TRANSACTION UPDATE [DimProduct] SET [StockLevel] = 4700 WHERE [ProductKey] = 213 INSERT INTO [Sales] ([ProductKey], [OrderQuantity], [UnitPrice], [SalesAmount]) VALUES (213, 300, 48.0673, 48.0673 * 300) COMMIT TRANSACTION
Let me show you the records in DimProduct,and Sales tables after that transaction.
This time we will use insert wrong information in the Sales table to fail the insertion deliberately.
USE [SQLTEST] GO BEGIN TRANSACTION UPDATE [DimProduct] SET [StockLevel] = 4700 WHERE [ProductKey] = 213 INSERT INTO [Sales] ([ProductKey], [OrderQuantity], [UnitPrice], [SalesAmount]) VALUES (213, 300, 48.0673, 'Hey! This is Wrong') COMMIT TRANSACTION
Let me show you the records in Dim Product and Sales tables after that transaction. As you can see from the above screenshot, a committed row (Update Statement) had rolled back.
Consistency in SQL Server ACID
Let me take the above example to explain this SQL ACID property. Say, the transaction has updated the stock with new data, and suddenly there is a system failure (right before the insertion into sales or in the middle). In this situation, the system will roll back the updates. Otherwise, you can’t trace the stock information.
Isolation in SQL Server ACID
One transaction can’t access the result of other transactions until the transaction completes. For this Acid Property in SQL Server, it uses Locks to lock the table. As you can see, we are using two separate instances :
- First Instance: we started the transaction and updating the record, but we haven’t committed or rolled back the transaction.
- Second Instance: Using the Select statement to select the records present in the Dim Product table.
As you can see from the below screenshot, the select statement is not returning any information because we can’t access one transaction result without completing the transaction.
Let me execute the Rollback transaction. It will immediately show the result of the Select statement because the lock released from the Dim Product table.
Hope you understood the ACID Properties in SQL Server