這篇文章將為大家詳細講解有關(guān)MySQL中怎么實現(xiàn)一個完整性約束,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
成都創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的昭通網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
約束條件作用:用于保證數(shù)據(jù)的完整性和一致性
主要分為
PRIMARY KEY (PK) #標識該字段為該表的主鍵,可以唯一的標識記錄
FOREIGN KEY (FK) #標識該字段為該表的外鍵
NOT NULL #標識該字段不能為空
UNIQUE KEY (UK) #標識該字段的值是唯一的,
AUTO_INCREMENT #標識該字段的值自動增長(整數(shù)類型,而且為主鍵)
DEFAULT #為該字段設(shè)置默認值
UNSIGNED #無符號
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
約束:約束的字段為自動增長,約束的字段必須同時被key約束
不指定id,則自動增長
# 創(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的記錄,會在之前的最后一條記錄繼續(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)
對于自增的字段,在用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是直接清空表,在刪除大表時用它 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,默認為1 # 起始的偏移量auto_increment_offset, 默認是1 # 設(shè)置步長 為會話設(shè)置,只在本次連接中有效 set session auto_increment_increment=5; #全局設(shè)置步長 都有效。 set global auto_increment_increment=5; # 設(shè)置起始偏移量 set global auto_increment_offset=3;
強調(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的值會被忽略
設(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) #因為之前有一條記錄id=1 mysql> select * from student; +----+---------+------+ | id | name | sex | +----+---------+------+ | 1 | xiaobai | male | +----+---------+------+ row in set (0.00 sec) # 下次插入的時候,從起始位置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
如上圖如果一個公司有很多員工,每個員工都對應(yīng)一個部門,在填表的時候就會重復寫這些部門,太冗余了
我們可以將它們分離
此時有兩張表,一張是employee表,簡稱emp表(關(guān)聯(lián)表,也就從表)。一張是department表,簡稱dep表(被關(guān)聯(lián)表,也叫主表)。
#1.創(chuàng)建表時先創(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.插入記錄時,先往被關(guān)聯(lián)表中插入記錄,再往關(guān)聯(lián)表中插入記錄 insert into dep values (1,'IT','IT技術(shù)有限部門'), (2,'銷售部','銷售部門'), (3,'財務(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.刪除表
#按道理來說,刪除了部門表中的某個部門,員工表的有關(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`)) #但是先刪除員工表的記錄之后,再刪除當前部門就沒有任何問題 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)
上面的刪除表記錄的操作比較繁瑣,按道理講,裁掉一個部門,該部門的員工也會被裁掉。其實呢,在建表的時候還有個很重要的內(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中怎么實現(xiàn)一個完整性約束就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)頁題目:mysql中怎么實現(xiàn)一個完整性約束
網(wǎng)頁鏈接:http://www.rwnh.cn/article28/pgsgjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、軟件開發(fā)、移動網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)