HTML基礎知識(30)外鍵的運用、條件及約束、

一、增加外鍵

創(chuàng)建表的時候增加外鍵:在所有的表字段之后,使用foreign key(外鍵字段) references 外部表(主鍵字段)

在新增表之后增加外鍵:修改表結構,使用alter table 表名 add [constraint 外鍵名字] foreign key(外鍵字段) references 父表(主鍵字段);

二、修改外鍵&刪除外鍵

alter table 表名 drop foreign key 外鍵名;

三、外鍵條件

外鍵要存在,首先必須保證表的存儲引擎是innodb

列類型必須與父表的主鍵類型一致

一張表中的外鍵名字不能重復

增加外鍵的字段數(shù)據(jù)已經(jīng)存在,必須保證數(shù)據(jù)與父表主鍵要求對應

四、外鍵約束

有三種約束模式

1、district:嚴格模式(默認的)

2、cascade:級聯(lián)模式

3、set null:置空模式

語法:foreign key(外鍵字段) references 父表(主鍵字段) on delete 模式 on update 模式;

五、聯(lián)合查詢

1、基本語法:

select 語句1

union [union 選項]

select 語句2……

2、union 選項

all:保留所有,不管重復

distinct:去重,默認的

六、按位置分類

from子查詢

where子查詢

exists子查詢

七、按結果分類

標量子查詢

列子查詢

行子查詢

表子查詢




————————代碼練習————————————————


-- 創(chuàng)建外鍵

create table my_foreign1(

idint primary key auto_increment,

namevarchar(20)not null comment

'學生姓名',

c_idint comment'班級id',

-- 增加外鍵

foreign key(c_id)references my_class(id)

)charset utf8;

-- 創(chuàng)建表

create table my_foreign2(

idint primary key auto_increment,

namevarchar(20)not null comment

'學生姓名',

c_idint comment'班級id' -- 普通字段

)charset utf8;

-- 增加外鍵

alter table my_foreign2add

-- 指定外鍵名

constraint student_class_1

-- 指定外鍵字段

foreign key(c_id)

-- 引用父表主鍵

references my_class(id);

-- 刪除外鍵

alter table my_foreign1drop

foreign key my_foreign1_ibfk_1;

-- 插入數(shù)據(jù):外鍵字段在父表不存在

insert into my_foreign2values(null,'郭富城',4);-- 沒有4號班級

insert into my_foreign2values(null,'項羽',1);

insert into my_foreign2values(null,'劉邦',2);

insert into my_foreign2values(null,'韓信',3);

-- 更新父表記錄

update my_classset id=4 where id=1;-- 失敗:id=1的記錄已經(jīng)被學生引用

update my_classset id=5 where id=3;-- 可以改,沒有學生引用此班級

-- 插入數(shù)據(jù)

insert into my_foreign1values(

null,'馬超',3);

-- 增加外鍵

alter table my_foreign1add foreign key(c_id)references my_class(id);

-- 失敗:因為沒有3號班

-- 創(chuàng)建外鍵:指定模式:刪除置空,更新及聯(lián)

create table my_foreign3(

idint primary key auto_increment,

namevarchar(20)not null,

c_idint,

-- 增加外鍵

foreign key(c_id)

-- 引用表

references my_class(id)

-- 指定刪除模式

on delete set null

-- 指定更新模式

on update cascade

)charset utf8;

-- 插入數(shù)據(jù)

insert into my_foreign3values

(null,'劉備',1),

(null,'曹操',1),

(null,'孫權',1),

(null,'諸葛亮',2),

(null,'周瑜',2);

-- 解除my_foreign2表的外鍵

alter table my_foreign2drop

foreign key student_class_1;

-- 增新父表主鍵

update my_classset id=3 where id=1;

-- 刪除父表主鍵

delete from my_classwhere id=2;

-- 聯(lián)合查詢

select *from my_class

union -- 默認去重

select *from my_class;

select *from my_class

union all -- 不去重

select *from my_class;

select id,c_name,roomfrom my_class

union all -- 不去重

select name,number,idfrom my_student;

-- 需求: 男生升序,女生降序 (年齡)

(select *from my_student

where sex='男'

order by ageasc limit9999999)

union

(select *from my_student

where sex='女'

order by agedesc limit9999999);

select *from my_studentwhere

c_id=(

-- 標量子查詢

select idfrom my_classwhere

c_name='Python1903');-- id一定只有一個值(一行一列)

insert into my_classvalues(1,'Python1907','B407');

-- 列子查詢

select *from my_studentwhere c_idin(select idfrom my_class);

-- any,some,all

select *from my_studentwhere c_id =any(select idfrom my_class);

select *from my_studentwhere c_id =some(select idfrom my_class);

select *from my_studentwhere c_id =all(select idfrom my_class);

select *from my_studentwhere c_id !=any(select idfrom my_class);-- 所有結果(NULL除外)

select *from my_studentwhere c_id !=some(select idfrom my_class);-- 所有結果(NULL除外)

select *from my_studentwhere c_id !=all(select idfrom my_class);-- 2號班級(NULL除外)

select *from my_studentwhere

age=(select max(age)from my_student)

and

height=(select max(height)from my_student);

-- 行子查詢

select *from my_student

-- (age,height)稱之為行元素

where (age,height)=(

select max(age),max(height)from my_student);

select *from my_studentorder by agedesc,heightdesc limit1;

-- 表子查詢

select *from my_studentgroup by c_idorder by heightdesc;-- 每個班選出的第一個學生

select *from (select *from my_studentorder by heightdesc)

as studentgroup by c_id;-- 每個班選出的第一個學生再按身高排序

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,501評論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,673評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,610評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,939評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,668評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,004評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,001評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,173評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,705評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,426評論 3 359
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,656評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,139評論 5 364
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,833評論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,247評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,580評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,371評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,621評論 2 380

推薦閱讀更多精彩內(nèi)容