These statements provide control over use of transactions:
o START TRANSACTION or BEGIN start a new transaction.
o COMMIT commits the current transaction, making its changes permanent.
o ROLLBACK rolls back the current transaction, canceling its changes.
o SET autocommit disables or enables the default autocommit mode for the current session.
By default, MySQL runs with autocommit mode enabled. This means that,when not otherwise inside a transaction, each statement is atomic, as if it were surrounded by START TRANSACTION and COMMIT. You cannot use ROLLBACK to undo the effect; however, if an error occurs during statement execution, the statement is rolled back.
To disable autocommit mode implicitly for a single series of statements, use the START TRANSACTION statement:
START TRANSACTION;
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
COMMIT;
With START TRANSACTION, autocommit remains disabled until you end the transaction with COMMIT or ROLLBACK. The autocommit mode then reverts to its previous state.
这些说明提供了对事务使用的控制:
启动事务或开始新事务。
提交提交当前事务,使其更改永久化。
回滚当前事务,取消其更改。
设置自动提交为当前会话禁用或启用默认自动提交模式。
默认情况下,MySQL在启用自动提交模式的情况下运行。这意味着,当不是在事务内部时,每条语句都是原子的,就好像它被START transaction和COMMIT包围一样。不能使用“回滚”来撤消效果;但是,如果在语句执行期间发生错误,则会回滚该语句。
要对一系列语句隐式禁用自动提交模式,请使用START TRANSACTION语句:
启动事务;START TRANSACTION;
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
COMMIT;
使用START TRANSACTION,自动提交将保持禁用状态,直到使用提交或回滚结束事务。然后,自动提交模式会恢复到以前的状态。