×

Save Point in SQL

In SQL, the classification is done into 4 languages. They are

  1. Data Definition Language (DDL)
  2. Data Manipulation Language (DML)
  3. Transaction Control Language (TCL)
  4. Data Control Language (DCL)

Save Point falls under the Transaction Control Language. Transaction is a unit of work in the database, which is formed after execution of many commandsconsecutively. So, Transaction Control Language handles/manages all the transactions in a database.

Under Transaction control Language we also have roll back and commit along with the save point command.

Commit:

Once we write all the data into the tables and perform all the necessary operations on the table, we should commit the work for long lasting storage.

When we perform any Data Manipulation Language commands like insert, delete, update on a table in the database, those manipulations will stay alive till the current session of the database terminates.

Once the current session terminates, all the manipulations on the table will be washed off permanently. At this time, COMMIT command helps us to save all the manipulation progress on the tables permanently.

Syntax for commit:

COMMIT;

Example:

Consider a table of Student data having Name, ID, Age as fields.

IDNameAge
511Daksh18
512Ravi17
513Priya19

Now let’s try to insert records into the table.

insert into Student values (514, ‘Yash’, 18);
insert into Student values (515, ‘Keerthi’, 19);
select * from Student;
IDNameAge
511Daksh18
512Ravi17
513Priya19
514Yash18
515Keerthi19

So, the table is updated with two new rows. Now, we should commit the changes in the table.

COMMIT;

So, the two rows are added to the table permanently.

Now, let’s update the age of Yash from 18 to 19.

Update Student set Age = 19 where ID = 514;
select * from Student;
IDNameAge
511Daksh18
512Ravi17
513Priya19
514Yash19
515Keerthi19

Again, after the update command operations on the table, we should again commit the changes for Permanent Storage.

Save Point:

We perform many operations on a particular table. All these operations are classified into two parts. Consider a part of the transactions having all delete statements and another part of the transaction having all update statements. For saving the insert queries into the insert part and delete queries into the delete part, we prefer to use Save point command in SQL.

Syntax:

Let’s suppose that we want to save all the insert related queries into an insert save point. For implementing that we will perform the following query,

SAVEPOINT savepoint name;

Example:

Consider a customer table having Name, ID, and Age as the fields of the table.

IDNameAge
2551Ravi23
2552Virat24
2553Rinku26
2554Sharma18
2555Shravya20

Now let’s delete a row having 2552 as the customer ID.

delete from Customer where ID = 2552;
select * from Customer;
IDNameAge
2551Ravi23
2553Rinku26
2554Sharma18
2555Shravya20

Now we will create a savepoint.

SAVEPOINT sp1;

This will create a savepoint after deletion of a row.

 The above syntax serves for the creation of the save point only. So, to undo some of the previous group of transactions, here we use rollback command.

ROLLBACK TO savepoint name;

Example:

In the above table, we are left with 4 rows after deleting a row and creating the first savepoint.

Consider the above table.

CUSTOMER TABLE

IDNameAge
2551Ravi23
2553Rinku26
2554Sharma18
2555Shravya20

 Now, try to delete another row from the Customer table and create another savepoint.

delete from Customer where ID = 2555;

Now the table looks like,

select * from Customer;

CUSTOMER TABLE

IDNameAge
2551Ravi23
2553Rinku26
2554Sharma18

Now, we should create another savepoint.

SAVEPOINT sp2;

Delete the third row now.

delete from Customer where ID = 2554;

Now the table has two rows left.

CUSTOMER TABLE

IDNameAge
2551Ravi23
2553Rinku26

So, now let’s bring back the rows with the help of the savepoints which we had created for the Customer table.

Execute the following command.

ROLLBACK TO sp2;

Rollback Complete.

Now if we try to print the table, we will be directed to sp2.

CUSTOMER TABLE

IDNameAge
2551Ravi23
2553Rinku26
2554Sharma18

As we can observe we got the last deleted row back. So, for undo operations performed on tables, SAVEPOINT works efficiently.

Now, execute the rollback to sp1 command, and observe the changes.

ROLLBACK TO sp1;

This will undo the operations performed on the table up to Save point 1 (sp1).

So, now we have the table as,

select * from Customer;

CUSTOMER TABLE

IDNameAge
2551Ravi23
2553Rinku26
2554Sharma18
2555Shravya20

So, we can observe the Rollback operation is done successfully.

Let’s now consider another example for the above table only.

CUSTOMER TABLE

IDNameAge
2551Ravi23
2552Virat24
2553Rinku26
2554Sharma18
2555Priya20

We will now insert a row to the above table.

insert into Customer values (2556, ‘Shashi’, 24);
select * from Customer;
IDNameAge
2551Ravi23
2552Virat24
2553Rinku26
2554Sharma18
2555Priya20
2556Shashi24

So, as we inserted a row, we will now create a Save point over here with the name Insertion.

SAVEPOINT Insertion;

For Updating the row having ID = 2555, we give the following query.

Update Customer set Age = 21 where ID = 2555;
IDNameAge
2551Ravi23
2552Virat24
2553Rinku26
2554Sharma18
2555Priya21
2556Shashi24

So, we can now create an Update savepoint.

SAVEPOINT updation;

So, again if we do not need any updation we can rollback to the Insertion Save Point.

ROLLBACK TO Insertion;

We can get the table which is having changes up to the Insertion Save Point.


Related Topics

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 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 INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

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

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

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

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 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 CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar...

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

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL Top Clause

Top Clause: It returns the first ‘n’ number of rows as output. This Top clause is used to retrieve the required number of rows when there are thousands of records stored...

3 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 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 Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.