MySQL 外鍵(FOREIGN KEY)用法案例詳解
引子:把所有數(shù)據(jù)都存放于一張表的弊端
- 表的組織結(jié)構(gòu)復(fù)雜不清晰
- 浪費空間
- 擴展性極差
為了解決上述的問題,就需要用多張表來存放數(shù)據(jù)。
表與表的記錄之間存在著三種關(guān)系:一對多、多對多、一對一的關(guān)系。
處理表之間關(guān)系問題就會利用到FOREIGN KEY
多對一關(guān)系:
尋找表與表之間的關(guān)系的套路
舉例:雇員表:emp表 部門:dep表
part1:
- 先站在表emp的角度
- 去找表emp的多條記錄能否對應(yīng)表dep的一條記錄。
- 翻譯2的意義:
左表emp的多條記錄==》多個員工
右表dep的一條記錄==》一個部門
最終翻譯結(jié)果:多個員工是否可以屬于一個部門?
如果是則需要進行part2的流程
part2:
- 站在表dep的角度
- 去找表dep的多條記錄能否對應(yīng)表emp的一條記錄
- 翻譯2的意義:
右表dep的多條記錄==》多個部門
左表emp的一條記錄==》一個員工
最終翻譯結(jié)果:多個部門是否可以包含同一個員工
如果不可以,則可以確定emp與dep的關(guān)系只一個單向的多對一
如何實現(xiàn)?
此時就可以用到外鍵了,在emp表中新增一個dep_id字段,該字段指向dep表的id字段
foreign key會帶來什么樣的效果?
約束1:在創(chuàng)建表時,先建被關(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:在插入記錄時,必須先插被關(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)危機'),
('技術(shù)部','開發(fā)項目,研究技術(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,準備重建
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
);
此時再去修改:

得到結(jié)果:

此時再去刪除:

得到結(jié)果:

多對多的關(guān)系:
兩張表記錄之間是一個雙向的多對一關(guān)系,稱之為多對多關(guān)系。
如何實現(xiàn)?
建立第三張表,該表中有一個字段foreign key左表的id,還有一個字段是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),
一對一關(guān)系左表的一條記錄唯一對應(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) #此時外鍵的字段一定要保證unique
on delete cascade
on update cascade
);
到此這篇關(guān)于MySQL 外鍵(FOREIGN KEY)用法案例詳解的文章就介紹到這了,更多相關(guān)MySQL 外鍵(FOREIGN KEY)用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL將select結(jié)果執(zhí)行update的實例教程
這篇文章主要給大家介紹了關(guān)于MySQL將select結(jié)果執(zhí)行update的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
MySQL數(shù)據(jù)庫基礎(chǔ)入門之常用命令小結(jié)
這篇文章主要介紹了MySQL數(shù)據(jù)庫基礎(chǔ)入門之常用命令,結(jié)合實例形式分析了MySQL數(shù)據(jù)庫管理、備份、日志常用操作命令與使用注意事項,需要的朋友可以參考下2020-05-05
Java將excel中的數(shù)據(jù)導(dǎo)入到mysql中
這篇文章主要介紹了Java將excel中的數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫,小編覺得挺不錯的,現(xiàn)在分享給大家,需要的朋友可以參考借鑒2018-05-05
mysql8.0及以上my.cnf設(shè)置lower_case_table_names=1無法啟動問題
這篇文章主要介紹了mysql8.0及以上my.cnf設(shè)置lower_case_table_names=1無法啟動問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11

