日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
mysql外鍵的關(guān)系有哪些

MySQL外鍵的關(guān)系有哪些,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

成都創(chuàng)新互聯(lián)公司基于成都重慶香港及美國(guó)等地區(qū)分布式IDC機(jī)房數(shù)據(jù)中心構(gòu)建的電信大帶寬,聯(lián)通大帶寬,移動(dòng)大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)服務(wù)器托管報(bào)價(jià),主機(jī)托管價(jià)格性價(jià)比高,為金融證券行業(yè)大邑服務(wù)器托管,ai人工智能服務(wù)器托管提供bgp線路100M獨(dú)享,G口帶寬及機(jī)柜租用的專業(yè)成都idc公司。

多對(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)

;

查詢結(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ì)多

# 創(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)系,即查詢二者的關(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ì)一

#例如: 一個(gè)用戶只能注冊(cè)一個(gè)博客

#兩張表: 用戶表 (user)和 博客表(blog)

# 創(chuàng)建用戶表

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

);

#插入用戶表中的記錄

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;

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。


新聞名稱:mysql外鍵的關(guān)系有哪些
轉(zhuǎn)載來(lái)于:http://www.dlmjj.cn/article/jedgog.html