当前位置:数据库 > > 正文

mysql8.0使用(MySQL 8.0新特性 — 检查性约束的使用简介)

时间:2021-10-16 00:39:29类别:数据库

mysql8.0使用

MySQL 8.0新特性 — 检查性约束的使用简介

前言

在mysql 8.0版本中,引入了一个非常有用的新特性 — 检查性约束,它可以提高对非法或不合理数据写入的控制能力;接下来我们就来详细了解一下。

检查性约束

创建、删除与查看

(1)可以在建表时,创建检查性约束

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • mysql> 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)
  •  -> );
  • query ok, 0 rows affected (0.03 sec)
  • (2)也可以通过下列语句,新增检查性约束

  • ?
  • 1
  • 2
  • 3
  • mysql> alter table t1 add constraint c3_nonzero check ((c3<>0));
  • query ok, 0 rows affected (0.16 sec)
  • records: 0 duplicates: 0 warnings: 0
  • (3)可以通过下列语句,删除检查性约束

  • ?
  • 1
  • 2
  • 3
  • mysql> alter table t1 drop constraint c3_nonzero;
  • query ok, 0 rows affected (0.02 sec)
  • records: 0 duplicates: 0 warnings: 0
  • (4)可以通过查询表结构的方式,查看检查性约束

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • mysql> show create table t1\g
  • *************************** 1. row ***************************
  •  table: t1
  • create table: create table `t1` (
  •  `c1` int default null,
  •  `c2` int default null,
  •  `c3` int 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
  • 1 row in set (0.00 sec)
  • (5)也可以通过下面两个视图查看,其中table_constraints查询表存在哪些约束,check_constraints查询检查性约束的具体定义

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • mysql> select * from information_schema.table_constraints where table_name='t1';
  • +--------------------+-------------------+-----------------+--------------+------------+-----------------+----------+
  • | constraint_catalog | constraint_schema | constraint_name | table_schema | table_name | constraint_type | enforced |
  • +--------------------+-------------------+-----------------+--------------+------------+-----------------+----------+
  • | def | test | c1_nonzero | test | t1 | check | yes |
  • | def | test | c2_positive | test | t1 | check | yes |
  • | def | test | t1_chk_1 | test | t1 | check | yes |
  • | def | test | t1_chk_2 | test | t1 | check | yes |
  • | def | test | t1_chk_3 | test | t1 | check | yes |
  • | def | test | t1_chk_4 | test | t1 | check | yes |
  • +--------------------+-------------------+-----------------+--------------+------------+-----------------+----------+
  • 6 rows in set (0.00 sec)
  •  
  • mysql> select * from information_schema.check_constraints where constraint_name='c1_nonzero';
  • +--------------------+-------------------+-----------------+--------------+
  • | constraint_catalog | constraint_schema | constraint_name | check_clause |
  • +--------------------+-------------------+-----------------+--------------+
  • | def | test | c1_nonzero | (`c1` <> 0) |
  • +--------------------+-------------------+-----------------+--------------+
  • 1 row in set (0.00 sec)
  • (6)当插入不符合检查性约束的数据时,会直接报错

  • ?
  • 1
  • 2
  • mysql> insert into t1 values(0,0,0);
  • error 3819 (hy000): check constraint 'c1_nonzero' is violated.
  • 限制

    (1)自增列和其他表的列,不支持检查性约束

    (2)不确定的函数,如connection_id(),current_user(),now()等,不支持检查性约束

    (3)用户自定义函数,不支持检查性约束

    (4)存储过程,不支持检查性约束

    (5)变量,不支持检查性约束

    (6)子查询,不支持检查性约束

    总结

    检查性约束,还是一个非常不错的功能,可以实现丰富的数据校验场景,大家可以尝试一下。

    以上就是mysql 8.0新特性 — 检查性约束的简单介绍的详细内容,更多关于mysql 8.0新特性 — 检查性约束的资料请关注开心学习网其它相关文章!

    原文链接:https://cloud.tencent.com/developer/inventory/2098/article/1762645

    上一篇下一篇

    猜您喜欢

    热门推荐