MySQL不就是多表查詢(xún)嗎
嗨!小伙伴們大家好呀,忙碌的一周就要開(kāi)始!在此之前我們學(xué)習(xí)的MySQL數(shù)據(jù)庫(kù)的各種操作都是在一張表之中,今天我們學(xué)習(xí)要對(duì)多張表進(jìn)行相關(guān)操作,相比較于單一的表來(lái)說(shuō),多張表操作相對(duì)復(fù)雜一些,我相信只要認(rèn)真學(xué)習(xí)多表查詢(xún)也不再話(huà)下!
一、多表關(guān)系1.概述項(xiàng)目開(kāi)發(fā)中,在進(jìn)行數(shù)據(jù)庫(kù)表結(jié)構(gòu)設(shè)計(jì)時(shí),會(huì)根據(jù)業(yè)務(wù)需求及業(yè)務(wù)模塊之間的關(guān)系,分析并設(shè)計(jì)表結(jié)構(gòu),由于業(yè)務(wù)之間相互關(guān)聯(lián),所以各個(gè)表結(jié)構(gòu)之間也存在著各種聯(lián)系,基本上分為三種:
一對(duì)一(多對(duì)一)
一對(duì)多
多對(duì)多
1.1 一對(duì)一案例: 用戶(hù)與 用戶(hù)詳情的關(guān)系關(guān)系:一對(duì)一關(guān)系,多用于單表拆分,將一張表的基礎(chǔ)字段放在一張表中,其他詳情字段放在另一張表中,以提升操作效率
實(shí)現(xiàn): 在任意一方加入外鍵,關(guān)聯(lián)另外一方的主鍵,并且設(shè)置外鍵為唯一的(UNIQUE)

1.1.1 創(chuàng)建用戶(hù)基本信息
create table tb_user( id int auto_increment comment '主鍵id'primary key, name varchar(10) null comment '姓名', age int null comment '年齡', gender charnull comment '年齡 1男 2女', phone char(11) null comment '手機(jī)號(hào)') comment '用戶(hù)基本信息表';1.1.2 創(chuàng)建用戶(hù)教育信息表
create table tb_user_edu( id int auto_increment comment '主鍵id'primary key, degreevarchar(20) null comment '學(xué)歷', major varchar(50) null comment '專(zhuān)業(yè)', primaryschool varchar(50) null comment '小學(xué)', middleschool varchar(50) null comment '中學(xué)', university varchar(50) null comment '大學(xué)', useridint null comment '用戶(hù)id', constraint useridunique (userid), constraint fk_useridforeign key (userid) references tb_user (id)) comment '用戶(hù)教育信息表'; 1.2 一對(duì)多(多對(duì)一)案例:部門(mén)與員工的關(guān)系 關(guān)系:一個(gè)部門(mén)對(duì)應(yīng)多個(gè)員工,一個(gè)員工對(duì)應(yīng)一個(gè)部門(mén) 實(shí)現(xiàn): 在多的一方建立外鍵,指向一的一方的主鍵

案例: 學(xué)生與 課程的關(guān)系關(guān)系:一個(gè)學(xué)生可以選修多門(mén)課程,一門(mén)課程也可以供多個(gè)學(xué)生選擇實(shí)現(xiàn): 建立第三張中間表,中間表至少包含兩個(gè)外鍵,分別關(guān)聯(lián)兩方主鍵

建立三張表的代碼如下
1.3.1 創(chuàng)建學(xué)生表且插入數(shù)據(jù)
create table student(id int auto_increment primary key comment'主鍵id',name varchar(10) comment'姓名',novar char(10) comment'學(xué)號(hào)') comment'學(xué)生表';insert into student values(null,'宋江','001'),(null,'魯智深','002'),(null,'李逵','001');1.3.2 創(chuàng)建課程表且插入數(shù)據(jù)
create table course(id int auto_increment primary key comment '主鍵id',name varchar(10) comment'課程名稱(chēng)') comment'課程表';insert into course values(null,'java'),(null,'js'),(null,'MySQL');1.3.3 創(chuàng)建課程中間表且插入數(shù)據(jù)
create table student_course( idint auto_increment comment '主鍵'primary key, studentid int not null comment '學(xué)生id', courseid int not null comment '課程id', constraint f_kcourseidforeign key (courseid) references course (id), constraint f_kstudentidforeign key (studentid) references student (id)) comment '學(xué)生課程中間表';
概述: 指從多張表中查詢(xún)數(shù)據(jù),多表查詢(xún)就是要消除笛卡爾積。笛卡爾積: 笛卡爾乘積是指在數(shù)學(xué)中,兩個(gè)集合A集合和 B集合的所有組合情況。(在多表查詢(xún)時(shí),需要消除無(wú)效的笛卡爾積)

創(chuàng)建兩張表:部門(mén)表、員工表
create table dept( id int auto_increment comment 'id'primary key, name varchar(50) not null comment '部門(mén)名稱(chēng)') comment '部門(mén)表'; create table emp( id int auto_increment comment 'id'primary key, name varchar(50) not null comment '姓名', ageint null comment '年齡', jobvarchar(20) null comment '職位', salary int null comment '薪水', entrydate datenull comment '入職時(shí)間', mangagerid int null comment '直屬的領(lǐng)導(dǎo)id', dept_id int null comment '部門(mén)id', constraint dept_idforeign key (dept_id) references dept (id)) comment '員工表';兩張表消除笛卡爾積的方法:emp.dept_id=dept.id
三、多表查詢(xún)分類(lèi)3.1 連接查詢(xún)內(nèi)連接:相當(dāng)于查詢(xún)A、B交集部分?jǐn)?shù)據(jù)。外連接: 左外連接:查詢(xún)左表所有數(shù)據(jù),以及兩張表交集部分?jǐn)?shù)據(jù) 。 右外連接:查詢(xún)右表所有數(shù)據(jù),以及兩張表交集部分?jǐn)?shù)據(jù)。
自連接:當(dāng)前表與自身的連接查詢(xún),自連接必須使用表別名。

內(nèi)連接是兩張表交集的部分
3.3 內(nèi)連接練習(xí)3.3.1 查詢(xún)每一個(gè)員工的姓名,及關(guān)聯(lián)的部門(mén)的名稱(chēng)(隱式內(nèi)連接實(shí)現(xiàn))select emp.name,dept.name from emp,dept where emp.id=dept.id;3.3.2 查詢(xún)每一個(gè)員工的姓名,及關(guān)聯(lián)的部門(mén)的名稱(chēng)(顯示內(nèi)連接實(shí)現(xiàn))select emp.name,dept.name from emp join dept on emp.id=dept.id;
左外連接
select 字段列表 from 表1 left [outer] join 表2 on 條件...;相當(dāng)于查詢(xún)表1(左表)的所有數(shù)據(jù) 包含 表1和表2交集部分的數(shù)據(jù)
右外連接
select 字段列表 from 表1 right [outer] join 表2 on 條件...;相當(dāng)于查詢(xún)表2(右表)的所有數(shù)據(jù) 包含 表1和表2交集部分的數(shù)據(jù)
3.5 外連接練習(xí)連接條件:emp.id=dept.id
3.5.1 查詢(xún)emp表的所有數(shù)據(jù),和對(duì)應(yīng)部門(mén)信息(左外連接)select e.*,d.name from emp e left outer join dept d on d.id=e.id;3.5.2 查詢(xún)dept表的所有數(shù)據(jù),和對(duì)應(yīng)的員工信息(右外連接)select d.*,e.* from emp e right outer join dept d on d.id=e.id;右連接可以改為左連接
select d.*,e.* from dept d left outer join emp e on d.id=e.id;
子連接查詢(xún),可以是內(nèi)連接查詢(xún),也可以是外連接查詢(xún)。
3.7 聯(lián)合查詢(xún) union,union all對(duì)于union查詢(xún),就是把多次查詢(xún)的結(jié)果合并起來(lái),形成一個(gè)新的查詢(xún)結(jié)果集。
union 語(yǔ)法
SELECT 字段列表 FROM 表A ....UNION [ ALL]SELECT 字段列表 FROM 表B ....;注意:
對(duì)于聯(lián)合查詢(xún)的多張表的列數(shù)必須保持一致,字段類(lèi)型也需要保持一致。
union all 會(huì)將全部的數(shù)據(jù)直接合并在一起,union 會(huì)對(duì)合并之后的數(shù)據(jù)去重。
四、總結(jié)今天的課程就到此結(jié)束了,今天所學(xué)的課程為多表查詢(xún)的一部分,在下一期的課程中我們主要學(xué)習(xí)子查詢(xún),期待我們下次再見(jiàn)
到此這篇關(guān)于MySQL不就是多表查詢(xún)嗎的文章就介紹到這了,更多相關(guān)mysql多表查詢(xún)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. MySQL分區(qū)的優(yōu)點(diǎn)2. mysql的like模式3. 學(xué)好Oracle的六條總結(jié)4. 從舊版本SQL Server中重新存儲(chǔ)數(shù)據(jù)5. 如何實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)的備份與恢復(fù)6. 加密你的Access數(shù)據(jù)庫(kù)asp打開(kāi)方法7. Oracle根據(jù)逗號(hào)拆分字段內(nèi)容轉(zhuǎn)成多行的函數(shù)說(shuō)明8. mysql啟動(dòng)時(shí)報(bào)錯(cuò) ERROR! Manager of pid-file quit without9. MYSQL(電話(huà)號(hào)碼,身份證)數(shù)據(jù)脫敏的實(shí)現(xiàn)10. 什么是Access數(shù)據(jù)庫(kù)

網(wǎng)公網(wǎng)安備