<bdo id='IstDK'></bdo><ul id='IstDK'></ul>

<tfoot id='IstDK'></tfoot>

      <small id='IstDK'></small><noframes id='IstDK'>

        <i id='IstDK'><tr id='IstDK'><dt id='IstDK'><q id='IstDK'><span id='IstDK'><b id='IstDK'><form id='IstDK'><ins id='IstDK'></ins><ul id='IstDK'></ul><sub id='IstDK'></sub></form><legend id='IstDK'></legend><bdo id='IstDK'><pre id='IstDK'><center id='IstDK'></center></pre></bdo></b><th id='IstDK'></th></span></q></dt></tr></i><div id='IstDK'><tfoot id='IstDK'></tfoot><dl id='IstDK'><fieldset id='IstDK'></fieldset></dl></div>

      1. <legend id='IstDK'><style id='IstDK'><dir id='IstDK'><q id='IstDK'></q></dir></style></legend>

        mysql 中 SET autocommit=1 和 START TRANSACTION 之间的区别(我错过了什么?)

        时间:2023-06-02
        <i id='99HQb'><tr id='99HQb'><dt id='99HQb'><q id='99HQb'><span id='99HQb'><b id='99HQb'><form id='99HQb'><ins id='99HQb'></ins><ul id='99HQb'></ul><sub id='99HQb'></sub></form><legend id='99HQb'></legend><bdo id='99HQb'><pre id='99HQb'><center id='99HQb'></center></pre></bdo></b><th id='99HQb'></th></span></q></dt></tr></i><div id='99HQb'><tfoot id='99HQb'></tfoot><dl id='99HQb'><fieldset id='99HQb'></fieldset></dl></div>

              <tbody id='99HQb'></tbody>
              <legend id='99HQb'><style id='99HQb'><dir id='99HQb'><q id='99HQb'></q></dir></style></legend>

              <small id='99HQb'></small><noframes id='99HQb'>

            1. <tfoot id='99HQb'></tfoot>
                  <bdo id='99HQb'></bdo><ul id='99HQb'></ul>
                • 本文介绍了mysql 中 SET autocommit=1 和 START TRANSACTION 之间的区别(我错过了什么?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在阅读 MySQL 中的事务,但不确定我是否正确掌握了某些特定内容,我想确保我理解正确,所以这里是.我知道事务应该做什么,我只是不确定我是否理解语句语义.

                  所以,我的问题是,以下有什么问题吗(如果是的话,有什么问题):

                  默认情况下,MySQL 中启用了自动提交模式.

                  现在,SET autocommit=0; 将开始一个事务,SET autocommit=1; 将隐式提交.COMMIT;ROLLBACK; 都是可能的,在这两种情况下,自动提交之后仍然设置为 0(并且隐式启动了一个新事务).>

                  START TRANSACTION; 基本上会 SET autocommit=0; 直到 COMMIT;ROLLBACK; 发生.

                  换句话说,START TRANSACTION;SET autocommit=0; 是等价的,除了 START TRANSACTION; 执行相当于在 COMMIT;ROLLBACK;

                  之后隐式添加一个 SET autocommit=0;

                  如果是这样的话,我不明白 http://dev.mysql.com/doc/refman/5.5/en/set-transaction.html#isolevel_serializable - 看到隔离级别意味着存在事务,这意味着应该关闭自动提交无论如何?

                  如果开始事务和设置自动提交之间还有另一个区别(除了上述区别),那是什么?

                  解决方案

                  了解数据库的事务(自动提交、显式和隐式)处理可以使您不必从备份中恢复数据.

                  事务控制数据操作语句以确保它们是原子的.原子"意味着事务要么发生,要么不发生.向数据库发出事务完成信号的唯一方法是使用 COMMITROLLBACK 语句(根据 ANSI-92,遗憾的是它没有包含用于创建/开始交易,因此它是特定于供应商的).COMMIT 应用在事务中所做的更改(如果有).ROLLBACK 忽略事务中发生的任何操作 - 当 UPDATE/DELETE 语句执行意外操作时非常需要.

                  通常单独的 DML(插入、更新、删除)语句在自动提交事务中执行 - 一旦语句成功完成,它们就会被提交.这意味着在像您这样的情况下,没有机会将数据库回滚到语句运行之前的状态.当出现问题时,唯一可用的恢复选项是从备份中重建数据(假设存在).在 MySQL 中,自动提交 on InnoDB 默认情况下 - MyISAM 不支持事务.可以使用以下方法禁用它:

                  SET 自动提交 = 0

                  显式事务是将语句包装在显式定义的事务代码块中 - 对于 MySQL,这是 START TRANSACTION.它还需要在事务结束时显式生成 COMMITROLLBACK 语句.嵌套事务超出了本主题的范围.

                  隐式交易与显式交易略有不同.隐式事务不需要显式定义事务.但是,与显式事务一样,它们需要提供 COMMITROLLBACK 语句.

                  结论

                  显式交易是最理想的解决方案 - 它们需要一个语句,COMMITROLLBACK,来完成交易,并且清楚地说明正在发生的事情供其他人阅读如果有需要.如果以交互方式使用数据库,隐式事务是可以的,但是 COMMIT 语句应该只在结果经过测试后指定彻底确定有效.

                  这意味着你应该使用:

                  SET 自动提交 = 0;开始交易;更新 ...;

                  ...并且只有在结果正确时才使用 COMMIT;.

                  也就是说,UPDATE 和 DELETE 语句通常只返回受影响的行数,而不是具体的细节.将此类语句转换为 SELECT 语句 &检查结果以确保之前尝试更新/删除语句的正确性.

                  附录

                  DDL(数据定义语言)语句是自动提交的——它们不需要 COMMIT 语句.IE:表、索引、存储过程、数据库和视图创建或更改语句.

                  I am reading up on transactions in MySQL and am not sure whether I have grasped something specific correctly, and I want to be sure I understood that correctly, so here goes. I know what a transaction is supposed to do, I'm just not sure whether I understood the statement semantics or not.

                  So, my question is, is anything wrong, (and, if that is the case, what is wrong) with the following:

                  By default, autocommit mode is enabled in MySQL.

                  Now, SET autocommit=0; will begin a transaction, SET autocommit=1; will implicitly commit. It is possible to COMMIT; as well as ROLLBACK;, in both of which cases autocommit is still set to 0 afterwards (and a new transaction is implicitly started).

                  START TRANSACTION; will basically SET autocommit=0; until a COMMIT; or ROLLBACK; takes place.

                  In other words, START TRANSACTION; and SET autocommit=0; are equivalent, except for the fact that START TRANSACTION; does the equivalent of implicitly adding a SET autocommit=0; after COMMIT; or ROLLBACK;

                  If that is the case, I don't understand http://dev.mysql.com/doc/refman/5.5/en/set-transaction.html#isolevel_serializable - seeing as having an isolation level implies that there is a transaction, meaning that autocommit should be off anyway?

                  And if there is another difference (other than the one described above) between beginning a transaction and setting autocommit, what is it?

                  解决方案

                  Being aware of the transaction (autocommit, explicit and implicit) handling for your database can save you from having to restore data from a backup.

                  Transactions control data manipulation statement(s) to ensure they are atomic. Being "atomic" means the transaction either occurs, or it does not. The only way to signal the completion of the transaction to database is by using either a COMMIT or ROLLBACK statement (per ANSI-92, which sadly did not include syntax for creating/beginning a transaction so it is vendor specific). COMMIT applies the changes (if any) made within the transaction. ROLLBACK disregards whatever actions took place within the transaction - highly desirable when an UPDATE/DELETE statement does something unintended.

                  Typically individual DML (Insert, Update, Delete) statements are performed in an autocommit transaction - they are committed as soon as the statement successfully completes. Which means there's no opportunity to roll back the database to the state prior to the statement having been run in cases like yours. When something goes wrong, the only restoration option available is to reconstruct the data from a backup (providing one exists). In MySQL, autocommit is on by default for InnoDB - MyISAM doesn't support transactions. It can be disabled by using:

                  SET autocommit = 0
                  

                  An explicit transaction is when statement(s) are wrapped within an explicitly defined transaction code block - for MySQL, that's START TRANSACTION. It also requires an explicitly made COMMIT or ROLLBACK statement at the end of the transaction. Nested transactions is beyond the scope of this topic.

                  Implicit transactions are slightly different from explicit ones. Implicit transactions do not require explicity defining a transaction. However, like explicit transactions they require a COMMIT or ROLLBACK statement to be supplied.

                  Conclusion

                  Explicit transactions are the most ideal solution - they require a statement, COMMIT or ROLLBACK, to finalize the transaction, and what is happening is clearly stated for others to read should there be a need. Implicit transactions are OK if working with the database interactively, but COMMIT statements should only be specified once results have been tested & thoroughly determined to be valid.

                  That means you should use:

                  SET autocommit = 0;
                  
                  START TRANSACTION;
                    UPDATE ...;
                  

                  ...and only use COMMIT; when the results are correct.

                  That said, UPDATE and DELETE statements typically only return the number of rows affected, not specific details. Convert such statements into SELECT statements & review the results to ensure correctness prior to attempting the UPDATE/DELETE statement.

                  Addendum

                  DDL (Data Definition Language) statements are automatically committed - they do not require a COMMIT statement. IE: Table, index, stored procedure, database, and view creation or alteration statements.

                  这篇关于mysql 中 SET autocommit=1 和 START TRANSACTION 之间的区别(我错过了什么?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用 LOAD DATA INFILE 将 CSV 文件中的选定列插入 MySQL 数据库 下一篇:保存多语言数据的最佳数据库结构是什么?

                  相关文章

                • <i id='2e4ZG'><tr id='2e4ZG'><dt id='2e4ZG'><q id='2e4ZG'><span id='2e4ZG'><b id='2e4ZG'><form id='2e4ZG'><ins id='2e4ZG'></ins><ul id='2e4ZG'></ul><sub id='2e4ZG'></sub></form><legend id='2e4ZG'></legend><bdo id='2e4ZG'><pre id='2e4ZG'><center id='2e4ZG'></center></pre></bdo></b><th id='2e4ZG'></th></span></q></dt></tr></i><div id='2e4ZG'><tfoot id='2e4ZG'></tfoot><dl id='2e4ZG'><fieldset id='2e4ZG'></fieldset></dl></div>

                    1. <tfoot id='2e4ZG'></tfoot>
                        <bdo id='2e4ZG'></bdo><ul id='2e4ZG'></ul>

                      <small id='2e4ZG'></small><noframes id='2e4ZG'>

                      <legend id='2e4ZG'><style id='2e4ZG'><dir id='2e4ZG'><q id='2e4ZG'></q></dir></style></legend>