在InnoDB中,事务中是否全部sql?

In InnoDB,is all sql in the transaction?

我好像有个错误的想法:

update table_name set id=222 where id >333;

我的旧观点是上面的单一 sql 没有 begin 并且 commit 不会开始交易。 但好像不对。


但是当我阅读 mysql doc 时,我发现似乎所有 sql 都将在 transaction.if 中,您没有明确使用 begincommit,它将启动 InnoDB 中隐含的事务。

In InnoDB, all user activity occurs inside a transaction. If autocommit mode is enabled, each SQL statement forms a single transaction on its own. By default, MySQL starts the session for each new connection with autocommit enabled, so MySQL does a commit after each SQL statement if that statement did not return an error. If a statement returns an error, the commit or rollback behavior depends on the error. See Section 14.21.4, “InnoDB Error Handling”.

我的问题:

是否所有sql都会在事务中执行,无论您是否显式使用begincommit

它将在 单个 事务中执行 - 除非使用 START TRANSACTION & COMMIT 分组语句(然后 autocommit 配置将被忽略,无论它的价值)。 SET autocommit = 0/1 可用于即时控制隔离级别。还有一个配置选项,以便全局更改行为:

[mysqld]
autocommit=0

"Server System Variables" 的文档对此进行了详细解释:

The autocommit mode. If set to 1, all changes to a table take effect immediately. If set to 0, you must use COMMIT to accept a transaction or ROLLBACK to cancel it. If autocommit is 0 and you change it to 1, MySQL performs an automatic COMMIT of any open transaction. Another way to begin a transaction is to use a START TRANSACTION or BEGIN statement.