一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數(shù)據(jù)庫技術(shù)|

服務(wù)器之家 - 數(shù)據(jù)庫 - Mysql - MySQL 8.0新特性 — 檢查性約束的使用簡介

MySQL 8.0新特性 — 檢查性約束的使用簡介

2021-04-26 17:10brightdeng@DBA Mysql

這篇文章主要介紹了MySQL 8.0新特性 — 檢查性約束的簡單介紹,幫助大家更好的理解和學(xué)習(xí)使用MySQL數(shù)據(jù)庫,感興趣的朋友可以了解下

前言

在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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: vomoulei成人舞蹈 | 四虎tv在线观看884aa | 天天狠天天透 | 国产亚洲精品美女久久久 | 色综久久天天综合绕视看 | 成人福利在线观看 | 亚洲欧洲日产国码 最新 | 91资源站| 久9视频这里只有精品123 | 1024免费福利永久观看网站 | 国产久视频 | 人妖欧美一区二区三区四区 | 日本色吧 | 日本久久影视 | segou视频在线观看 | 亚洲乱码一二三四区国产 | 免费老外的毛片清高 | 成人性爱视频在线观看 | 精品久久久久国产免费 | 欧美黑人一级片 | 四虎影业 | 亚洲v成人天堂影视 | 无码人妻丰满熟妇啪啪网不卡 | 久久婷婷丁香五月色综合啪免费 | 日本高清视频网址 | 236宅宅2021最新理论 | 男人边吃奶边做好爽视频免费 | yy8090韩国日本三理论免费 | 亚洲国产精品无码中文在线 | 日韩在线一区二区三区 | 欧美人禽杂交狂配无删完整 | 99热这里只有精品免费 | 吃瓜视频在线观看 | 精品无人区乱码1区2区3区在线 | 久久精品视频uu | 星星动漫无删减在线观看 | 日韩在线视频免费观看 | 美女张开下身让男人桶 | 欧洲破处| 教练你好大轻点漫 | 福利片免费一区二区三区 |