主頁 > 知識(shí)庫 > mysql完整性約束實(shí)例詳解

mysql完整性約束實(shí)例詳解

熱門標(biāo)簽:在哪里申請(qǐng)400電話 巫師三血與酒地圖標(biāo)注 外呼系統(tǒng)電銷受騙 400電話申請(qǐng)信用卡 銷售語音電話機(jī)器人 常州網(wǎng)絡(luò)外呼系統(tǒng)開發(fā) 走過哪個(gè)省地圖標(biāo)注 安徽ai電話電銷機(jī)器人有效果嗎 萊西市地圖標(biāo)注

本文實(shí)例講述了mysql完整性約束。分享給大家供大家參考,具體如下:

主要內(nèi)容

  • not null 與 default
  • unique
  • primary
  • auto_increment
  • foreign key

約束條件作用:用于保證數(shù)據(jù)的完整性和一致性

主要分為

PRIMARY KEY (PK)    #標(biāo)識(shí)該字段為該表的主鍵,可以唯一的標(biāo)識(shí)記錄
FOREIGN KEY (FK)    #標(biāo)識(shí)該字段為該表的外鍵
NOT NULL    #標(biāo)識(shí)該字段不能為空
UNIQUE KEY (UK)    #標(biāo)識(shí)該字段的值是唯一的,
AUTO_INCREMENT    #標(biāo)識(shí)該字段的值自動(dòng)增長(整數(shù)類型,而且為主鍵)
DEFAULT    #為該字段設(shè)置默認(rèn)值
UNSIGNED #無符號(hào)
ZEROFILL #使用0填充

unique

在mysql中稱為單列唯一

#例子1:
create table department(
  id int,
  name char(10) unique
);
mysql> insert into department values(1,'it'),(2,'it');
ERROR 1062 (23000): Duplicate entry 'it' for key 'name'
#例子2:
create table department(
  id int unique,
  name char(10) unique
);
insert into department values(1,'it'),(2,'sale');
#第二種創(chuàng)建unique的方式
create table department(
  id int,
  name char(10) ,
  unique(id),
  unique(name)
);
insert into department values(1,'it'),(2,'sale');

聯(lián)合唯一:只要兩列記錄,有一列不同,既符合聯(lián)合唯一的約束

# 創(chuàng)建services表
mysql> create table services(
  -> id int,
  -> ip char(15),
  -> port int,
  -> unique(id),
  -> unique(ip,port)
  -> );
Query OK, 0 rows affected (0.05 sec)
mysql> desc services;
+-------+----------+------+-----+---------+-------+
| Field | Type   | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | YES  | UNI | NULL    |       |
| ip    | char(15) | YES  | MUL | NULL    |       |
| port  | int(11) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.01 sec)
#聯(lián)合唯一,只要兩列記錄,有一列不同,既符合聯(lián)合唯一的約束
mysql> insert into services values
  -> (1,'192,168,11,23',80),
  -> (2,'192,168,11,23',81),
  -> (3,'192,168,11,25',80);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from services;
+------+---------------+------+
| id  | ip      | port |
+------+---------------+------+
|  1 | 192,168,11,23 |  80 |
|  2 | 192,168,11,23 |  81 |
|  3 | 192,168,11,25 |  80 |
+------+---------------+------+
3 rows in set (0.00 sec)
mysql> insert into services values (4,'192,168,11,23',80);
ERROR 1062 (23000): Duplicate entry '192,168,11,23-80' for key 'ip'

auto_increment

約束:約束的字段為自動(dòng)增長,約束的字段必須同時(shí)被key約束

不指定id,則自動(dòng)增長

# 創(chuàng)建student
create table student(
id int primary key auto_increment,
name varchar(20),
sex enum('male','female') default 'male'
);

mysql> desc student;
+-------+-----------------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra     |
+-------+-----------------------+------+-----+---------+----------------+
| id  | int(11)        | NO  | PRI | NULL  | auto_increment |
| name | varchar(20)      | YES |   | NULL  |        |
| sex  | enum('male','female') | YES |   | male  |        |
+-------+-----------------------+------+-----+---------+----------------+
rows in set (0.17 sec)
#插入記錄
mysql> insert into student(name) values ('老白'),('小白');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+--------+------+
| id | name  | sex |
+----+--------+------+
| 1 | 老白  | male |
| 2 | 小白  | male |
+----+--------+------+
rows in set (0.00 sec)

指定id的情況

mysql> insert into student values(4,'asb','female');
Query OK, 1 row affected (0.00 sec)
mysql> insert into student values(7,'wsb','female');
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+----+--------+--------+
| id | name  | sex  |
+----+--------+--------+
| 1 | 老白  | male  |
| 2 | 小白  | male  |
| 4 | asb  | female |
| 7 | wsb  | female |
+----+--------+--------+
rows in set (0.00 sec)
# 再次插入一條不指定id的記錄,會(huì)在之前的最后一條記錄繼續(xù)增長
mysql> insert into student(name) values ('大白');
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+----+--------+--------+
| id | name  | sex  |
+----+--------+--------+
| 1 | 老白  | male  |
| 2 | 小白  | male  |
| 4 | asb  | female |
| 7 | wsb  | female |
| 8 | 大白  | male  |
+----+--------+--------+
rows in set (0.00 sec)

對(duì)于自增的字段,在用delete刪除后,再插入值,該字段仍按照刪除前的位置繼續(xù)增長

mysql> delete from student;
Query OK, 5 rows affected (0.00 sec)
mysql> select * from student;
Empty set (0.00 sec)
mysql> select * from student;
Empty set (0.00 sec)
mysql> insert into student(name) values('ysb');
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+----+------+------+
| id | name | sex |
+----+------+------+
| 9 | ysb | male |
+----+------+------+
row in set (0.00 sec)
#應(yīng)該用truncate清空表,比起delete一條一條地刪除記錄,truncate是直接清空表,在刪除大表時(shí)用它
mysql> truncate student;
Query OK, 0 rows affected (0.03 sec)
mysql> insert into student(name) values('xiaobai');
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+----+---------+------+
| id | name  | sex |
+----+---------+------+
| 1 | xiaobai | male |
+----+---------+------+
row in set (0.00 sec)
mysql>
auto_increment_increment和 auto_increment_offset

查看可用的 開頭auto_inc的詞

mysql> show variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name      | Value |
+--------------------------+-------+
| auto_increment_increment | 1   |
| auto_increment_offset  | 1   |
+--------------------------+-------+
rows in set (0.02 sec)

# 步長auto_increment_increment,默認(rèn)為1
# 起始的偏移量auto_increment_offset, 默認(rèn)是1
# 設(shè)置步長 為會(huì)話設(shè)置,只在本次連接中有效
set session auto_increment_increment=5;
#全局設(shè)置步長 都有效。
set global auto_increment_increment=5;
# 設(shè)置起始偏移量
set global auto_increment_offset=3;

強(qiáng)調(diào):If the value of auto_increment_offset is greater than that of auto_increment_increment, the value of auto_increment_offset is ignored.
翻譯:如果auto_increment_offset的值大于auto_increment_increment的值,則auto_increment_offset的值會(huì)被忽略

設(shè)置完起始偏移量和步長之后,再次執(zhí)行show variables like'auto_inc%';

發(fā)現(xiàn)跟之前一樣,必須先exit,再登錄才有效。

mysql> show variables like'auto_inc%';
+--------------------------+-------+
| Variable_name      | Value |
+--------------------------+-------+
| auto_increment_increment | 5   |
| auto_increment_offset  | 3   |
+--------------------------+-------+
rows in set (0.00 sec)
#因?yàn)橹坝幸粭l記錄id=1
mysql> select * from student;
+----+---------+------+
| id | name  | sex |
+----+---------+------+
| 1 | xiaobai | male |
+----+---------+------+
row in set (0.00 sec)
# 下次插入的時(shí)候,從起始位置3開始,每次插入記錄id+5
mysql> insert into student(name) values('ma1'),('ma2'),('ma3');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+---------+------+
| id | name  | sex |
+----+---------+------+
| 1 | xiaobai | male |
| 3 | ma1   | male |
| 8 | ma2   | male |
| 13 | ma3   | male |
+----+---------+------+

清空表區(qū)分delete和truncate的區(qū)別:

delete from t1; #如果有自增id,新增的數(shù)據(jù),仍然是以刪除前的最后一樣作為起始。

truncate table t1;數(shù)據(jù)量大,刪除速度比上一條快,且直接從零開始。

foreign key

理解foreign key

 

如上圖如果一個(gè)公司有很多員工,每個(gè)員工都對(duì)應(yīng)一個(gè)部門,在填表的時(shí)候就會(huì)重復(fù)寫這些部門,太冗余了

我們可以將它們分離

 

此時(shí)有兩張表,一張是employee表,簡稱emp表(關(guān)聯(lián)表,也就從表)。一張是department表,簡稱dep表(被關(guān)聯(lián)表,也叫主表)。

#1.創(chuàng)建表時(shí)先創(chuàng)建被關(guān)聯(lián)表,再創(chuàng)建關(guān)聯(lián)表
# 先創(chuàng)建被關(guān)聯(lián)表(dep表)
create table dep(
  id int primary key,
  name varchar(20) not null,
  descripe varchar(20) not null
);
#再創(chuàng)建關(guān)聯(lián)表(emp表)
create table emp(
  id int primary key,
  name varchar(20) not null,
  age int not null,
  dep_id int,
  constraint fk_dep foreign key(dep_id) references dep(id) //創(chuàng)建約束
);
#2.插入記錄時(shí),先往被關(guān)聯(lián)表中插入記錄,再往關(guān)聯(lián)表中插入記錄
insert into dep values
(1,'IT','IT技術(shù)有限部門'),
(2,'銷售部','銷售部門'),
(3,'財(cái)務(wù)部','花錢太多部門');
insert into emp values
(1,'zhangsan',18,1),
(2,'lisi',19,1),
(3,'egon',20,2),
(4,'yuanhao',40,3),
(5,'alex',18,2);

3.刪除表

#按道理來說,刪除了部門表中的某個(gè)部門,員工表的有關(guān)聯(lián)的記錄相繼刪除。
mysql> delete from dep where id=3;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`db5`.`emp`, CONSTRAINT `fk_name` FOREIGN KEY (`dep_id`) REFERENCES `dep` (`id`))
#但是先刪除員工表的記錄之后,再刪除當(dāng)前部門就沒有任何問題
mysql> delete from emp where dep_id =3;
Query OK, 1 row affected (0.00 sec)
mysql> select * from emp;
+----+----------+-----+--------+
| id | name   | age | dep_id |
+----+----------+-----+--------+
| 1 | zhangsan | 18 |   1 |
| 2 | lisi   | 18 |   1 |
| 3 | egon   | 20 |   2 |
| 5 | alex   | 18 |   2 |
+----+----------+-----+--------+
4 rows in set (0.00 sec)
mysql> delete from dep where id=3;
Query OK, 1 row affected (0.00 sec)
mysql> select * from dep;
+----+-----------+----------------------+
| id | name   | descripe       |
+----+-----------+----------------------+
| 1 | IT    | IT技術(shù)有限部門    |
| 2 | 銷售部  | 銷售部門       |
+----+-----------+----------------------+
2 rows in set (0.00 sec)

上面的刪除表記錄的操作比較繁瑣,按道理講,裁掉一個(gè)部門,該部門的員工也會(huì)被裁掉。其實(shí)呢,在建表的時(shí)候還有個(gè)很重要的內(nèi)容,叫同步刪除,同步更新

on delete cascade #同步刪除
on update cascade #同步更新

create table emp(
  id int primary key,
  name varchar(20) not null,
  age int not null,
  dep_id int,
  constraint fk_dep foreign key(dep_id) references dep(id) 
  on delete cascade #同步刪除
  on update cascade #同步更新
);

#再去刪被關(guān)聯(lián)表(dep)的記錄,關(guān)聯(lián)表(emp)中的記錄也跟著刪除
mysql> delete from dep where id=3;
Query OK, 1 row affected (0.00 sec)
mysql> select * from dep;
+----+-----------+----------------------+
| id | name   | descripe       |
+----+-----------+----------------------+
| 1 | IT    | IT技術(shù)有限部門    |
| 2 | 銷售部  | 銷售部門       |
+----+-----------+----------------------+
2 rows in set (0.00 sec)
mysql> select * from emp;
+----+----------+-----+--------+
| id | name   | age | dep_id |
+----+----------+-----+--------+
| 1 | zhangsan | 18 |   1 |
| 2 | lisi   | 19 |   1 |
| 3 | egon   | 20 |   2 |
| 5 | alex   | 18 |   2 |
+----+----------+-----+--------+
4 rows in set (0.00 sec)
#再去更改被關(guān)聯(lián)表(dep)的記錄,關(guān)聯(lián)表(emp)中的記錄也跟著更改
mysql> update dep set id=222 where id=2;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
# 趕緊去查看一下兩張表是否都被刪除了,是否都被更改了
mysql> select * from dep;
+-----+-----------+----------------------+
| id | name   | descripe       |
+-----+-----------+----------------------+
|  1 | IT    | IT技術(shù)有限部門    |
| 222 | 銷售部  | 銷售部門       |
+-----+-----------+----------------------+
2 rows in set (0.00 sec)
mysql> select * from emp;
+----+----------+-----+--------+
| id | name   | age | dep_id |
+----+----------+-----+--------+
| 1 | zhangsan | 18 |   1 |
| 2 | lisi   | 19 |   1 |
| 3 | egon   | 20 |  222 |
| 5 | alex   | 18 |  222 |
+----+----------+-----+--------+
4 rows in set (0.00 sec)

更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲(chǔ)過程技巧大全》及《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》

希望本文所述對(duì)大家MySQL數(shù)據(jù)庫計(jì)有所幫助。

您可能感興趣的文章:
  • MySQL約束超詳解
  • MySQL中常見的六個(gè)約束類型詳解
  • MySQL約束類型及舉例介紹
  • 基于MySQL數(shù)據(jù)庫的數(shù)據(jù)約束實(shí)例及五種完整性約束介紹
  • MySQL學(xué)習(xí)之?dāng)?shù)據(jù)庫表五大約束詳解小白篇

標(biāo)簽:煙臺(tái) 果洛 來賓 河北 鞍山 陽江 赤峰 黃石

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《mysql完整性約束實(shí)例詳解》,本文關(guān)鍵詞  mysql,完整性,約束,實(shí)例,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《mysql完整性約束實(shí)例詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于mysql完整性約束實(shí)例詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章