MySQL5.7數(shù)據(jù)庫(kù)介紹表連接、子查詢、外鍵

MySQL5.7數(shù)據(jù)庫(kù)介紹表連接、子查詢、外鍵

免費(fèi)學(xué)習(xí)推薦:mysql視頻教程

文章目錄

      • 表連接
        • 內(nèi)連接
        • 左連接
        • 右連接
        • 子查詢
      • 自關(guān)聯(lián)
      • 外鍵
        • 外鍵介紹
        • 創(chuàng)建表時(shí)設(shè)置外鍵約束

表連接

  • 當(dāng)查詢結(jié)果的列來(lái)源于多張表時(shí),需要將多張表連接成一個(gè)大的數(shù)據(jù)集,再選擇合適的列返回mysql
  • 這時(shí)需要表進(jìn)行連接

內(nèi)連接

  • 內(nèi)連接僅選出兩張表中互相匹配的記錄
select * from 表1 inner join 表2 on 表1.列 = 表2.列-- 顯示學(xué)生的所有信息,但只顯示班級(jí)名稱select s.*, c.name from students s inner join classes c on s.id=c.id;-- 將班級(jí)名稱顯示在第一列select c.name, s.* from students s inner join classes c on s.id=c.id;-- 查詢 有能夠?qū)?yīng)班級(jí)的學(xué)生以及班級(jí)信息,按照班級(jí)進(jìn)行排序select c.name, s.* from students s inner join classes c on s.cls_id = c.id order by c.name asc;-- 當(dāng)同一個(gè)班級(jí)時(shí),按照學(xué)生的id進(jìn)行從小到大排序select c.name, s.* from students s inner join classes c on s.cls_id = c.id order by c.name asc, s.id asc;

MySQL5.7數(shù)據(jù)庫(kù)介紹表連接、子查詢、外鍵
MySQL5.7數(shù)據(jù)庫(kù)介紹表連接、子查詢、外鍵
在這里插入圖片描述

左連接

查詢的結(jié)果為兩個(gè)表匹配到的數(shù)據(jù),左表持有的數(shù)據(jù),對(duì)于右表中不存的數(shù)據(jù)使用null填充

select * from 表1 left join 表2 on 表1.列=表2.列-- students表左連接classes表 并查看班級(jí)為null的數(shù)據(jù)select * from students s left join classes c on s.cls_id=c.id having s.cls_id is null;-- 左連接 并且 查詢 s.cls_id=1 并且 s.name="small-j" 的數(shù)據(jù)select * from students s left join classes c on s.cls_id=c.id having s.cls_id=1 and s.name="small-j";

MySQL5.7數(shù)據(jù)庫(kù)介紹表連接、子查詢、外鍵

右連接

查詢結(jié)果為兩個(gè)表匹配到的數(shù)據(jù),右表持有的數(shù)據(jù),對(duì)于左表中不存在的數(shù)據(jù)使用null填充。

select * from 表1 right join 表2 on 表1.列 = 表2.列;

子查詢

某些情況下,當(dāng)進(jìn)行查詢的時(shí)候,需要的條件是另外一個(gè)select語(yǔ)句的結(jié)果,這個(gè)時(shí)候,就要使用到子查詢

select * from 表 where 表(子查詢語(yǔ)句)-- 查詢出students中身高最高的男生。顯示名字和身高select s.name, s.high from students s where high=(select max(high) from students) and gender="男";-- 查詢出高于平均身高的學(xué)生信息select * from students where high>(select avg(high) from students);-- 查詢學(xué)生班級(jí)號(hào)cls_id能夠?qū)?yīng)的學(xué)生信息select * from students where cls_id in (select id from students);-- 查詢最大年齡的女生的idselect * from students where id=(select max(id) from students where gender="女") and  gender="女";

在這里插入圖片描述

自關(guān)聯(lián)

簡(jiǎn)單理解為自己與自己進(jìn)行連接查詢

-- 查詢廣東省下的所有廣東市select * from cities c inner join provinces p on c.provinceid=p.provinceid having p.province="廣東省";-- 查詢廣東省下的所有廣東市-- 自關(guān)聯(lián)select * from areas a inner join areas b on a.id=b.pid having a.name="廣東";

MySQL5.7數(shù)據(jù)庫(kù)介紹表連接、子查詢、外鍵
MySQL5.7數(shù)據(jù)庫(kù)介紹表連接、子查詢、外鍵

外鍵

外鍵介紹

  • MySQL的外鍵(foreing key)是表的一個(gè)特殊字段。對(duì)于兩個(gè)具有關(guān)聯(lián)關(guān)系的表而言,相關(guān)聯(lián)字段的主鍵所在表就是主表(父表),外鍵所在的表是從表(子表)。
  • 注意: 主鍵不能包含空值,但允許在外鍵中出現(xiàn)空值,也就是說(shuō),只要外鍵的每個(gè)非空值出現(xiàn)在指定的主鍵中,這個(gè)外鍵的內(nèi)容就是正確的。

創(chuàng)建表時(shí)設(shè)置外鍵約束

  • 當(dāng)創(chuàng)建外鍵的時(shí)候,必須先刪除從表才能刪除主表。
  • 主表需存在時(shí)創(chuàng)建從表。
  • 從表的外鍵關(guān)聯(lián)必須是主表的主鍵,并且主鍵與外鍵的類型必須保持一致。
[constraint 外鍵名] foreign key (字段名 [,字段名2, ...]) references <主表名> 主鍵列1 [, 主鍵列2, ...]
-- 創(chuàng)建班級(jí)表create table classes(     id int(4) not null primary key,     name varchar(36));-- 創(chuàng)建學(xué)生表create table student(     sid int(4) not null primary key,     sname varchar(30),     cid int(4) not null);-- 創(chuàng)建直接含有外鍵關(guān)系的學(xué)生表create table student(   	sid int(4) not null primary key,   	sname varchar(30),   	cid int(4) not null,   	constraint pk_id foreign key (cid) references classes(id));-- 通過(guò)alter來(lái)添加外鍵關(guān)系alter table student add constraint pk_id foreign key (cid) references classes(id);-- 刪除外鍵約束alter table student drop foreign key pk_id;

MySQL5.7數(shù)據(jù)庫(kù)介紹表連接、子查詢、外鍵

MySQL5.7數(shù)據(jù)庫(kù)介紹表連接、子查詢、外鍵

相關(guān)免費(fèi)學(xué)習(xí)推薦:mysql視頻教程(視頻)

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊5 分享
站長(zhǎng)的頭像-小浪學(xué)習(xí)網(wǎng)月度會(huì)員