Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

ACID Properties in SQL Server

by suresh

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 acid 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

ACID Properties in SQL Server 1

and the data inside a sales table is:

ACID Properties in SQL Server 2

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. 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

OUTPUT

ACID Properties in SQL Server 3

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

ACID Properties in SQL Server 4

This time we will 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

OUTPUT

ACID Properties in SQL Server 5

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

ACID Properties in SQL Server 6

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 acid properties screenshot, the select statement is not returning any information. Because we can’t access one transaction result without completing the transaction.

ACID Properties in SQL Server 7

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.

ACID Properties in SQL Server 8

Hope you understood the ACID Properties in SQL Server.

Placed Under: SQL

  • Install SQL Server
  • Install SQL Management Studio
  • Uninstall Management Studio
  • Install AdventureWorks Database
  • SQL Management Studio Intro
  • Connect SQL with sqlcmd utility
  • SQL Attach Database
  • SQL Detach Database
  • SQL Restore Database
  • Restore Database using BAK
  • SQL Rename Database with Files
  • Get SQL Database Names
  • SQL Create Table
  • SQL Rename Table
  • SQL Alter Table
  • SQL Add Column
  • SQL Rename Column
  • Get SQL Table Names in a DB
  • Find SQL Table Dependencies
  • Rename SQL Table & Column
  • SQL Global & Local Temp Table
  • SQL Table Variable
  • SQL Derived Table
  • SQL DATALENGTH
  • SQL Data Types
  • DML, DDL, DCL & TCL Cmds
  • SQL Query Builder
  • SQL ALIAS
  • SQL SELECT Statement
  • SQL SELECT DISTINCT
  • SQL SELECT INTO Statement
  • SQL INSERT Statement
  • SQL INSERT INTO SELECT
  • SQL BULK INSERT or BCP
  • SQL UPDATE Statement
  • SQL UPDATE from SELECT
  • SQL DELETE Statement
  • SQL TRUNCATE Table
  • SQL CASE Statement
  • SQL MERGE Statement
  • SQL Subquery
  • SQL CTE
  • SQL PIVOT
  • SQL UNPIVOT
  • SQL Clauses Examples
  • SQL TOP Clause
  • SQL WHERE Clause
  • SQL ORDER BY Clause
  • SQL GROUP BY Clause
  • SQL HAVING Clause
  • SQL Primary Key
  • SQL Foreign Key
  • SQL Referential Integrity
  • SQL Check Constraint
  • SQL Unique Constraint
  • SQL Default Constraint
  • SQL Clustered Index
  • SQL Non Clustered Index
  • SQL Filtered Indexes
  • SQL COALESCE Function
  • SQL IS NOT NULL
  • SQL IS NULL Function
  • SQL ISNULL
  • SQL JOINS
  • SQL CROSS JOIN
  • SQL FULL JOIN
  • SQL SELF JOIN
  • SQL Outer Joins
  • SQL Cross Join Vs Inner Join
  • SQL LEFT JOIN
  • SQL RIGHT JOIN
  • SQL AND & OR Operators
  • SQL Arithmetic Operators
  • SQL BETWEEN Operator
  • SQL Comparison Operators
  • SQL LIKE
  • SQL EXCEPT
  • SQL EXISTS Operator
  • SQL NOT EXISTS Operator
  • SQL INTERSECT
  • SQL IN Operator
  • SQL NOT IN Operator
  • SQL UNION
  • SQL UNION ALL
  • SQL IF ELSE
  • SQL ELSE IF
  • SQL WHILE LOOP
  • SQL Nested While Loop
  • SQL BREAK Statement
  • SQL CONTINUE Statement
  • SQL GOTO Statement
  • SQL IIF Function
  • SQL CHOOSE Function
  • SQL Change Data Capture
  • SQL Table Partitioning
  • SQL Table Partition using SSMS
  • SQL TRY CATCH
  • SQL VIEWS
  • SQL User Defined Functions
  • SQL Alter User Defined Functions
  • SQL Stored Procedure Intro
  • Useful System Stored Procedures
  • SQL SELECT Stored Procedure
  • SQL INSERT Stored Procedure
  • SQL UPDATE Stored Procedure
  • Stored Procedure Return Values
  • Stored Procedure Output Params
  • Stored Procedure Input Params
  • Insert SP result into Temp Table
  • SQL Triggers Introduction
  • SQL AFTER INSERT Triggers
  • SQL AFTER UPDATE Triggers
  • SQL AFTER DELETE Triggers
  • SQL INSTEAD OF INSERT
  • SQL INSTEAD OF UPDATE
  • SQL INSTEAD OF DELETE
  • SQL STATIC CURSOR
  • SQL DYNAMIC CURSOR
  • SQL FORWARD_ONLY Cursor
  • SQL FAST_FORWARD CURSOR
  • SQL KEYSET CURSOR
  • SQL TRANSACTIONS
  • SQL Nested Transactions
  • SQL ACID Properties
  • Create SQL Windows Login
  • Create SQL Server Login
  • SQL Server Login Error
  • Create SQL Server Roles
  • SQL Maintenance Plan
  • Backup SQL Database
  • SQL Ranking Functions Intro
  • SQL RANK Function
  • SQL PERCENT_RANK Function
  • SQL DENSE_RANK Function
  • SQL NTILE Function
  • SQL ROW_NUMBER
  • SQL Aggregate Functions
  • SQL Date Functions
  • SQL Mathematical Functions
  • SQL String Functions
  • SQL CAST Function
  • SQL TRY CAST
  • SQL CONVERT
  • SQL TRY CONVERT
  • SQL PARSE Function
  • SQL TRY_PARSE Function
  • SQL Calculate Running Total
  • SQL Find Nth Highest Salary
  • SQL Reverse String
  • SQL FOR XML PATH
  • SQL FOR XML AUTO
  • SQL FOR XML RAW
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy