在mysql中保存点提交回滚

时间:2009-08-18 14:47:07

标签: mysql

我们如何在mysql中使用commit,rollback和savepoint?

2 个答案:

答案 0 :(得分:15)

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

答案 1 :(得分:8)

mysql> start transaction;
mysql> savepoint id;

在此您可以更改表数据,然后:

mysql> rollback to savepoint id;

查看数据,最后:

mysql> release savepoint id;