前言
在mysql 8.0版本中,引入了一個(gè)非常有用的新特性 — 檢查性約束,它可以提高對非法或不合理數(shù)據(jù)寫入的控制能力;接下來我們就來詳細(xì)了解一下。
檢查性約束
創(chuàng)建、刪除與查看
(1)可以在建表時(shí),創(chuàng)建檢查性約束
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)可以通過查詢表結(jié)構(gòu)的方式,查看檢查性約束
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)也可以通過下面兩個(gè)視圖查看,其中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)當(dāng)插入不符合檢查性約束的數(shù)據(jù)時(shí),會(huì)直接報(bào)錯(cuò)
1
2
|
mysql> insert into t1 values (0,0,0); error 3819 (hy000): check constraint 'c1_nonzero' is violated. |
限制
(1)自增列和其他表的列,不支持檢查性約束
(2)不確定的函數(shù),如connection_id(),current_user(),now()等,不支持檢查性約束
(3)用戶自定義函數(shù),不支持檢查性約束
(4)存儲(chǔ)過程,不支持檢查性約束
(5)變量,不支持檢查性約束
(6)子查詢,不支持檢查性約束
總結(jié)
檢查性約束,還是一個(gè)非常不錯(cuò)的功能,可以實(shí)現(xiàn)豐富的數(shù)據(jù)校驗(yàn)場景,大家可以嘗試一下。
以上就是mysql 8.0新特性 — 檢查性約束的簡單介紹的詳細(xì)內(nèi)容,更多關(guān)于mysql 8.0新特性 — 檢查性約束的資料請關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://cloud.tencent.com/developer/inventory/2098/article/1762645