MySQL 外鍵(FOREIGN KEY)用法案例詳解
引子:把所有數(shù)據(jù)都存放于一張表的弊端
- 表的組織結(jié)構(gòu)復(fù)雜不清晰
- 浪費(fèi)空間
- 擴(kuò)展性極差
為了解決上述的問(wèn)題,就需要用多張表來(lái)存放數(shù)據(jù)。
表與表的記錄之間存在著三種關(guān)系:一對(duì)多、多對(duì)多、一對(duì)一的關(guān)系。
處理表之間關(guān)系問(wèn)題就會(huì)利用到FOREIGN KEY
多對(duì)一關(guān)系:
尋找表與表之間的關(guān)系的套路
舉例:雇員表:emp表 部門:dep表
part1:
- 先站在表emp的角度
- 去找表emp的多條記錄能否對(duì)應(yīng)表dep的一條記錄。
- 翻譯2的意義:
左表emp的多條記錄==》多個(gè)員工
右表dep的一條記錄==》一個(gè)部門
最終翻譯結(jié)果:多個(gè)員工是否可以屬于一個(gè)部門?
如果是則需要進(jìn)行part2的流程
part2:
- 站在表dep的角度
- 去找表dep的多條記錄能否對(duì)應(yīng)表emp的一條記錄
- 翻譯2的意義:
右表dep的多條記錄==》多個(gè)部門
左表emp的一條記錄==》一個(gè)員工
最終翻譯結(jié)果:多個(gè)部門是否可以包含同一個(gè)員工
如果不可以,則可以確定emp與dep的關(guān)系只一個(gè)單向的多對(duì)一
如何實(shí)現(xiàn)?
此時(shí)就可以用到外鍵了,在emp表中新增一個(gè)dep_id字段,該字段指向dep表的id字段
foreign key會(huì)帶來(lái)什么樣的效果?
約束1:在創(chuàng)建表時(shí),先建被關(guān)聯(lián)的表dep,才能建關(guān)聯(lián)表emp
create table dep(
id int primary key auto_increment,
dep_name char(10),
dep_comment char(60)
);
create table emp(
id int primary key auto_increment,
name char(16),
gender enum('male','female') not null default 'male',
dep_id int,
foreign key(dep_id) references dep(id)
);
約束2:在插入記錄時(shí),必須先插被關(guān)聯(lián)的表dep,才能插關(guān)聯(lián)表emp
insert into dep(dep_name,dep_comment) values
('教學(xué)部','輔導(dǎo)學(xué)生學(xué)習(xí),教授課程'),
('公關(guān)部','處理公關(guān)危機(jī)'),
('技術(shù)部','開發(fā)項(xiàng)目,研究技術(shù)');
insert into emp(name,gender,dep_id) values
('monicx0','male',1),
('monicx1','male',2),
('monicx2','male',1),
('monicx3','male',1),
('lili','female',3);

約束3:更新與刪除都需要考慮到關(guān)聯(lián)與被關(guān)聯(lián)的關(guān)系。
解決方案:
1、先刪除關(guān)聯(lián)表emp,再刪除被關(guān)聯(lián)表dep,準(zhǔn)備重建
2、重建:新增功能,同步更新,同步刪除
create table dep(
id int primary key auto_increment,
dep_name char(10),
dep_comment char(60)
);
create table emp(
id int primary key auto_increment,
name char(16),
gender enum('male','female') not null default 'male',
dep_id int,
foreign key(dep_id) references dep(id)
on update cascade
on delete cascade
);
此時(shí)再去修改:

得到結(jié)果:

此時(shí)再去刪除:

得到結(jié)果:

多對(duì)多的關(guān)系:
兩張表記錄之間是一個(gè)雙向的多對(duì)一關(guān)系,稱之為多對(duì)多關(guān)系。
如何實(shí)現(xiàn)?
建立第三張表,該表中有一個(gè)字段foreign key左表的id,還有一個(gè)字段是foreign key右表的id
create table author(
id int primary key auto_increment,
name char(16)
);
create table book(
id int primary key auto_increment,
bname char(16),
price int
);
insert into author(name) values
('monicx1'),
('monicx2'),
('monicx3')
;
insert into book(bname,price) values
('python從入門到入土',200),
('liunx從入門到入土',400),
('java從入門到入土',300),
('php從入門到入土',100)
;
#建立第三張表:
create table author2book(
id int primary key auto_increment,
author_id int,
book_id int,
foreign key(author_id) references author(id)
on update cascade
on delete cascade,
foreign key(book_id) references book(id)
on update cascade
on delete cascade
);
insert into author2book(author_id,book_id) values
(1,3),
(1,4),
(2,2),
(2,4),
(3,1),
(3,2),
一對(duì)一關(guān)系左表的一條記錄唯一對(duì)應(yīng)右表的一條記錄,反之也一樣
create table customer(
id int primary key auto_increment,
name char(20) not null,
qq char(10) not null,
phone char(16) not null
);
create table student(
id int primary key auto_increment,
class_name char(20) not null,
customer_id int unique, #該字段一定要是唯一的
foreign key(customer_id) references customer(id) #此時(shí)外鍵的字段一定要保證unique
on delete cascade
on update cascade
);
到此這篇關(guān)于MySQL 外鍵(FOREIGN KEY)用法案例詳解的文章就介紹到這了,更多相關(guān)MySQL 外鍵(FOREIGN KEY)用法內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信