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

1.7.3.1 PRIMARY KEY and UNIQUE Index ConstraintsPRIMARY KEY和UNIQUE索引约束
1.7.3.2 FOREIGN KEY Constraints约束
1.7.3.3 Enforced Constraints on Invalid Data对无效数据的强制约束
1.7.3.4 ENUM and SET ConstraintsENUM和SET约束

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. MySQL使您既可以处理允许回滚的事务表,也可以处理不允许回滚的非事务表。正因为如此,MySQL中的约束处理与其他DBMS中的有点不同。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.当您在一个非事务表中插入或更新了许多行,而当发生错误时,这些行的更改无法回滚时,我们必须处理这种情况。

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 Server在解析要执行的语句时,会尝试为其检测到的任何错误生成错误,并尝试从执行语句时发生的任何错误中恢复。我们在大多数情况下都这样做,但还不是所有人都这样做。

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. 当发生错误时,MySQL的选项是在中间停止语句,或者尽可能地从问题中恢复并继续。默认情况下,服务器遵循后一个过程。This means, for example, that the server may coerce invalid values to the closest valid values.这意味着,例如,服务器可能会将无效值强制为最接近的有效值。

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. 有几个SQL模式选项可用于更好地控制错误数据值的处理,以及在发生错误时是继续执行语句还是中止语句。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. 使用这些选项,您可以将MySQL Server配置为以更传统的方式运行,就像其他拒绝不正确输入的DBMS一样。SQL模式可以在服务器启动时全局设置,以影响所有客户端。Individual clients can set the SQL mode at runtime, which enables each client to select the behavior most appropriate for its requirements. 单个客户端可以在运行时设置SQL模式,这使每个客户端能够选择最适合其需求的行为。See Section 5.1.11, “Server SQL Modes”.请参阅第5.1.11节,“服务器SQL模式”

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

1.7.3.1 PRIMARY KEY and UNIQUE Index Constraints
1.7.3.2 FOREIGN KEY Constraints
1.7.3.3 Enforced Constraints on Invalid Data
1.7.3.4 ENUM and SET Constraints