ROLLBACK 的 SQL 查询中存在哪些类型的错误?

发布于 2024-10-10 18:38:13 字数 382 浏览 2 评论 0原文

例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨巷深深 2024-10-17 18:38:13

我想你是在问,“INSERT 语句会导致什么样的错误,从而使 My​​SQL 回滚事务?”

违反任何约束的 INSERT 将导致回滚。它可以是您概述的外键约束,但也可以是 UNIQUE 约束或 CHECK 约束。 (CHECK 约束可能会在 MySQL 中实现为触发器。)

尝试插入无效值(不可空列中的 NULL、超出范围的数字、无效日期)可能会导致回滚。但它们可能不会,具体取决于服务器配置。 (请参阅下面的链接。)

INSERT 也可能因缺乏权限而失败。这也会导致回滚。

某些在其他平台上会导致回滚的情况不会在 MySQL 上导致回滚。

MySQL 出错时的选项
发生是为了停止该语句
中间或恢复以及
可能从问题和
继续。默认情况下,服务器
遵循后一种路线。这意味着,
例如,服务器可以
将非法值强制到最接近的值
法律价值。

该引用来自MySQL 如何处理约束

MySQL 文档中我最喜欢的引用之一,1.8.6.2 。对无效数据的限制

MySQL 使您能够存储某些
DATE 和中的日期值不正确
DATETIME 列(例如“2000-02-31”
或“2000-02-00”)。这个想法是,它
不是 SQL Server 的工作
验证日期。

这不是可爱吗?

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.

The options MySQL has when an error
occurs are to stop the statement in
the middle or to recover as well as
possible from the problem and
continue. By default, the server
follows the latter course. This means,
for example, that the server may
coerce illegal values to the closest
legal values.

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.

MySQL enables you to store certain
incorrect date values into DATE and
DATETIME columns (such as '2000-02-31'
or '2000-02-00'). The idea is that it
is not the job of the SQL server to
validate dates.

Isn't that cute?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文