ACID Properties in SQL Server

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, 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 can’t access the result of another 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 the 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 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 one UPDATE and an INSERT statement inside a transaction. Please refer to the Transaction and Nested concept.

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

When you execute the above UPDATE query, the following Message will display.

Messages
-------------
(1 row(s) affected)
(1 row(s) affected)

Let me show you the records in DimProduct, and Sales tables after that transaction.

Atomicity ACID Property Example

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

Here, the UPDATE statement has no issue, whereas the INSERT statement is trying to load a text message in SalesAmount. So, the first one executes and the second one throws the following error.

Messages
-------------
(1 row(s) affected)

Msg 235, Level 16, State 0, Line 9
Cannot convert a char value to money. The char value has incorrect syntax.

Let me show you the records in the Dim Product and Sales tables after that transaction. As you can see from the above screenshot, a committed row (Update Statement) had rolled back.

SQL ACID Properties TRANSACTION

SQL Consistency ACID Property

Let me take the above example to explain this 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 ACID

One transaction can’t access the result of another 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 it.
  • Second Instance: Use the Select statement to select the records present in the Dim Product table.

As you can see from the screenshot below, the select statement is not returning any information. Because we can’t access one transaction result without completing the other.

ISOLATION ACID Properties Example

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.

ACID Properties ROLLBACK
Categories SQL

Comments are closed.