×

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known as a transaction.

There are some commands in the Structured Query Language, and Transaction Control Language command is one of the SQL commands. TCL commands handle the transactions in a database

In Structured Query Language, Commit, Rollback, and Savepoint are the Transaction Control Language commands widely used in SQL.

Now let’s understand and learn how the Transaction Control Language commands work in the Structured Query language with the help of a few examples. Transaction Control Language commands are:

  1. COMMIT Command
  2. ROLLBACK Command
  3. SAVEPOINT Command     

COMMIT Command

Commit Command in SQL is used to save all the transactions which change permanently to the disk. Whenever we perform the operation on Data Definition Language commands (DDL) such as INSERT command, UPDATE command, and DELETE command, whatever modifications made by these commands are permanent but before closing the current session or till the end of the session. We can easily undo the operation we performed using the Data Definition Language before the session gets shut down. Commit command is used to save the modification permanently to the storage without session ends.

Syntax:

COMMIT;

If you have an existing database in the system, use that existing database or create a new database. We will use the existing database Company.

USE Company

Consider the already existing employees' tables which have the certain data:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65000PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAORACLE1
1003NIKHILVANI50000JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAITESTING4
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4

In SQL, the START TRANSACTION query is used to begin the transaction.

START TRANSACTION;           
TCL Commands In SQL

Now, we will insert two records into the existing Employees table. To insert multiple records, we will use the below statement:

INSERT INTO EMPLOYEES (EMPLOYEEID, FIRST_NAME, LAST_NAME, SALARY, CITY, DEPARTMENT, MANAGERID) VALUES (4002, 'ASHWINI', 'BAGHAT', 54500, 'JAIPUR', 'JAVA', 3), (4003, 'PURVA', 'DHANDEKAR', 57000, 'PUNE', 'TESTING', 4);

We will now use the SELECT query to verify whether the records are inserted successfully or not in the table.

SELECT * FROM EMPLOYEES;

Output on the above query:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65000PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAORACLE1
1003NIKHILVANI50000JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAITESTING4
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500JAIPURJAVA3
4003PURVA DHANDEKAR57000PUNETESTING4
TCL Commands In SQL

The output shows all the records are inserted successfully.

To save the INSERT operation on the Employees table, we will execute the COMMIT command.

COMMIT;
TCL Commands In SQL

In SQL, by default, auto-commit is on. To turn off the auto-commit in the SQL, set the auto-commit value to 0.

We will update the employee id 4002 and set the city as 'Noida. Use the below query to update the employee record.

UPDATE EMPLOYEES SET CITY = 'NOIDA' WHERE EMPLOYEEID = 4002;

We will now use the SELECT query to verify whether the record is updated successfully or not in the table.

SELECT * FROM EMPLOYEES;

Output on the above query:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65000PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAORACLE1
1003NIKHILVANI50000JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAITESTING4
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3
4003PURVA DHANDEKAR57000PUNETESTING4
TCL Commands In SQL

As we can see in the output, employee Id 4002 city was modified.

To save the changes on the employees’ table, we will write a COMMIT command.

COMMIT;
TCL Commands In SQL

Now, we will delete the employee id 4003 from the Employees table. Use the below query to delete the employee record.

DELETE FROM EMPLOYEES WHERE EMPLOYEEID = 4003;

We will now use the SELECT query to verify whether the record is deleted successfully or not in the table.

SELECT * FROM EMPLOYEES;

Output on the above query:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65000PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAORACLE1
1003NIKHILVANI50000JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAITESTING4
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3
TCL Commands In SQL

The output confirms we deleted the employee id 4003 details.

To save the changes on the employees’ table, we will write a COMMIT command.

COMMIT;
TCL Commands In SQL

SAVEPOINT command

Database operations performed can be divided into two parts. For example, consider all the delete statements we performed as one segment of the transaction and the update statement as one segment of the transaction. To save insert queries into the insert part and delete queries into the delete part of the same transaction, we use the SAVEPOINT command in SQL. Suppose we want to save all the delete-related queries with the savepoint named delete. To save all the delete-related operations in one savepoint, we have to perform the SAVEPOINT command followed by the savepoint name after performing the delete query.

Syntax:

SAVEPOINT savepoint name; 

ROLLBACK command

While carrying out a transaction, we built the savepoints to save the different segments of the transaction. As the User's requirements changes, the User can undo the transaction to different savepoints. Consider a scenario: We have initiated a transaction followed by the table creation and record insertion into the table. We built the INS named savepoint after adding records to the table. After Insertion, we performed the delete operation, and later we remembered mistakenly deleted the wrong records. We will undo our transaction to reach the savepoint INS. To undo the transaction, use the ROLLBACK command.

We can roll back the operation we performed or roll back the entire savepoint transaction.

Syntax to Rollback the operation:

ROLLBACK;

Syntax to Rollback the transaction:

ROLLBACK TO savepoint;

Examples of the SAVEPOINT and ROLLBACK commands:

If you have an existing database in the system, use that existing database or create a new database. We will select the already existing database named Company.

USE Company

Consider the existing employee's tables which have the following records:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65000PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAORACLE1
1003NIKHILVANI50000JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAITESTING4
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3

In SQL, the START TRANSACTION query is used to begin the transaction.

START TRANSACTION;           
TCL Commands In SQL

Now, we will insert two records in the existing Employees table. To insert multiple records we will use below statement:

INSERT INTO EMPLOYEES (EMPLOYEEID, FIRST_NAME, LAST_NAME, SALARY, CITY, DEPARTMENT, MANAGERID) VALUES (4003, 'PURVA', 'DHANDEKAR', 57000, 'PUNE', 'TESTING', 4), (5001, 'ASHWIN', 'PATEL', 54500, 'JAIPUR', 'JAVA', 3);

We will now use the SELECT query to verify whether the records are inserted successfully or not in the table.

SELECT * FROM EMPLOYEES;

Output on the above query:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65000PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAORACLE1
1003NIKHILVANI50000JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAITESTING4
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3
4003PURVADHANDEKAR57000PUNETESTING4
5001ASHWINPATEL54500JAIPURJAVA3
TCL Commands In SQL

As we all know, the SAVEPOINT command in SQL is used to save different parts of the same transaction. Assume, Insertion is one segment of our transaction. Use savepoint to save this insertion segment as Insertion.

SAVEPOINT Insertion;
TCL Commands In SQL

Now, we will perform the UPDATE operation on the Employee tables, and we will update employee salary by 1.2 for the employee id 2003.

UPDATE EMPLOYEES SET SALARY = SALARY * 1.2 WHERE EMPLOYEEID = 2003;

We will cross-check the update operation by executing the SELECT query on the Employees table:

SELECT * FROM EMPLOYEES WHERE EMPLOYEEID = 2003;
EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
2003RUCHIKAJAIN60000MUMBAITESTING4
TCL Commands In SQL

The above query results show that the employee id 2003 record is updated successfully.

Consider the updation query as one segment of our transaction. We will save the update operation part using a SAVEPOINT named an Updation.

SAVEPOINT Updation;
TCL Commands In SQL

Now, we will perform a delete operation on the Employees table. We will perform delete operation on employees table where employee salary is greater than 60000.

DELETE FROM EMPLOYEES WHERE SALARY > 60000;

We will cross-check the delete operation by executing the SELECT query on the Employees table:

SELECT * FROM EMPLOYEES;

Output on the above query:

EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1002VAIBHAVSHARMA60000NOIDAORACLE1
1003NIKHILVANI50000JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2003RUCHIKAJAIN60000MUMBAITESTING4
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4002ASHWINIBAGHAT54500NOIDAJAVA3
4003PURVADHANDEKAR57000PUNETESTING4
5001ASHWINPATEL54500JAIPURJAVA3
TCL Commands In SQL

The above query shows that deleting the employee records was successful.

Consider the delete queries as one segment of our transaction. We will save the update operation part using a SAVEPOINT named a Deletion.

SAVEPOINT Deletion;
TCL Commands In SQL

Suddenly User realized the query we wanted to perform on the deletion operation was not supposed to be that query. In such a situation, users will roll back to the entire transaction to the SAVEPOINT; the User can roll back to any SAVEPOINT he wants to roll back like Insertion, Updation SAVEPOINT.

ROLLBACK TO Insertion;

If the user don' want to remove the records. Hence, the User will be rollbacked to the Insertion SAVEPOINT.

To cross-check that we have got the same employee table we had before performing the updation and deletion operations. To retrieve the employees' details perform the SELECT operation on the employees' table.

SELECT * FROM EMPLOYEES;
EMPLOYEEIDFIRST_NAMELAST_NAMESALARYCITYDEPARTMENTMANAGERID
1001VAIBHAVIMISHRA65000PUNEORACLE1
1002VAIBHAVSHARMA60000NOIDAORACLE1
1003NIKHILVANI50000JAIPURFMW2
2001PRACHISHARMA55500CHANDIGARHORACLE1
2002BHAVESHJAIN65500PUNEFMW2
2003RUCHIKAJAIN50000MUMBAITESTING4
3001PRANOTISHENDE55500PUNEJAVA3
3002ANUJAWANRE50500JAIPURFMW2
3003DEEPAMJAUHARI58500MUMBAIJAVA3
4001RAJESHGOUD60500MUMBAITESTING4
4002ASHWINIBAGHAT54500NOIDAJAVA3
4003PURVADHANDEKAR57000PUNETESTING4
5001ASHWINPATEL54500JAIPURJAVA3
TCL Commands In SQL

Related Topics

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

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

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

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

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

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

SQL Topological Sorting

It is for Directed Acyclic Graph, which linearly describes the ordering of graphs. It is not possible for all graphs, it is possible for only Directed acyclic graphs. Applications of Topological...

3 minutes read.

SQL Count

Structured Query Language Count() Function is used with Structured Query Language SELECT Statement. SQL Count() function returns the number of items that match the specified criteria in the SELECT statement. Count()...

2 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL SELECT WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples. The WHERE clause in SELECT query displays those records as...

5 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.