START TRANSACTION, COMMIT, and ROLLBACK

https://dev.mysql.com/doc/refman/5.7/en/commit.html

START TRANSACTION
    [transaction_characteristic [, transaction_characteristic] ...]

transaction_characteristic: {
    WITH CONSISTENT SNAPSHOT
  | READ WRITE
  | READ ONLY
}

BEGIN [WORK]
COMMIT [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
ROLLBACK [WORK] [AND [NO] CHAIN] [[NO] RELEASE]
SET autocommit = {0 | 1}

关闭自动提交,使用START TRANSACTION statement:

START TRANSACTION;
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
COMMIT;

To disable autocommit mode explicitly, use the following statement:

SET autocommit=0;

Statements That Cannot Be Rolled Back

有些语句不能被回滚,总体上来说,包括数据定义语言(DDL),比如说那些数据create 或者drop databases,还有那些create,drop,alter tables or stored routines.

你必须的保证你的事务中不包含这些语句。如果在事务中前面的语句不能回滚,后面的其他语句也将发生错误,将会造成整个事务的不能回滚。

原文地址:https://www.cnblogs.com/wonchaofan/p/13521196.html