Skip to main content

Transactions In SQL server

 SQL Server is a relational database management system developed by Microsoft. Transactions in SQL Server are a fundamental concept that ensures data consistency and integrity. A transaction is a logical unit of work that includes one or more SQL statements. In this blog, we will discuss transactions in SQL Server using T-SQL.

What is a Transaction?

A transaction is a set of one or more SQL statements that are executed as a single unit of work. The transaction is either committed or rolled back as a single entity. A committed transaction makes the changes to the database permanent, whereas a rolled-back transaction undoes all the changes made during the transaction.

The ACID properties define the characteristics of a transaction. The acronym ACID stands for Atomicity, Consistency, Isolation, and Durability.

Atomicity: A transaction is atomic, which means that it is an indivisible unit of work. Either all the statements within a transaction are executed, or none of them are executed. If any statement in the transaction fails, the entire transaction is rolled back.

Consistency: A transaction ensures the database is consistent before and after the transaction. The data in the database must satisfy all the constraints and rules defined on it.

Isolation: A transaction executes independently of other transactions. The changes made during the transaction are not visible to other transactions until the transaction is committed.

Durability: Once a transaction is committed, the changes made during the transaction become permanent and cannot be undone.

In any database management system, a transaction is a sequence of one or more database operations that are treated as a single logical unit of work. Transactions ensure that the data in the database remains consistent, accurate, and reliable. Transactions provide the ability to group multiple operations into a single atomic operation, which means either all the operations within a transaction are completed successfully, or none of them are.

In SQL Server, transactions are implemented using the Transact-SQL (T-SQL) language. In this blog, we will discuss how to use T-SQL to implement transactions in SQL Server.

The syntax of T-SQL transactions:

BEGIN TRANSACTION

-- SQL statements

COMMIT TRANSACTION -- or ROLLBACK TRANSACTION

 The BEGIN TRANSACTION statement begins a new transaction. All the SQL statements executed after this statement and before the COMMIT TRANSACTION or ROLLBACK TRANSACTION statement are part of the transaction. The COMMIT TRANSACTION statement commits the changes made to the database during the transaction, while the ROLLBACK TRANSACTION statement rolls back the changes made during the transaction and restores the database to its original state.

Let's look at an example of using transactions in T-SQL:

Suppose we have a table called "Customers" with the following structure:

CREATE TABLE Customers (

ID INT PRIMARY KEY,

Name VARCHAR(50),

Email VARCHAR(50),

Phone VARCHAR(15)

)

Now, let's say we want to update the phone number of a customer with a specific ID. We can use the following SQL statement:

UPDATE Customers SET Phone = '555-1234' WHERE ID = 1;

However, we also want to update the email address of the same customer. We can use another SQL statement:

UPDATE Customers SET Email = 'johndoe@email.com' WHERE ID = 1;

If we execute these two SQL statements without using a transaction, there is a chance that one of the statements might fail, leaving the database in an inconsistent state. For example, if the second statement fails, the customer's phone number will have been updated but not their email address.

To avoid this, we can use a transaction to group these two statements into a single atomic operation. Here's how we can do it using T-SQL:

BEGIN TRANSACTION

UPDATE Customers SET Phone = '555-1234' WHERE ID = 1;

UPDATE Customers SET Email = 'johndoe@email.com' WHERE ID = 1;

COMMIT TRANSACTION

 Now, if either of the two SQL statements fails, the entire transaction will be rolled back, and the database will be restored to its original state.

In conclusion, transactions are a critical feature of any database management system, and SQL Server provides powerful tools for working with transactions using T-SQL. By grouping multiple operations into a single atomic transaction, we can ensure that the database remains consistent, reliable, and accurate, even in the face of unexpected errors or failures.

Comments

Popular posts from this blog

SFTP Integration in SSIS package Using WinSCP DLL

  In this blog, I am planning to write about SSIS SFTP Task details and reference sites, upload and download the files from SFTP server using SSIS package with help of the winscp library. Steps to configure the winscp DLL and Download Files 1.        Please download the DLL and required files from below path https://winscp.net/eng/downloads.php#additional 2.        Once Downloaded, use the below comments to add in Local GAC "Path to the gacutil exe \gacutil.exe" /i WinSCPnet.dll 3.        Please use the below URL as guide to implement the SSIS script task to download the files from SFTP using winscp as library https://winscp.net/eng/docs/library_ssis 4.        I am just briefing the steps based on the above URL what we can try in SSIS package. 5.        Please create the below variables in SSIS package which we need ...

SQL Server - Tables

  What is Table?               Tables are database objects that contain all the data in a database. In tables, data is logically organized in a row-and-column format similar to a spreadsheet. Each row represents a unique record, and each column represents a field in the record.               There are different types of table available in SQL Server 1.        Partitioned Tables 2.        Temporal tables 3.        Wide Tables 4.        System Tables Please refer the below link to more about the Tables. https://learn.microsoft.com/en-us/sql/relational-databases/tables/tables?view=sql-server-ver16   Create a table in SQL Server There are multiple ways to create a table in SQL server. 1.    ...

SQL Server - Data Types

Data Types  In this post I have collected the information from micro soft site and keep all the same in a single page.  Please find the details    Data types are the heart of the SQL language, as developer or designer we need to see the every data as data type, how we can store the data in the database. In SQL Server, each column, local variable, expression, and parameter has a related data type. A data type is an attribute that specifies the type of data that the object can hold: integer data, character data, monetary data, date and time data, binary strings, and so on. In SQL Server, based on the characteristic data types grouped as below categories Exact Numerics Unicode character strings   Approximate Numerics Binary strings Date and time Other data types Character strings   ...