ROLLBACK 的 SQL 查询中存在哪些类型的错误?
例如:
insert into table( a, b ) values ('a','b') could generate the following error:
**a-b duplicate entry**
但是在这里我可以忽略这个错误,选择这个值的ID,然后使用这个ID:
select ID from table where a = 'a' and b = 'b'
insert into brother( table ) values (ID)
最后我可以提交程序。如果我需要 ID,请注意此错误与回滚无关。
问题是:什么样的错误会让我回滚过程???
我希望你能理解。
For example:
insert into table( a, b ) values ('a','b') could generate the following error:
**a-b duplicate entry**
BUT here I can ignore this error selecting the ID of this values, then use this ID:
select ID from table where a = 'a' and b = 'b'
insert into brother( table ) values (ID)
Finally I could COMMIT the PROCEDURE. Look that this error isn't relevant for rollback if I need the ID.
The question is: what kind of errors will doing me to ROLLBACK the PROCEDURE???
I hope you understand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你是在问,“INSERT 语句会导致什么样的错误,从而使 MySQL 回滚事务?”
违反任何约束的 INSERT 将导致回滚。它可以是您概述的外键约束,但也可以是 UNIQUE 约束或 CHECK 约束。 (CHECK 约束可能会在 MySQL 中实现为触发器。)
尝试插入无效值(不可空列中的 NULL、超出范围的数字、无效日期)可能会导致回滚。但它们可能不会,具体取决于服务器配置。 (请参阅下面的链接。)
INSERT 也可能因缺乏权限而失败。这也会导致回滚。
某些在其他平台上会导致回滚的情况不会在 MySQL 上导致回滚。
该引用来自MySQL 如何处理约束。
MySQL 文档中我最喜欢的引用之一,1.8.6.2 。对无效数据的限制。
这不是可爱吗?
I think you're asking, "What kind of errors can an INSERT statement cause that will make MySQL rollback a transaction?"
An INSERT that violates any constraint will cause a rollback. It could be foreign key constraint like you've outlined, but it could also be a UNIQUE constraint, or a CHECK constraint. (A CHECK constraint would probably be implemented as a trigger in MySQL.)
Trying to insert values that aren't valid (NULL in nonnullable columns, numbers that are out of range, invalid dates) might cause a rollback. But they might not, depending on the server configuration. (See link below.)
An INSERT can also fail due because it lacks permissions. That will also cause a rollback.
Some conditions that would cause a rollback on other platforms don't cause a rollback on MySQL.
That quote is from How MySQL Deals with Constraints.
One of my favorite quotes from the MySQL documentation, 1.8.6.2. Constraints on Invalid Data.
Isn't that cute?