我正在阅读 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 - 看到隔离级别意味着存在事务,这意味着应该关闭自动提交无论如何?
如果开始事务和设置自动提交之间还有另一个区别(除了上述区别),那是什么?
了解数据库的事务(自动提交、显式和隐式)处理可以使您不必从备份中恢复数据.
事务控制数据操作语句以确保它们是原子的.原子"意味着事务要么发生,要么不发生.向数据库发出事务完成信号的唯一方法是使用 COMMIT
或 ROLLBACK
语句(根据 ANSI-92,遗憾的是它没有包含用于创建/开始交易,因此它是特定于供应商的).COMMIT
应用在事务中所做的更改(如果有).ROLLBACK
忽略事务中发生的任何操作 - 当 UPDATE/DELETE 语句执行意外操作时非常需要.
通常单独的 DML(插入、更新、删除)语句在自动提交事务中执行 - 一旦语句成功完成,它们就会被提交.这意味着在像您这样的情况下,没有机会将数据库回滚到语句运行之前的状态.当出现问题时,唯一可用的恢复选项是从备份中重建数据(假设存在).在 MySQL 中,自动提交 on InnoDB 默认情况下 - MyISAM 不支持事务.可以使用以下方法禁用它:
SET 自动提交 = 0
显式事务是将语句包装在显式定义的事务代码块中 - 对于 MySQL,这是 START TRANSACTION
.它还需要在事务结束时显式生成 COMMIT
或 ROLLBACK
语句.嵌套事务超出了本主题的范围.
隐式交易与显式交易略有不同.隐式事务不需要显式定义事务.但是,与显式事务一样,它们需要提供 COMMIT
或 ROLLBACK
语句.
显式交易是最理想的解决方案 - 它们需要一个语句,COMMIT
或 ROLLBACK
,来完成交易,并且清楚地说明正在发生的事情供其他人阅读如果有需要.如果以交互方式使用数据库,隐式事务是可以的,但是 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.
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.
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 之间的区别(我错过了什么?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!