ACID Properties in SQL Server ensures Data Integrity during a transaction. The SQL ACID is an acronym for Atomicity, Consistency, Isolation, Durability.
- Atomicity : The atomicity acid property in Sql means, either all the operations (insert, update, delete) inside a transaction are takes place or none. Or you can simply say, all the statements (insert, update, delete) inside a transaction is either successfully completed or they were rolled back.
- Consistency : This SQL ACID property ensures the database consistency. It means, whatever happens in the middle of the transaction, this property will never leave your database in half-completed state.
- If the transaction is completed successfully then it will apply all the changes to the database.
- If there is any error in a transaction then all the changes that are already made will be rolled back automatically. It means, database will be restored 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 that are already made will automatically rolled back.
- Isolation : Every transaction is individual, and One transaction can’t access the result of other transaction until the transaction is completed. Or, you can’t perform same operation using multiple transaction at the same time. We will explain this SQL acid property in a separate article.
- Durability : Once the transaction is successfully 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 10 products (product key = 216) then update stock level to 4990 and insert new record in 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 Sales table to deliberately fail the insertion.
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, 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, 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 system will rollback the updates Otherwise, you can’t trace out the stock information.
Isolation in SQL Server ACID
One transaction can’t access the result of other transaction until the transaction is completed. For this Acid Property in SQL Server, it use 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 form the below screenshot, select statement is not returning any information. Because, we can’t access one transaction result with completing the transaction.
Let me execute the Rollback transaction. This will immediately show the result of the Select statement because lock is released from the Dim Prodcut table.
Thank You For Visiting Our Blog