溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

創(chuàng)建MySQL外鍵步驟

發(fā)布時間:2020-05-23 17:38:00 來源:網(wǎng)絡(luò) 閱讀:241 作者:三月 欄目:MySQL數(shù)據(jù)庫

本文主要給大家簡單講講創(chuàng)建MySQL外鍵步驟,相關(guān)專業(yè)術(shù)語大家可以上網(wǎng)查查或者找一些相關(guān)書籍補充一下,這里就不涉獵了,我們就直奔主題吧,希望創(chuàng)建MySQL外鍵步驟這篇文章可以給大家?guī)硪恍嶋H幫助。

1.創(chuàng)建表

(1)外鍵:FOREIGN KEY(ordersid) references orders(id)
在建表過程中

create table team(
id int primary key auto_increment,
name varchar(40)
);

create table star(
id int ,
name varchar(40),
team_id int,
foreign key (team_id) references team(id)
);

insert into team values(null,'Toronto Raptors'),(null,'Milwaukee Bucks'),(null,'Boston Celtics'),(null,'Golden State Warriors'),(null,'Oklahoma City Thunder'),(null,'Dallas Mavericks');

insert into star values(2,'科懷-倫納德',1),(7,'洛瑞',1),(34,'阿德托昆博',2),(22,'米德爾頓',2),(11,'歐文',3),(20,'海沃德',3),(35,'杜蘭特',4),(30,'庫里',4),(0,'威斯布魯克',5),(13,'保羅-喬治',5),(77,'盧克-東契奇',6),(41,'諾維斯基',6);

(2)在表已經(jīng)存在,通過修改表的語句增加外鍵

ALTER TABLE 表名 ADD constraint FK_ID#外鍵名 foreign key(外鍵字段名) references 外表表名(主字段名)

(3)刪除外鍵
alter table 表名 drop foreign key 外鍵名;

(4)操作關(guān)聯(lián)表

多對多關(guān)系:新建一張第三方關(guān)系表,保存兩張表的主鍵作為外鍵,存儲兩張表主鍵主鍵之間的對應(yīng)關(guān)系,來保存兩張表之間的關(guān)系

一對一:在從表建立外鍵

2.連接查詢

(1)多表設(shè)計多表查詢
select * from team,star;         #兩張表相乘結(jié)果

select * from team,star where team.id = star.team_id;  #過濾

~內(nèi)連接:自然連接
SELECT 查詢字段 FROM 表1 [INNER] JOIN 表2 ON 表1.關(guān)系字段 = 表2.關(guān)系字段

select * from team inner join star on team.id = star.team_id;

~左外連接查詢:在內(nèi)鏈接的基礎(chǔ)上增加上左邊表有而右邊表沒有的記錄

select * from team left join star on team.id = star.team_id;

~右外連接查詢,在內(nèi)鏈接的基礎(chǔ)上增加上右邊表有而左邊表沒有的記錄

select * from team right join star on team.id = star.team_id;

~全連接查詢:

select * from team full join star on team.id = star.team_id;#mysql不支持
但是支持union

select  from team left join star on team.id = star.team_id
union
select  from team right join star on team.id = star.team_id;

~查詢3號球隊的名稱和其中的球員的姓名

select * from team inner join star on team.id = star.team_id where team_id = 3;

select team.name 隊名,star.name 球員 from team inner join star on team.id = star.team_id where team_id = 3;

3.子查詢

(1)帶IN關(guān)鍵字的子查詢,內(nèi)層查詢語句僅僅返回一個數(shù)據(jù)列,這個數(shù)據(jù)列中的值將供外層查詢語句語句進(jìn)行比較操作,即嵌套查詢

~查詢號碼大于20的球員所屬的部門

select name from team where id in (select team_id from star where id>20);

~查詢不大于20的球員所屬的部門

select name from team where id not in (select team_id from star where id = 20);

(2)帶EXISTS關(guān)鍵字的子查詢:子查詢不返回任何數(shù)據(jù),只返回Ture or False ,為Ture時才執(zhí)行外查詢

select * from team where  exists (select team_id from star where id > 20);  #兩張表的字段名要一樣

(3)帶any關(guān)鍵字的子查詢:ANY關(guān)鍵字表示滿足其中任意一個條件即可

select * from team where id > any (select team_id from star where id = 20);

(4) 帶ALL關(guān)鍵字的子查詢:需要同時滿足內(nèi)查詢所有條件

select * from team where id > all (select team_id from star where id > 20);

select * from team where id > all (select team_id from star where id < 10);

表內(nèi)數(shù)據(jù)不能為空

(5)帶比較運算符的子查詢 <  >  <=  >=  =  <>

select * from team where id > (select team_id from star where id =0);

創(chuàng)建MySQL外鍵步驟就先給大家講到這里,對于其它相關(guān)問題大家想要了解的可以持續(xù)關(guān)注我們的行業(yè)資訊。我們的板塊內(nèi)容每天都會捕捉一些行業(yè)新聞及專業(yè)知識分享給大家的。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI