ACID Properties in SQL Server ensure Data Integrity during a transaction. The 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 to the same. In this article, Let me define every ACID property in SQL Server:
- Atomicity: The atomicity property. 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 ensures database consistency. It means that whatever happens in the middle of the transaction, this will never leave your database in a half-completed state.
- If the transaction is 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 have already been made will be rolled back automatically. It means the database will restore to its state before the transaction starts.
- If there is a system failure in the middle of the transaction, all the changes already made will automatically roll back.
- Isolation: Every transaction is individual, and One transaction can’t access the result of other transactions until the transaction completes. Or, you can’t perform the same operation using multiple transactions at the same time. We will explain this in a separate article.
- Durability: Once the transaction is 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 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. The below screenshot will show you the data inside DimProduct table
and the data inside a sales table is:
For this demonstration, Whenever Sales happen, 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. Please refer to the Transaction and Nested Transactions articles.
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 insert the 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 updated the record, but we haven’t committed or rolled back the transaction.
- Second Instance: Use 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.