8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

1.7.3 MySQL 如何处理约束1.7.3 How MySQL Deals with Constraints

MySQL 使您能够使用允许回滚的事务表和不允许回滚的非事务表。因此,MySQL 中的约束处理与其他 DBMS 中的约束处理有些不同。我们必须处理这样的情况:您在非事务性表中插入或更新了很多行,而这些行在发生错误时无法回滚更改。MySQL enables you to work both with transactional tables that permit rollback and with nontransactional tables that do not. Because of this, constraint handling is a bit different in MySQL than in other DBMSs. We must handle the case when you have inserted or updated a lot of rows in a nontransactional table for which changes cannot be rolled back when an error occurs.

基本原理是,MySQL Server 尝试在解析要执行的语句时对其可以检测到的任何内容产生错误,并尝试从执行该语句时发生的任何错误中恢复。我们在大多数情况下都会这样做,但还不是在所有情况下都这样做。The basic philosophy is that MySQL Server tries to produce an error for anything that it can detect while parsing a statement to be executed, and tries to recover from any errors that occur while executing the statement. We do this in most cases, but not yet for all.

发生错误时,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 invalid values to the closest valid values.

可以使用多个 SQL 模式选项来更好地控制错误数据值的处理以及在发生错误时是否继续语句执行或中止。使用这些选项,您可以将 MySQL Server 配置为以更传统的方式运行,就像其他 DBMS 拒绝不正确的输入一样。 SQL 模式可以在服务器启动时全局设置以影响所有客户端。各个客户端可以在运行时设置 SQL 模式,这使每个客户端能够选择最适合其需求的行为。请参阅第 7.1.11 节“服务器 SQL 模式”。Several SQL mode options are available to provide greater control over handling of bad data values and whether to continue statement execution or abort when errors occur. Using these options, you can configure MySQL Server to act in a more traditional fashion that is like other DBMSs that reject improper input. The SQL mode can be set globally at server startup to affect all clients. Individual clients can set the SQL mode at runtime, which enables each client to select the behavior most appropriate for its requirements. See Section 7.1.11, “Server SQL Modes”.

以下各节介绍 MySQL 服务器如何处理不同类型的约束。The following sections describe how MySQL Server handles different types of constraints.