×

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query Language.

Transaction Control Language commands in the Structured Query Language are Commit and Rollback.

Whatever commands we executed wrapped in one unit of work known as a transaction.

All the operations or commands of DDL or DML are stored or executed in a transaction. Suppose we have executed one update or delete operation on a table executed in a transaction. To save such executed DDL or DML, we have to execute the commit command of a Transaction Control Language. Commit is used to save all the operations we performed on a table, and all the operation is saved. All this is about Commit.

Now, think, what if we want to undo the operations we saved using the commit commands? Then can we undo the operation in the Structured Query Language? Yes, we can undo the committed operations; we will use another command of Transaction Control Language to undo the operations, and that command is Rollback. We will use the Rollback command to undo the commit operation in the Structured Query Language.

Let us see a few examples of Commit and Rollback commands:

Consider the existing employees tables which have the following records:

EmployeeidFirst_NameLast_NameEmployee_SalaryEmployee_CityEmployee_DepartmentGender
1001PranotiShende48000KolhapurFMWF
1002VaibhavSharma20000NoidaFMWM
1003NikhilVani55000IndoreOracleM
1004PrachiSharma55000NoidaOracleF
1005HarshadaKoli48500NashikAngularF
1006SonalMaheshwari24000PuneOracleF
1007BhaveshJain65000PuneFMWM
1008KapilVerma50000NashikAngularM
1009RajeshGoud55000IndoreTestingM
1010DeepamJauhari60000IndoreTestingM

We can use the commit and Rollback command without starting the transactions, but we will start our transaction using Start Transaction Command for good practice.

Let’s begin and see each operation example one by one.

First, we will turn off the auto-commit by assigning a value of auto-commit to 0

SET AUTOCOMMIT = 0;

Example 1:  In this example, we will insert new values into existing table employees and then will commit

INSERT INTO EMPLOYEES VALUES(2001, ‘SURILI’, ‘JAIN’,  55000, ‘MUMBAI’,  ‘TESTING’, ‘F’);

We will use the SELECT query to check the record that is inserted successfully or not in the employee's table:

SELECT * FROM EMPLOYEES;

The output of the above query is as follows:

EmployeeidFirst_NameLast_NameEmployee_SalaryEmployee_CityEmployee_DepartmentGender
1001PranotiShende48000KolhapurFMWF
1002VaibhavSharma20000NoidaFMWM
1003NikhilVani55000IndoreOracleM
1004PrachiSharma55000NoidaOracleF
1005HarshadaKoli48500NashikAngularF
1006SonalMaheshwari24000PuneOracleF
1007BhaveshJain65000PuneFMWM
1008KapilVerma50000NashikAngularM
1009RajeshGoud55000IndoreTestingM
1010DeepamJauhari60000IndoreTestingM
2001SuriliJain55000MumbaiTestingF
Commit and Rollback in SQL

As we can see, the record, which we insert into the table is successfully inserted.

Now, to save this transaction, we will use the commit command.

COMMIT;
Commit and Rollback in SQL

The employee id 2001 details are saved now, and it won't be Rollback until and unless we delete it using the Delete command.

Now, I want to delete the employee whose id is 2001. We will use the Delete operation to delete this employee's details

DELETE FROM EMPLOYEES WHERE EMPLOYEEID = 2001;

We will execute the Select query to verify whether the records we deleted are successfully deleted or not.

SELECT * FROM EMPLOYEES;

The output of the above query is as follows:

EmployeeidFirst_NameLast_NameEmployee_SalaryEmployee_CityEmployee_DepartmentGender
1001PranotiShende48000KolhapurFMWF
1002VaibhavSharma20000NoidaFMWM
1003NikhilVani55000IndoreOracleM
1004PrachiSharma55000NoidaOracleF
1005HarshadaKoli48500NashikAngularF
1006SonalMaheshwari24000PuneOracleF
1007BhaveshJain65000PuneFMWM
1008KapilVerma50000NashikAngularM
1009RajeshGoud55000IndoreTestingM
1010DeepamJauhari60000IndoreTestingM
Commit and Rollback in SQL

We deleted the employee whose id was 2001. Now, what if I want to undo the Delete operation committed, can we undo the delete operation? Yes, we can undo the operation committed using the Rollback command. Rollback is used to undo the transaction.

We will execute the ROLLBACK command to undo the operation.

ROLLBACK;
Commit and Rollback in SQL

We will execute the Select query to view the records of the employee's table. We undo the delete operation. But how will you know whether the Delete operation is Rollback or not?

SELECT * FROM EMPLOYEES;

The output of the above query is as follows:

EmployeeidFirst_NameLast_NameEmployee_SalaryEmployee_CityEmployee_DepartmentGender
1001PranotiShende48000KolhapurFMWF
1002VaibhavSharma20000NoidaFMWM
1003NikhilVani55000IndoreOracleM
1004PrachiSharma55000NoidaOracleF
1005HarshadaKoli48500NashikAngularF
1006SonalMaheshwari24000PuneOracleF
1007BhaveshJain65000PuneFMWM
1008KapilVerma50000NashikAngularM
1009RajeshGoud55000IndoreTestingM
1010DeepamJauhari60000IndoreTestingM
2001SuriliJain55000MumbaiTestingF
Commit and Rollback in SQL

The above output clearly says us that we successfully undo the Delete operation using the Rollback command.

Now next operation we will see is on the Update operation. We will write a query to update the employee salary whose employee id is 1002 and set the employee salary as 48500.

UPDATE EMPLOYEES SET EMPLOYEE_SALARY = 48500 WHERE EMPLOYEEID = 1002;

Again, we will execute the Select query to check whether the employee salary is modified or not.

SELECT * FROM EMPLOYEES WHERE EMPLOYEEID = 1002;
EmployeeidFirst_NameLast_NameEmployee_SalaryEmployee_CityEmployee_DepartmentGender
1002VaibhavSharma48500NoidaFMWM
Commit and Rollback in SQL

The result clearly itself says that employee salary is modified. What if I want to undo the update operation of employee id 1002? Will I need to execute an update operation on employee Id 1002 again? Rather than executing an update operation on employee id 1002 and modifying the salary, we will undo the update operation using Rollback Command.

ROLLBACK;

Commit and Rollback in SQL

We undo the update operation now, will execute the Select query, and check whether it is Rollback or not.

 SELECT * FROM EMPLOYEES WHERE EMPLOYEEID = 1002;

The output of the above query is as follows:

EmployeeidFirst_NameLast_NameEmployee_SalaryEmployee_CityEmployee_DepartmentGender
1002VaibhavSharma20000NoidaFMWM
Commit and Rollback in SQL

As we can see, the employee id 1002 employee salary as same earlier before executing the Update query.

Note: We can undo the Delete command and truncate command, but we cannot undo the Drop command because Drop removes all the data and Drop the table structure and Drop all the index or views related to that table.


Related Topics

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL SELECT BETWEEN Operator

This SQL tutorial explains and helps us understand how to use the BETWEEN operator in the SELECT query with examples. The SQL SELECT BETWEEN operator retrieves the data from the table...

2 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.

SQL Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

4 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.