13.1.20.6 CHECK Constraints检查约束

Prior to MySQL 8.0.16, CREATE TABLE permits only the following limited version of table CHECK constraint syntax, which is parsed and ignored:在MySQL 8.0.16之前,CREATE TABLE仅允许以下有限版本的表检查约束语法,该语法将被解析并忽略:

CHECK (expr)

As of MySQL 8.0.16, CREATE TABLE permits the core features of table and column CHECK constraints, for all storage engines. 从MySQL 8.0.16开始,对于所有存储引擎,CREATE TABLE允许表和列CHECK约束的核心功能。CREATE TABLE permits the following CHECK constraint syntax, for both table constraints and column constraints:对于表约束和列约束,CREATE TABLE允许使用以下检查约束语法:

[CONSTRAINT [symbol]] CHECK (expr) [[NOT] ENFORCED]

The optional symbol specifies a name for the constraint. 可选symbol指定约束的名称。If omitted, MySQL generates a name from the table name, a literal _chk_, and an ordinal number (1, 2, 3, ...). 如果省略,MySQL将根据表名、文本_chk_和序号(1、2、3,…)生成一个名称。Constraint names have a maximum length of 64 characters. 约束名称的最大长度为64个字符。They are case-sensitive, but not accent-sensitive.它们区分大小写,但不区分重音。

expr specifies the constraint condition as a boolean expression that must evaluate to TRUE or UNKNOWN (for NULL values) for each row of the table. expr将约束条件指定为布尔表达式,对于表的每一行,该表达式的计算结果必须为TRUEUNKNOWN(对于NULL值)。If the condition evaluates to FALSE, it fails and a constraint violation occurs. 如果条件的计算结果为FALSE,则它将失败并发生约束冲突。The effect of a violation depends on the statement being executed, as described later in this section.冲突的效果取决于正在执行的语句,如本节后面所述。

The optional enforcement clause indicates whether the constraint is enforced:可选强制子句指示是否强制执行约束:

A CHECK constraint is specified as either a table constraint or column constraint:CHECK约束指定为表约束或列约束:

Consider this table definition:考虑这个表定义:

CREATE TABLE t1
(
  CHECK (c1 <> c2),
  c1 INT CHECK (c1 > 10),
  c2 INT CONSTRAINT c2_positive CHECK (c2 > 0),
  c3 INT CHECK (c3 < 100),
  CONSTRAINT c1_nonzero CHECK (c1 <> 0),
  CHECK (c1 > c3)
);

The definition includes table constraints and column constraints, in named and unnamed formats:定义包括命名和未命名格式的表约束和列约束:

As mentioned, MySQL generates a name for any CHECK constraint specified without one. 如前所述,MySQL为未指定的任何CHECK约束生成名称。To see the names generated for the preceding table definition, use SHOW CREATE TABLE:要查看为前面的表定义生成的名称,请使用SHOW CREATE TABLE

mysql> SHOW CREATE TABLE t1\G
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `c1` int(11) DEFAULT NULL,
  `c2` int(11) DEFAULT NULL,
  `c3` int(11) DEFAULT NULL,
  CONSTRAINT `c1_nonzero` CHECK ((`c1` <> 0)),
  CONSTRAINT `c2_positive` CHECK ((`c2` > 0)),
  CONSTRAINT `t1_chk_1` CHECK ((`c1` <> `c2`)),
  CONSTRAINT `t1_chk_2` CHECK ((`c1` > 10)),
  CONSTRAINT `t1_chk_3` CHECK ((`c3` < 100)),
  CONSTRAINT `t1_chk_4` CHECK ((`c1` > `c3`))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

The SQL standard specifies that all types of constraints (primary key, unique index, foreign key, check) belong to the same namespace. SQL标准指定所有类型的约束(主键、唯一索引、外键、检查)都属于同一名称空间。In MySQL, each constraint type has its own namespace per schema (database). 在MySQL中,每个约束类型在每个模式(数据库)中都有自己的名称空间。Consequently, CHECK constraint names must be unique per schema; no two tables in the same schema can share a CHECK constraint name. 因此,每个模式的CHECK约束名称必须是唯一的;同一架构中的两个表不能共享CHECK约束名称。(Exception: A TEMPORARY table hides a non-TEMPORARY table of the same name, so it can have the same CHECK constraint names as well.)(例外:临时表隐藏同名的非临时表,因此它也可以具有相同的CHECK约束名称。)

Beginning generated constraint names with the table name helps ensure schema uniqueness because table names also must be unique within the schema.以表名开头生成的约束名称有助于确保模式的唯一性,因为表名在模式中也必须是唯一的。

CHECK condition expressions must adhere to the following rules. CHECK条件表达式必须遵守以下规则。An error occurs if an expression contains disallowed constructs.如果表达式包含不允许的构造,则会发生错误。

Foreign key referential actions (ON UPDATE, ON DELETE) are prohibited on columns used in CHECK constraints. 在检查约束中使用的列上禁止外键引用操作(ON UPDATEON DELETE)。Likewise, CHECK constraints are prohibited on columns used in foreign key referential actions.同样,在外键引用操作中使用的列上禁止使用CHECK约束。

CHECK constraints are evaluated for INSERT, UPDATE, REPLACE, LOAD DATA, and LOAD XML statements and an error occurs if a constraint evaluates to FALSE. INSERTUPDATEREPLACELOAD DATALOAD XML语句计算CHECK约束,如果约束的计算结果为FALSE,则会发生错误。If an error occurs, handling of changes already applied differs for transactional and nontransactional storage engines, and also depends on whether strict SQL mode is in effect, as described in Strict SQL Mode.如果发生错误,事务性和非事务性存储引擎对已应用更改的处理会有所不同,还取决于严格SQL模式是否有效,如严格SQL模式中所述。

CHECK constraints are evaluated for INSERT IGNORE, UPDATE IGNORE, LOAD DATA ... IGNORE, and LOAD XML ... IGNORE statements and a warning occurs if a constraint evaluates to FALSE. INSERT IGNOREUPDATE IGNORELOAD DATA ... IGNORELOAD XML ... IGNORE语句评估CHECK约束,如果约束的计算结果为FALSE,则会出现警告。The insert or update for any offending row is skipped.跳过任何有问题行的插入或更新。

If the constraint expression evaluates to a data type that differs from the declared column type, implicit coercion to the declared type occurs according to the usual MySQL type-conversion rules. 如果约束表达式的计算结果为与声明的列类型不同的数据类型,则会根据通常的MySQL类型转换规则对声明的类型进行隐式强制。See Section 12.3, “Type Conversion in Expression Evaluation”. 请参阅第12.3节,“表达式计算中的类型转换”If type conversion fails or results in a loss of precision, an error occurs.如果类型转换失败或导致精度损失,则会发生错误。

Note注意

Constraint expression evaluation uses the SQL mode in effect at evaluation time. 约束表达式求值使用求值时有效的SQL模式。If any component of the expression depends on the SQL mode, different results may occur for different uses of the table unless the SQL mode is the same during all uses.如果表达式的任何组件依赖于SQL模式,则表的不同使用可能会产生不同的结果,除非SQL模式在所有使用过程中都是相同的。

The INFORMATION_SCHEMA.CHECK_CONSTRAINTS table provides information about CHECK constraints defined on tables. INFORMATION_SCHEMA.CHECK_CONSTRAINTS表提供了有关表上定义的CHECK约束的信息。See Section 26.3.5, “The INFORMATION_SCHEMA CHECK_CONSTRAINTS Table”.请参阅第26.3.5节,“信息模式检查约束表”