Prior to MySQL 8.0.16, 在MySQL 8.0.16之前,CREATE TABLE
permits only the following limited version of table CHECK
constraint syntax, which is parsed and ignored:CREATE TABLE
仅允许以下有限版本的表检查约束语法,该语法将被解析并忽略:
CHECK (expr
)
As of MySQL 8.0.16, 从MySQL 8.0.16开始,对于所有存储引擎,CREATE TABLE
permits the core features of table and column CHECK
constraints, for all storage engines. 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 如果省略,MySQL将根据表名、文本_chk_
, and an ordinal number (1, 2, 3, ...). _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
将约束条件指定为布尔表达式,对于表的每一行,该表达式的计算结果必须为TRUE
或UNKNOWN
(对于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:可选强制子句指示是否强制执行约束:
If omitted or specified as 如果省略或指定为ENFORCED
, the constraint is created and enforced.ENFORCED
,将创建并强制约束。
If specified as 如果指定为NOT ENFORCED
, the constraint is created but not enforced.NOT ENFORCED
,则会创建约束,但不会强制。
A CHECK
constraint is specified as either a table constraint or column constraint:CHECK
约束指定为表约束或列约束:
A table constraint does not appear within a column definition and can refer to any table column or columns. 表约束不出现在列定义中,可以引用任何表列。Forward references are permitted to columns appearing later in the table definition.允许对表定义后面出现的列进行正向引用。
A column constraint appears within a column definition and can refer only to that column.列约束出现在列定义中,并且只能引用该列。
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:定义包括命名和未命名格式的表约束和列约束:
The first constraint is a table constraint: It occurs outside any column definition, so it can (and does) refer to multiple table columns. 第一个约束是表约束:它出现在任何列定义之外,因此它可以(并且确实)引用多个表列。This constraint contains forward references to columns not defined yet. 此约束包含对尚未定义的列的正向引用。No constraint name is specified, so MySQL generates a name.没有指定约束名称,因此MySQL生成一个名称。
The next three constraints are column constraints: Each occurs within a column definition, and thus can refer only to the column being defined. 接下来的三个约束是列约束:每个约束都出现在列定义中,因此只能引用正在定义的列。One of the constraints is named explicitly. 其中一个约束被显式命名。MySQL generates a name for each of the other two.MySQL为其他两个数据库分别生成一个名称。
The last two constraints are table constraints. 最后两个约束是表约束。One of them is named explicitly. 其中一个是显式命名的。MySQL generates a name for the other one.MySQL为另一个生成一个名称。
As mentioned, MySQL generates a name for any 如前所述,MySQL为未指定的任何CHECK
constraint specified without one. 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.如果表达式包含不允许的构造,则会发生错误。
Nongenerated and generated columns are permitted, except columns with the 允许使用非生成列和生成列,但具有AUTO_INCREMENT
attribute and columns in other tables.AUTO_INCREMENT
属性的列和其他表中的列除外。
Literals, deterministic built-in functions, and operators are permitted. 允许使用文字、确定性内置函数和运算符。A function is deterministic if, given the same data in tables, multiple invocations produce the same result, independently of the connected user. 如果给定表中相同的数据,多个调用产生相同的结果,而与连接的用户无关,则函数是确定的。Examples of functions that are nondeterministic and fail this definition: 不确定且未通过此定义的函数示例:CONNECTION_ID()
, CURRENT_USER()
, NOW()
.CONNECTION_ID()
、CURRENT_USER()
、NOW()
。
Stored functions and loadable functions are not permitted.不允许使用存储函数和可加载函数。
Stored procedure and function parameters are not permitted.不允许使用存储过程和函数参数。
Variables (system variables, user-defined variables, and stored program local variables) are not permitted.不允许使用变量(系统变量、用户定义变量和存储程序局部变量)。
Subqueries are not permitted.不允许子查询。
Foreign key referential actions (在检查约束中使用的列上禁止外键引用操作(ON UPDATE
, ON DELETE
) are prohibited on columns used in CHECK
constraints. ON UPDATE
、ON 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
. INSERT
、UPDATE
、REPLACE
、LOAD DATA
和LOAD 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 IGNORE
、UPDATE IGNORE
、LOAD DATA ... IGNORE
和LOAD 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.如果类型转换失败或导致精度损失,则会发生错误。
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节,“信息模式检查约束表”。