在 mysql 中,直到 mysql 8.0.16 版本之前,标准 sql 的 check 约束并未被完全支持。然而,从 mysql 8.0.16 开始,check 约束在表定义中得到了支持,允许你定义列中必须满足的条件。
使用 check 约束,你可以确保在插入或更新记录时,某列或某组列的值满足特定的条件。
以下是如何在 mysql 中使用 check 约束
1、mysql5.7.x check 约束
mysql5.7.x check 约束是无效的,只做检查check ,不强制check
1.1、测试用例
在这个示例中,我们创建了一个名为 t_check 的表,其中有一个c_age 列。check约束确保c_age 列的值在大于等于18。
select @@version 检查mysql版本信息
mysql> select @@version; +------------+ | @@version | +------------+ | 5.7.28-log | +------------+ 1 row in set (0.00 sec) mysql> status -------------- mysql ver 14.14 distrib 5.7.28, for linux-glibc2.12 (x86_64) using editline wrapper connection id: 2 current database: superdb current user: root@localhost ssl: not in use current pager: stdout using outfile: '' using delimiter: ; server version: 5.7.28-log mysql community server (gpl) protocol version: 10 connection: localhost via unix socket server characterset: utf8 db characterset: utf8 client characterset: utf8 conn. characterset: utf8 unix socket: /tmp/mysql.sock uptime: 6 min 59 sec threads: 1 questions: 18 slow queries: 0 opens: 111 flush tables: 1 open tables: 26 queries per second avg: 0.042 --------------
create table t_check ( id int not null primary key, c_name varchar(32), c_age int check(c_age>=18) ); -- insert mysql> insert into t_check values(1,'column_check_001',18); query ok, 1 row affected (0.04 sec) mysql> insert into t_check values(2,'column_check_002',17); query ok, 1 row affected (0.00 sec) mysql> insert into t_check values(3,'column_check_003',30); query ok, 1 row affected (0.00 sec) mysql> commit; query ok, 0 rows affected (0.00 sec) mysql> select * from t_check; +----+------------------+-------+ | id | c_name | c_age | +----+------------------+-------+ | 1 | column_check_001 | 18 | | 2 | column_check_002 | 17 | | 3 | column_check_003 | 30 | +----+------------------+-------+ 3 rows in set (0.00 sec)
2、mysql8.x check 约束
mysql8.0.x check是有效的,做检查check ,强制check
2.1、创建表t_check,插入满足 check 约束的数据
在这个示例中,我们创建了一个名为 t_check 的表,其中有一个c_age 列。check约束确保c_age 列的值在大于等于18。
mysql> create table t_check -> ( id int not null primary key, -> c_name varchar(32), -> c_age int check(c_age>=18) -> ); query ok, 0 rows affected (0.15 sec) mysql> insert into t_check values(1,'column_check_001',18); query ok, 1 row affected (0.01 sec)
2.2、尝试插入不满足 check 约束的数据:
mysql> insert into t_check values(2,'column_check_002',17); error 3819 (hy000): check constraint 't_check_chk_1' is violated.
2.3、插入满足 check 约束的数据
mysql> insert into t_check values(3,'column_check_003',30); query ok, 1 row affected (0.00 sec) mysql> commit; query ok, 0 rows affected (0.02 sec) mysql> select * from t_check; +----+------------------+-------+ | id | c_name | c_age | +----+------------------+-------+ | 1 | column_check_001 | 18 | | 3 | column_check_003 | 30 | +----+------------------+-------+ 2 rows in set (0.00 sec)
2.4、给已经创建好的表增加约束
create table t_check2 ( id int not null primary key, c_name varchar(32), c_age int ); alter table t_check2 add constraint check(c_age>=18); insert into t_check2 values(1,'column_check_001',18); insert into t_check2 values(2,'column_check_002',17); insert into t_check2 values(3,'column_check_003',30); commit; select * from t_check2;
同上用例测试 mysql8.0 ,check有效检查并强制约束,执行效果如下
mysql> insert into t_check2 values(2,'column_check_002',17); error 3819 (hy000): check constraint 't_check2_chk_1' is violated.
3、约束的相关查询及管理
3.1、8.0.x 视图information_schema.check_constraints
select * from information_schema.check_constraints ;
mysql> select * from information_schema.check_constraints ; +--------------------+-------------------+-----------------+-----------------+ | constraint_catalog | constraint_schema | constraint_name | check_clause | +--------------------+-------------------+-----------------+-----------------+ | def | db01 | t_check2_chk_1 | (`c_age` >= 18) | +--------------------+-------------------+-----------------+-----------------+
select * from information_schema.table_constraints
where constraint_schema=‘superdb’
and table_name=‘t_check2’;
mysql> select * from information_schema.table_constraints -> where constraint_schema='superdb' -> and table_name='t_check2'; +--------------------+-------------------+-----------------+--------------+------------+-----------------+----------+ | constraint_catalog | constraint_schema | constraint_name | table_schema | table_name | constraint_type | enforced | +--------------------+-------------------+-----------------+--------------+------------+-----------------+----------+ | def | superdb | primary | superdb | t_check2 | primary key | yes | | def | superdb | t_check2_chk_1 | superdb | t_check2 | check | yes | +--------------------+-------------------+-----------------+--------------+------------+-----------------+----------+ 2 rows in set (0.00 sec)
3.2、查看表的主键约束信息
select * from information_schema.key_column_usage kcu
where constraint_schema=‘superdb’
and table_name=‘t_check’;
mysql> select * from information_schema.key_column_usage kcu -> where constraint_schema='superdb' -> and table_name='t_check'; +--------------------+-------------------+-----------------+---------------+--------------+------------+-------------+------------------+-------------------------------+-------------------------+-----------------------+------------------------+ | constraint_catalog | constraint_schema | constraint_name | table_catalog | table_schema | table_name | column_name | ordinal_position | position_in_unique_constraint | referenced_table_schema | referenced_table_name | referenced_column_name | +--------------------+-------------------+-----------------+---------------+--------------+------------+-------------+------------------+-------------------------------+-------------------------+-----------------------+------------------------+ | def | superdb | primary | def | superdb | t_check | id | 1 | null | null | null | null | +--------------------+-------------------+-----------------+---------------+--------------+------------+-------------+------------------+-------------------------------+-------------------------+---
3.3、查看表的外键约束信息
select * from information_schema.referential_constraints rc
where constraint_schema=‘superdb’
and table_name=‘t_check’;
mysql> select * from information_schema.referential_constraints rc -> where constraint_schema='superdb' -> and table_name='t_check'; empty set (0.00 sec) -- 因没有外键约束,查出为空
4、 删除 check 约束:
要删除一个 check 约束,你需要知道它的名字(如果在创建时指定了的话)。但如果你没有指定名字,你可能需要删除整个表并重新创建它,或者使用其他方法(如触发器)来模拟 check 约束的行为。
mysql> alter table t_check2 drop constraint t_check2_chk_1; query ok, 0 rows affected (0.02 sec) records: 0 duplicates: 0 warnings: 0
再次插入之前不满足条件的数据,则执行成功
mysql> insert into t_check2 values(2,'column_check_002',17); query ok, 1 row affected (0.00 sec) mysql> commit; query ok, 0 rows affected (0.00 sec) mysql> select * from t_check2; +----+------------------+-------+ | id | c_name | c_age | +----+------------------+-------+ | 1 | column_check_001 | 18 | | 2 | column_check_002 | 17 | | 3 | column_check_003 | 30 | +----+------------------+-------+ 3 rows in set (0.00 sec)
5、在多个列上使用 check 约束:
你也可以在多个列上使用 check 约束,例如:
create table t_students ( id int auto_increment primary key, age int, grade char(1), check (age >= 0 and age <= 100), check (grade in ('a', 'b', 'c', 'd', 'f')) );
在这个示例中,我们添加了一个额外的 check 约束来确保 grade 列的值是 ‘a’、‘b’、‘c’、‘d’ 或 ‘f’ 中的一个。
到此这篇关于mysql check约束(5.7和8.0)的使用的文章就介绍到这了,更多相关mysql check约束内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论