×

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

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.

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 LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

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

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 UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

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

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

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

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

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

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 minutes read.