The Wayback Machine - https://web.archive.org/web/20240822162006/https://www.geeksforgeeks.org/auto-commit-commands-and-compensating-transactions-in-sql/
Open In App

Auto-commit commands and Compensating transactions in SQL

Last Updated : 17 Feb, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

As the name suggests, the auto-commit statement is used to avoid the explicit mention of the commit command after each SQL statement. The commit statement itself gets executed by the server after each SQL statement. Each SQL statement is treated as a different transaction. An SQL statement executed in autocommit mode cannot be rolled back.

Auto-commit Commands :

There are four Auto-commit commands that exist in SQL, they are:

  1. SET AUTOCOMMIT ON –
    By executing this particular command, the auto-commit status turned to be ON, if it is OFF initially. This command is itself executed by the SQL server initially. After execution, the commit statement itself gets executed after each SQL statement. Here each statement is a separate transaction.
     
  2. SET AUTOCOMMIT OFF –
    This instruction is just the reverse of the first one. After executing this, the auto-commit status is changed to OFF. Now, the user needs to explicitly mention the commit statements, wherever required. Here Commit() or rollback() completes a transaction.
     
  3. SET AUTOCOMMIT INT_VALUE –
    This instruction is used to limit the number of statements, which itself gets auto-committed by the server. For example, after the execution of the below command, the auto-commit remains ON for every 9 transactions, and then it turned OFF by itself. SET AUTOCOMMIT 9
     
  4. SHOW AUTOCOMMIT –
    This statement is used to determine the current status of the auto-commit so that the database users can change it according to the requirements. Hence, this statement has only two possible outcomes, either ON or OFF.

    It completely depends upon the type of the database and the operations that need to be performed in the database that if the status would be kept ON or OFF. Because the changes or the modifications can’t be undone if the auto-commit is ON. The ROLLBACK statement has no effect if the AUTO-COMMIT is ON. Here, the concept of compensating transactions walks in. Yes, you can’t undo the changes but can write a compensating transaction to get back the previous state of the database.  

Let’s take a look at what compensating transactions actually are.

Compensating Transactions in SQL :

A compensating transaction is nothing but a list of database operations that are capable to undo the changes of an incomplete or inconsistent transaction. Compensating transactions are helpful to get back to the previous consistent state of the database. Let’s consider a simple example. A user deleted an important record from the database accidentally. So, the compensating transaction is nothing but the insertion of that record again in the database. Also, the compensating transactions do not ensure the entire restoration of the database initial state, since there is a chance that the other transactions may start executing meanwhile.



Similar Reads

SQL Commands | DDL, DQL, DML, DCL and TCL Commands
SQL commands are very used to interact with the database. These commands allow users to perform various actions on a database. This article will teach us about SQL commands or SQL sublanguage commands like DDL, DQL, DML, DCL, and TCL. All important SQL commands with their syntax and examples are covered in this article. But before heading to the SQ
6 min read
SQL TRANSACTIONS
Transactions group a set of tasks into a single execution unit. Each transaction begins with a specific job and ends when all the tasks in the group successfully completed. If any of the tasks fail, the transaction fails. Therefore, a transaction has only two results: success or failure. Example of a transaction to transfer $150 from account A to a
4 min read
PL/SQL Transactions
Prerequisites - PL/SQL Introduction, PL/SQL | User Input Write a PL/SQL code that will accept an account number from user. Check if user balance is less than minimum balance than only deduct Rs 100 from balance. The process is fired on the acct_mstr. Example - Consider the following scenario, acct_master (acct_no number(5) primary key, acct_name va
2 min read
Basic SQL Commands
QUESTION: Consider the following table ITEM given below, write the commands in SQL for 1 – 10 and output for 11 – 20 TABLE: ITEM S_NoItem_NameCompanyCostPurchase_QtyDOP1MonitorABC6300202010-05-302CPUCDF8000302010-07-233MouseFGH250252010-08-044UPSHIJ2100102010-06-195PrinterJKL840042010-11-276ScannerJKL450062010-08-14 Write the SQL commands for 1. to
2 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
Flat & Nested Distributed Transactions
Introduction : A transaction is a series of object operations that must be done in an ACID-compliant manner. Atomicity - The transaction is completed entirely or not at all.Consistency - It is a term that refers to the transition from one consistent state to another.Isolation - It is carried out separately from other transactions.Durability - Once
6 min read
SQL Auto Increment
Sometimes while creating a table we do not have a unique identifier within the table hence we face difficulty in choosing the Primary Key. So as to resolve such an issue we've to manually provide unique keys to every record but this is often also a tedious task. So we can use the Auto Increment feature that automatically generates a numerical Prima
5 min read
DDL Commands & Syntax
In this article, we will discuss the overview of DDL commands and will understand DDL commands like create, alter, truncate, drop. We will cover each command syntax with the help of an example for better understanding. Let's discuss it one by one. Overview :Data Definition Language(DDL) is a subset of SQL and a part of DBMS(Database Management Syst
3 min read
SQL SERVER – Input and Output Parameter For Dynamic SQL
An Input Parameter can influence the subset of rows it returns from a select statement within it. A calling script can get the value of an output parameter. An aggregate function or any computational expression within the stored process can be used to determine the value of the output parameter. A parameter whose value is given into a stored proced
3 min read
Difference between T-SQL and PL-SQL
1. Transact SQL (T-SQL) : T-SQL is an abbreviation for Transact Structure Query Language. It is a product by Microsoft and is an extension of SQL Language which is used to interact with relational databases. It is considered to perform best with Microsoft SQL servers. T-SQL statements are used to perform the transactions to the databases. T-SQL has
3 min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced
SQL (Structured Query Language) is a powerful tool used for managing and manipulating relational databases. Whether we are beginners or experienced professionals, practicing SQL exercises is important for improving your skills. Regular practice helps you get better at using SQL and boosts your confidence in handling different database tasks. So, in
15+ min read
Difference between SQL and T-SQL
SQL (Structured Query Language) is the standard language for managing and manipulating relational databases, enabling operations like querying, updating, and deleting data. T-SQL (Transact-SQL), an extension of SQL developed by Microsoft, adds advanced features and procedural capabilities specifically for SQL Server. In this article, We will learn
4 min read
SQL Server | Convert tables in T-SQL into XML
In this, we will focus on how tables will be converted in T-SQL into XML in SQL server. And you will be able to understand how you can convert it with the help of command. Let's discuss it one by one. Overview :XML (Extensible Markup Language) is a markup language similar to HTML which was designed to share information between different platforms.
2 min read
SQL - SELECT from Multiple Tables with MS SQL Server
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n
3 min read
How to Execute SQL Server Stored Procedure in SQL Developer?
A stored procedure is a set of (T-SQL ) statements needed in times when we are having the repetitive usage of the same query. When there is a need to use a large query multiple times we can create a stored procedure once and execute the same wherever needed instead of writing the whole query again. In this article let us see how to execute SQL Serv
2 min read
SQL Query to Check if Date is Greater Than Today in SQL
In this article, we will see the SQL query to check if DATE is greater than today's date by comparing date with today's date using the GETDATE() function. This function in SQL Server is used to return the present date and time of the database system in a ‘YYYY-MM-DD hh:mm: ss. mmm’ pattern. Features: This function is used to find the present date a
2 min read
SQL Query to Add a New Column After an Existing Column in SQL
Structured Query Language or SQL is a standard Database language that is used to create, maintain and retrieve data from relational databases like MySQL, Oracle, SQL Server, Postgres, etc. In Microsoft SQL Server, we can change the order of the columns and can add a new column by using ALTER command. ALTER TABLE is used to add, delete/drop or modif
3 min read
SQL Query to Convert Rows to Columns in SQL Server
In this article we will see, how to convert Rows to Column in SQL Server. In a table where many columns have the have same data for many entries in the table, it is advisable to convert the rows to column. This will help to reduce the table and make the table more readable. For example, Suppose we have a table given below: NAMECOLLEGEROLL NUMBERSUB
2 min read
How to SQL Select from Stored Procedure using SQL Server?
There may be situations in SQL Server where you need to use a stored procedure to get data from a SQL query. For direct data selection from a stored procedure within a query, SQL Server offers options like OPENQUERY and OPENROWSET. The usual way is running the stored procedure independently and then querying the outcomes. The idea of utilizing SQL
3 min read
SQL Quiz : Practice SQL Questions Online
This SQL quiz covers various topics like SQL basics, CRUD operations, operators, aggregation functions, constraints, joins, indexes, transactions, and query-based scenarios. We've included multiple-choice questions, fill-in-the-blank questions, and interactive coding challenges to keep things interesting and challenging. Whether you're a beginner l
3 min read
BULK INSERT in SQL Server(T-SQL command)
BULK INSERT in SQL Server(T-SQL command): In this article, we will cover bulk insert data from csv file using the T-SQL command in the SQL server and the way it is more useful and more convenient to perform such kind of operations. Let's discuss it one by one.  ConditionSometimes there is a scenario when we have to perform bulk insert data from .cs
3 min read
Combining aggregate and non-aggregate values in SQL using Joins and Over clause
Prerequisite - Aggregate functions in SQL, Joins in SQLAggregate functions perform a calculation on a set of values and return a single value. Now, consider an employee table EMP and a department table DEPT with following structure:Table - EMPLOYEE TABLE NameNullTypeEMPNONOT NULLNUMBER(4)ENAME VARCHAR2(10)JOB VARCHAR2(9)MGR NUMBER(4)HIREDATE DATESA
2 min read
SQL Full Outer Join Using Left and Right Outer Join and Union Clause
An SQL join statement is used to combine rows or information from two or more than two tables on the basis of a common attribute or field. There are basically four types of JOINS in SQL. In this article, we will discuss FULL OUTER JOIN using LEFT OUTER Join, RIGHT OUTER JOIN, and UNION clause. Consider the two tables below: Sample Input Table 1: Pu
3 min read
SQL AND and OR Operators
SQL AND and OR operators are used for data filtering and getting precise results based on conditions. They are used with the WHERE clause and are also called conjunctive operators. The AND operator returns records where all conditions specified are true and the OR operator returns records where at least one condition specified is true. AND and OR o
3 min read
SQL using Python and SQLite | Set 2
Databases offer numerous functionalities by which one can manage large amounts of information easily over the web, and high-volume data input and output over a typical file such as a text file. SQL is a query language and is very popular in databases. Many websites use MySQL. SQLite is a "light" version that works over syntax very much similar to S
3 min read
Difference between Static and Dynamic SQL
Static or Embedded SQL are SQL statements in an application that do not change at runtime and, therefore, can be hard-coded into the application. Dynamic SQL is SQL statements that are constructed at runtime; for example, the application may allow users to enter their own queries. Dynamic SQL is a programming technique that enables you to build SQL
2 min read
Difference between Simple and Complex View in SQL
A View in SQL as a logical subset of data from one or more tables. Views are used to restrict data access. A View contains no data of its own but it is like a window through which data from tables can be viewed or changed. The table on which a View is based is called BASE Tables. There are 2 types of Views in SQL: Simple View and Complex View. Simp
2 min read
How to Execute SQL File with Java using File and IO Streams?
In many cases, we often find the need to execute SQL commands on a database using JDBC to load raw data. While there are command-line or GUI interfaces provided by the database vendor, sometimes we may need to manage the database command execution using external software. In this article, we will learn how to execute an SQL file containing thousand
5 min read
Mean and Mode in SQL Server
Mean is the average of the given data set calculated by dividing the total sum by the number of values in data set. Example: Input: 1, 2, 3, 4, 5 Output: 3 Explanation: sum = 1 + 2 + 3 + 4 + 5 = 15 number of values = 5 mean = 15 / 5 = 3 Query to find mean in the table SELECT Avg(Column_Name) FROM Table_Name Example: Creating Table: Table Content: Q
1 min read
three90RightbarBannerImg