Movatterモバイル変換


[0]ホーム

URL:


Studytonight is now part of theGUVI universe.Explore GUVI →
🤩 NewCool Developer Tools for you.Explore →
FREE JavaScript Video SeriesStart Learning →
 Signup/Sign In
Tests
MCQs to test your knowledge.
Compilers
Compilers to execute code in browser.
Index

Commit, Rollback and Savepoint SQL commands

Transaction Control Language(TCL) commands are used to manage transactions in thedatabase.

Before moving forward with TCL commands, check these topics out first:

These are used to manage the changes made to the data in a table by DML statements. It also allows statements to be grouped together into logical transactions.


COMMIT command

COMMIT command is used to permanently save any transaction into the database.

When we use any DML command likeINSERT,UPDATE orDELETE, the changes made by these commands are not permanent, until the current session is closed, the changes made by these commands can be rolled back.

To avoid that, we use theCOMMIT command to mark the changes as permanent.

Following is commit command's syntax,

COMMIT;

ROLLBACK command

This command restores the database to last commited state. It is also used withSAVEPOINT command to jump to a savepoint in an ongoing transaction.

If we have used theUPDATE command to make some changes into the database, and realise that those changes were not required, then we can use theROLLBACK command to rollback those changes, if they were not commited using theCOMMIT command.

Following is rollback command's syntax,

ROLLBACK TO savepoint_name;

SAVEPOINT command

SAVEPOINT command is used to temporarily save a transaction so that you can rollback to that point whenever required.

Following is savepoint command's syntax,

SAVEPOINT savepoint_name;

In short, using this command we canname the different states of our data in any table and then rollback to that state using theROLLBACK command whenever required.


Using Savepoint and Rollback

Following is the tableclass,

idname
1Abhi
2Adam
4Alex

Lets use some SQL queries on the above table and see the results.

INSERT INTO class VALUES(5, 'Rahul');COMMIT;UPDATE class SET name = 'Abhijit' WHERE id = '5';SAVEPOINT A;INSERT INTO class VALUES(6, 'Chris');SAVEPOINT B;INSERT INTO class VALUES(7, 'Bravo');SAVEPOINT C;SELECT * FROM class;

NOTE:SELECT statement is used to show the data stored in the table.

The resultant table will look like,

idname
1Abhi
2Adam
4Alex
5Abhijit
6Chris
7Bravo

Now let's use theROLLBACK command to roll back the state of data to thesavepoint B.

ROLLBACK TO B;SELECT * FROM class;

Now ourclass table will look like,

idname
1Abhi
2Adam
4Alex
5Abhijit
6Chris

Now let's again use theROLLBACK command to roll back the state of data to thesavepoint A

ROLLBACK TO A;SELECT * FROM class;

Now the table will look like,

idname
1Abhi
2Adam
4Alex
5Abhijit

So now you know how the commandsCOMMIT,ROLLBACK andSAVEPOINT works.




  DBMS MCQ Tests

Prepare for DBMS Interview in TCS, Infosys, etc. companies.

DBMS  Practice SQL Queries

Practice SQL in our online Query executor with sample data.

[8]ページ先頭

©2009-2025 Movatter.jp