Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

savepoint commit rollback in mysql [duplicate]

Tags:

mysql

how can we use commit, rollback and savepoint in mysql ?

like image 356
Sakthivel Avatar asked Aug 18 '09 14:08

Sakthivel


People also ask

Can we ROLLBACK to same savepoint more than once?

The specified SQL savepoint also remains active after the ROLLBACK TO statement has been executed. This means the ROLLBACK TO statement can be executed in the same transaction more than once by specifying the same SQL savepoint name.

What is the difference between COMMIT ROLLBACK and savepoint?

COMMIT − to save the changes. ROLLBACK − to roll back the changes. SAVEPOINT − creates points within the groups of transactions in which to ROLLBACK.

Can we ROLLBACK to savepoint after COMMIT?

You can only roll back to the most recently marked savepoint. An implicit savepoint is marked before executing an INSERT , UPDATE , or DELETE statement. If the statement fails, a rollback to the implicit savepoint is done.

Does MySQL support savepoint?

MySQL InnoDB provides support for the statements SAVEPOINT, ROLLBACK TO SAVEPOINT, RELEASE SAVEPOINT. The SAVEPOINT statement is used to set a save point for the transaction with the specified name.


2 Answers

CREATE TABLE t_test (id INT NOT NULL PRIMARY KEY) ENGINE=InnoDB;

START TRANSACTION;

INSERT
INTO    t_test
VALUES  (1);

SELECT  *
FROM    t_test;

 id
---
  1

SAVEPOINT tran2;

INSERT
INTO    t_test
VALUES  (2);

SELECT  *
FROM    t_test;

 id
---
  1
  2

ROLLBACK TO tran2;

SELECT  *
FROM    t_test;

 id
---
  1

ROLLBACK;

SELECT  *
FROM    t_test;

 id
---
like image 87
Quassnoi Avatar answered Oct 13 '22 02:10

Quassnoi


mysql> start transaction;
mysql> savepoint id;

Here you alter the table data and then:

mysql> rollback to savepoint id;

View the data and finally:

mysql> release savepoint id;
like image 24
vennila Avatar answered Oct 13 '22 04:10

vennila