MySQL外鍵的關系有哪些,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
成都創新互聯公司基于成都重慶香港及美國等地區分布式IDC機房數據中心構建的電信大帶寬,聯通大帶寬,移動大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業服務器托管報價,主機托管價格性價比高,為金融證券行業大邑服務器托管,ai人工智能服務器托管提供bgp線路100M獨享,G口帶寬及機柜租用的專業成都idc公司。
多對一
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
);
# 先往被關聯表中插入記錄
insert into press(name) values
('北京工業地雷出版社'),
('人民音樂不好聽出版社'),
('知識產權沒有用出版社')
;
# 再往關聯表中插入記錄
insert into book(name,press_id) values
('九陽神功',1),
('九陰真經',2),
('九陰白骨爪',2),
('獨孤九劍',3),
('降龍十巴掌',2),
('葵花寶典',3)
;
查詢結果:
mysql> select * from book;
+----+-----------------+----------+
| id | name | press_id |
+----+-----------------+----------+
| 1 | 九陽神功 | 1 |
| 2 | 九陰真經 | 2 |
| 3 | 九陰白骨爪 | 2 |
| 4 | 獨孤九劍 | 3 |
| 5 | 降龍十巴掌 | 2 |
| 6 | 葵花寶典 | 3 |
+----+-----------------+----------+
rows in set (0.00 sec)
mysql> select * from press;
+----+--------------------------------+
| id | name |
+----+--------------------------------+
| 1 | 北京工業地雷出版社 |
| 2 | 人民音樂不好聽出版社 |
| 3 | 知識產權沒有用出版社 |
+----+--------------------------------+
rows in set (0.00 sec)
多對多
# 創建被關聯表author表,之前的book表在講多對一的關系已創建
create table author(
id int primary key auto_increment,
name varchar(20)
);
#這張表就存放了author表和book表的關系,即查詢二者的關系查這表就可以了
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)
);
#插入四個作者,id依次排開
insert into author(name) values('egon'),('alex'),('wusir'),('yuanhao');
# 每個作者的代表作
egon: 九陽神功、九陰真經、九陰白骨爪、獨孤九劍、降龍十巴掌、葵花寶典
alex: 九陽神功、葵花寶典
wusir:獨孤九劍、降龍十巴掌、葵花寶典
yuanhao:九陽神功
# 在author2book表中插入相應的數據
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)
;
# 現在就可以查author2book對應的作者和書的關系了
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)
一對一
#例如: 一個用戶只能注冊一個博客
#兩張表: 用戶表 (user)和 博客表(blog)
# 創建用戶表
create table user(
id int primary key auto_increment,
name varchar(20)
);
# 創建博客表
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
);
#插入用戶表中的記錄
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)
;
# 查詢wusir的博客地址
select url from blog where user_id=2;
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注創新互聯行業資訊頻道,感謝您對創新互聯的支持。
分享題目:mysql外鍵的關系有哪些
分享URL:http://m.kartarina.com/article6/jedgog.html
成都網站建設公司_創新互聯,為您提供定制開發、響應式網站、ChatGPT、外貿網站建設、營銷型網站建設、網站制作
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯