mysql外鍵的三種關(guān)系實(shí)例詳解
本文實(shí)例講述了mysql外鍵的三種關(guān)系。分享給大家供大家參考,具體如下:
因?yàn)橛衒oreign key的約束,使得兩張表形成了三種了關(guān)系:
- 多對(duì)一
- 多對(duì)多
- 一對(duì)一
一對(duì)多或多對(duì)一
多對(duì)一
create table press( id int primary key auto_increment, name varchar(20) ); create table book( id int primary key auto_increment, name varchar(20), press_id int not null, constraint fk_book_press foreign key(press_id) references press(id) on delete cascade on update cascade );
# 先往被關(guān)聯(lián)表中插入記錄 insert into press(name) values ('北京工業(yè)地雷出版社'), ('人民音樂(lè)不好聽(tīng)出版社'), ('知識(shí)產(chǎn)權(quán)沒(méi)有用出版社') ; # 再往關(guān)聯(lián)表中插入記錄 insert into book(name,press_id) values ('九陽(yáng)神功',1), ('九陰真經(jīng)',2), ('九陰白骨爪',2), ('獨(dú)孤九劍',3), ('降龍十巴掌',2), ('葵花寶典',3) ;
查詢(xún)結(jié)果:
mysql> select * from book; +----+-----------------+----------+ | id | name | press_id | +----+-----------------+----------+ | 1 | 九陽(yáng)神功 | 1 | | 2 | 九陰真經(jīng) | 2 | | 3 | 九陰白骨爪 | 2 | | 4 | 獨(dú)孤九劍 | 3 | | 5 | 降龍十巴掌 | 2 | | 6 | 葵花寶典 | 3 | +----+-----------------+----------+ rows in set (0.00 sec) mysql> select * from press; +----+--------------------------------+ | id | name | +----+--------------------------------+ | 1 | 北京工業(yè)地雷出版社 | | 2 | 人民音樂(lè)不好聽(tīng)出版社 | | 3 | 知識(shí)產(chǎn)權(quán)沒(méi)有用出版社 | +----+--------------------------------+ rows in set (0.00 sec)
多對(duì)多,引入第三張表
多對(duì)多
# 創(chuàng)建被關(guān)聯(lián)表author表,之前的book表在講多對(duì)一的關(guān)系已創(chuàng)建 create table author( id int primary key auto_increment, name varchar(20) ); #這張表就存放了author表和book表的關(guān)系,即查詢(xún)二者的關(guān)系查這表就可以了 create table author2book( id int not null unique auto_increment, author_id int not null, book_id int not null, constraint fk_author foreign key(author_id) references author(id) on delete cascade on update cascade, constraint fk_book foreign key(book_id) references book(id) on delete cascade on update cascade, primary key(author_id,book_id) ); #插入四個(gè)作者,id依次排開(kāi) insert into author(name) values('egon'),('alex'),('wusir'),('yuanhao'); # 每個(gè)作者的代表作 egon: 九陽(yáng)神功、九陰真經(jīng)、九陰白骨爪、獨(dú)孤九劍、降龍十巴掌、葵花寶典 alex: 九陽(yáng)神功、葵花寶典 wusir:獨(dú)孤九劍、降龍十巴掌、葵花寶典 yuanhao:九陽(yáng)神功 # 在author2book表中插入相應(yīng)的數(shù)據(jù) insert into author2book(author_id,book_id) values (1,1), (1,2), (1,3), (1,4), (1,5), (1,6), (2,1), (2,6), (3,4), (3,5), (3,6), (4,1) ;
# 現(xiàn)在就可以查author2book對(duì)應(yīng)的作者和書(shū)的關(guān)系了 mysql> select * from author2book; +----+-----------+---------+ | id | author_id | book_id | +----+-----------+---------+ | 1 | 1 | 1 | | 2 | 1 | 2 | | 3 | 1 | 3 | | 4 | 1 | 4 | | 5 | 1 | 5 | | 6 | 1 | 6 | | 7 | 2 | 1 | | 8 | 2 | 6 | | 9 | 3 | 4 | | 10 | 3 | 5 | | 11 | 3 | 6 | | 12 | 4 | 1 | +----+-----------+---------+ rows in set (0.00 sec)
一對(duì)一的情況
一對(duì)一
#例如: 一個(gè)用戶(hù)只能注冊(cè)一個(gè)博客 #兩張表: 用戶(hù)表 (user)和 博客表(blog) # 創(chuàng)建用戶(hù)表 create table user( id int primary key auto_increment, name varchar(20) ); # 創(chuàng)建博客表 create table blog( id int primary key auto_increment, url varchar(100), user_id int unique, constraint fk_user foreign key(user_id) references user(id) on delete cascade on update cascade ); #插入用戶(hù)表中的記錄 insert into user(name) values ('alex'), ('wusir'), ('egon'), ('xiaoma') ; # 插入博客表的記錄 insert into blog(url,user_id) values ('http://www.cnblog/alex',1), ('http://www.cnblog/wusir',2), ('http://www.cnblog/egon',3), ('http://www.cnblog/xiaoma',4) ; # 查詢(xún)wusir的博客地址 select url from blog where user_id=2;
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《MySQL查詢(xún)技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲(chǔ)過(guò)程技巧大全》及《MySQL數(shù)據(jù)庫(kù)鎖相關(guān)技巧匯總》
希望本文所述對(duì)大家MySQL數(shù)據(jù)庫(kù)計(jì)有所幫助。
- mysql外鍵基本功能與用法詳解
- Mysql數(shù)據(jù)庫(kù)中數(shù)據(jù)表的優(yōu)化、外鍵與三范式用法實(shí)例分析
- MySQL創(chuàng)建數(shù)據(jù)表并建立主外鍵關(guān)系詳解
- MySQL外鍵約束常見(jiàn)操作方法示例【查看、添加、修改、刪除】
- MySQL無(wú)法創(chuàng)建外鍵的原因及解決方法
- MySQL刪除有外鍵約束的表數(shù)據(jù)方法介紹
- MySQL刪除表的時(shí)候忽略外鍵約束的簡(jiǎn)單實(shí)現(xiàn)
- MySQL添加外鍵時(shí)報(bào)錯(cuò):1215 Cannot add the foreign key constraint的解決方法
- MySQL使用外鍵實(shí)現(xiàn)級(jí)聯(lián)刪除與更新的方法
- Mysql表創(chuàng)建外鍵報(bào)錯(cuò)解決方案
相關(guān)文章
MySQL級(jí)聯(lián)復(fù)制下如何進(jìn)行大表的字段擴(kuò)容
這篇文章主要介紹了MySQL級(jí)聯(lián)復(fù)制下進(jìn)行大表的字段擴(kuò)容,庫(kù)表信息環(huán)境是Mysql 8.0.22,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04SQL實(shí)現(xiàn)LeetCode(180.連續(xù)的數(shù)字)
這篇文章主要介紹了SQL實(shí)現(xiàn)LeetCode(180.連續(xù)的數(shù)字),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08MySQL數(shù)據(jù)表分區(qū)策略及優(yōu)缺點(diǎn)分析
項(xiàng)目開(kāi)發(fā)中,隨著數(shù)據(jù)庫(kù)數(shù)據(jù)量越來(lái)越大,單個(gè)表中數(shù)據(jù)太多,從而導(dǎo)致查詢(xún)速度變慢,而且由于表的鎖機(jī)制導(dǎo)致應(yīng)用操作也受到嚴(yán)重影響,出現(xiàn)了數(shù)據(jù)庫(kù)性能瓶頸。因此我們需要考慮分表與分區(qū),MySQL分表分區(qū)就是為了解決大數(shù)據(jù)量導(dǎo)致MySQL性能低下的問(wèn)題。2021-05-05mysql查詢(xún)字符串替換語(yǔ)句小結(jié)(數(shù)據(jù)庫(kù)字符串替換)
有時(shí)候我們需要對(duì)mysql的字符串進(jìn)行替換,我們就可以通過(guò)sql語(yǔ)句直接實(shí)現(xiàn)了,不過(guò)對(duì)于大數(shù)據(jù)量的字段不建議使用此方法2012-07-07使用MySQL實(shí)現(xiàn)select?into臨時(shí)表的功能
這篇文章主要介紹了使用MySQL實(shí)現(xiàn)select?into臨時(shí)表的功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09MySQL中查詢(xún)?nèi)罩九c慢查詢(xún)?nèi)罩镜幕緦W(xué)習(xí)教程
這篇文章主要介紹了MySQL中查詢(xún)?nèi)罩九c慢查詢(xún)?nèi)罩镜幕緦W(xué)習(xí)教程,文中還提到了MySQL自帶的Mysqldumpslow日志分析工具的使用,需要的朋友可以參考下2015-12-12