SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. Define Transaction in DBMS

Text copied!

« Previous
Next »

Define Transaction in DBMS

July 9, 2023, 10:57 p.m. under SQL

  • Hi! You should check SQL WITH TIES post first.

    In DBMS, a transaction is a way to execute multiple statements together. When you group statements into a transaction, they either all succeed or all fail. If everything goes smoothly, the changes made by the transaction are saved. However, if any part of the transaction encounters an error, all changes are undone or "rolled back."

    Consider a simple scenario where you're transferring money from one bank account to another. You want both the deduction from one account and the addition to the other to happen together. If either step fails (like insufficient funds), you want to cancel the whole transaction.

    Here's a simplified syntax for SQL Server transaction :
    BEGIN TRANSACTION; -- Start the transaction
    
    
    -- SQL statements to perform within the transaction
    
    
    ROLLBACK TRANSACTION; -- If any error occurs, rollback the transaction
    
    COMMIT TRANSACTION; -- If everything goes well, commit the transaction
            
    BEGIN TRANSACTION :

    BEGIN TRANSACTION marks the start of a transaction. You can define it as "BEGIN TRANSACTION," "BEGIN TRAN," or "BEGIN TRANSACTION [Transaction_Name]" to initiate a transaction.

    ROLLBACK TRANSACTION :

    ROLLBACK TRANSACTION undoes the changes made in the transaction if an error occurs. You can define it as "ROLLBACK TRANSACTION," "ROLLBACK TRAN," or "ROLLBACK TRANSACTION [Transaction_Name]" to rollback a transaction.

    COMMIT TRANSACTION :

    COMMIT TRANSACTION saves the changes made in the transaction to the database if everything was successful. You can define it as "COMMIT TRANSACTION," "COMMIT TRAN," or "COMMIT TRANSACTION [Transaction_Name]" to rollback a transaction.

    Here's a simple example of a transaction in SQL Server :-
    This is an example where all the queries within the transaction run without any errors :

    1. Let's consider a table named '[Product]' containing the following values :

    Define Transaction in DBMS

    2. Now, let's examine the ROLLBACK TRANSACTION query below, which rolls back changes whether the queries within the transaction succeeded or failed. Run below transaction statement :

    BEGIN TRANSACTION ABC
    
    INSERT INTO [dbo].[Product] (
    	    [Product_Id], [Name], [GroupName], [ModifiedDate] )
            VALUES 
    		(2, 'Soap', 'Group-B', '2023-01-01 00:00:00.000');
    
    UPDATE [dbo].[Product]
    	SET [GroupName] = 'Sales'
    	WHERE [Product_Id] = 1;
    
    ROLLBACK TRANSACTION ABC
            
    Define Transaction in DBMS

    3. In this example, we've named our transaction 'ABC' and included two SQL statements within it. We're demonstrating the rollback query, which undoes any changes made, regardless of whether the queries within the transaction succeeded or failed. Rollback always reverts all changes back to its original state.

    Check the output below, both insert and update queries within the transaction command are successful and therefore, no changes are made to the product table. To apply any changes defined within the transaction, we must commit.

    Define Transaction in DBMS

    4. Now, let's examine the COMMIT TRANSACTION query below, which commits changes only if all the queries within the transaction are successful. Run below transaction statement :

    BEGIN TRANSACTION ABC
    
    INSERT INTO [dbo].[Product] (
    	    [Product_Id], [Name], [GroupName], [ModifiedDate] )
            VALUES 
    		(2, 'Soap', 'Group-B', '2023-01-01 00:00:00.000');
    
    UPDATE [dbo].[Product]
    	SET [GroupName] = 'Sales'
    	WHERE [Product_Id] = 1;
    
    COMMIT TRANSACTION ABC
            
    Define Transaction in DBMS

    5. In this example, we've named our transaction 'ABC' and included two SQL statements within it. We're demonstrating the commit query, It applies changes only if all the queries within the transaction are successful.

    Look at the results below. Both insert and update queries within the transaction command were successful, and therefore, changes were applied to the product table because we used the commit transaction command. The group name has been changed to 'sales', and a new record with product ID 2 has been added.

    Define Transaction in DBMS
    This is an example where one of the queries within the transaction encounters an error :

    1. Let's consider a table named '[Product]' containing the following values :

    Define Transaction in DBMS

    2. Now, let's examine the ROLLBACK TRANSACTION query below, which rolls back changes whether the queries within the transaction succeeded or failed. Run below transaction statement :

    BEGIN TRANSACTION ABC
    
    INSERT INTO [dbo].[Product] (
    	    [Product_Id], [Name], [GroupName], [ModifiedDate] )
            VALUES 
    		(2, 'Soap', 'Group-B', '2023-01-01 00:00:00.000');
    
    UPDATE [dbo].[Product]
    	SET [GroupName] = 'Sales'
    	WHERE [Product_Id] = 'A';
    
    ROLLBACK TRANSACTION ABC
            
    Define Transaction in DBMS

    3. In this example, we've named our transaction 'ABC' and included two SQL statements within it. We're demonstrating the rollback query, which undoes any changes made, regardless of whether the queries within the transaction succeeded or failed. Rollback always reverts all changes back to its original state.

    Check the output below, insert query is successful but update query within the transaction command is failed because we specified 'A' as the product ID, which is a string and cannot serve as a valid product Id. And therefore, no changes are made to the product table. To apply any changes defined within the transaction, we must commit.

    Define Transaction in DBMS

    4. Now, let's examine the COMMIT TRANSACTION query below, which commits changes only if all the queries within the transaction are successful. Run below transaction statement :

    BEGIN TRANSACTION ABC
    
    INSERT INTO [dbo].[Product] (
    	    [Product_Id], [Name], [GroupName], [ModifiedDate] )
            VALUES 
    		(2, 'Soap', 'Group-B', '2023-01-01 00:00:00.000');
    
    UPDATE [dbo].[Product]
    	SET [GroupName] = 'Sales'
    	WHERE [Product_Id] = 'A';
    
    COMMIT TRANSACTION ABC
            
    Define Transaction in DBMS

    5. In this example, we've named our transaction 'ABC' and included two SQL statements within it. We're demonstrating the commit query, It applies changes only if all the queries within the transaction are successful.

    Look at the results below. Insert query is successful but update query within the transaction command is failed because we specified 'A' as the product ID, which is a string and cannot serve as a valid product Id. and therefore, no changes were applied to the product table. Consequently, the system automatically rolled back the changes made by the insert query.

    Define Transaction in DBMS
    In the next chapter, we will explore the ACID properties of transaction in DBMS.

    Frequently Asked Questions :

    Define Transaction in DBMS?

    A transaction in a database management system (DBMS) is a defined sequence of operations that must be completed as a whole, ensuring atomicity, consistency, isolation, and durability (ACID properties). Define Transaction in DBMS as a structured set of actions executed on a database to maintain data integrity.

    Define transaction in dbms with example?

    Transaction in DBMS refers to a unit of work that includes one or more operations. For instance, transferring funds from one bank account to another constitutes a transaction in DBMS.

    What is transaction in dbms and its properties?

    In DBMS, a transaction is a logical unit of work that is performed against a database. Transactions in a database management system (DBMS) must maintain ACID properties: Atomicity, Consistency, Isolation, and Durability. These properties of DBMS ensure that transaction are reliable and maintain data integrity.

    What is transaction in dbms in simple words?

    Transaction in DBMS is a set of operations performed as a single logical unit of work. For example, imagine buying a book online – it involves searching for the book, adding it to your cart, and then making the payment, all of which form a transaction.
    Thank You! You should check ACID properties in DBMS post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy