Skip to content

Latest commit

 

History

History
53 lines (42 loc) · 1.54 KB

information-schema-check-constraints.md

File metadata and controls

53 lines (42 loc) · 1.54 KB
title summary
CHECK_CONSTRAINTS
了解 INFORMATION_SCHEMA 表 `CHECK_CONSTRAINTS`。

CHECK_CONSTRAINTS

CHECK_CONSTRAINTS 表提供关于表上 CHECK 约束的信息。

USE INFORMATION_SCHEMA;
DESC CHECK_CONSTRAINTS;

命令输出如下:

+--------------------+-------------+------+-----+---------+-------+
| Field              | Type        | Null | Key | Default | Extra |
+--------------------+-------------+------+-----+---------+-------+
| CONSTRAINT_CATALOG | varchar(64) | NO   |     | NULL    |       |
| CONSTRAINT_SCHEMA  | varchar(64) | NO   |     | NULL    |       |
| CONSTRAINT_NAME    | varchar(64) | NO   |     | NULL    |       |
| CHECK_CLAUSE       | longtext    | NO   |     | NULL    |       |
+--------------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

下述示例使用 CREATE TABLE 语句添加 CHECK 约束:

SET GLOBAL tidb_enable_check_constraint = ON;
CREATE TABLE test.t1 (id INT PRIMARY KEY, CHECK (id%2 = 0));
SELECT * FROM CHECK_CONSTRAINTS\G

命令输出如下:

*************************** 1. row ***************************
CONSTRAINT_CATALOG: def
 CONSTRAINT_SCHEMA: test
   CONSTRAINT_NAME: t1_chk_1
      CHECK_CLAUSE: (`id` % 2 = 0)
1 row in set (0.00 sec)

CHECK_CONSTRAINTS 表的字段描述如下:

  • CONSTRAINT_CATALOG:约束的目录,始终为 def
  • CONSTRAINT_SCHEMA:约束的库名。
  • CONSTRAINT_NAME:约束的名字。
  • CHECK_CLAUSE:检查约束的子句。