Mysql多表操作方法講解教程
外鍵約束
概念

特點(diǎn)
定義一個(gè)外鍵時(shí),需要遵守下列規(guī)則:
主表必須已經(jīng)存在于數(shù)據(jù)庫(kù)中,或者是當(dāng)前正在創(chuàng)建的表。
必須為主表定義主鍵。
主鍵不能包含空值,但允許在外鍵中出現(xiàn)空值。也就是說,只要外鍵的每個(gè)非空值出現(xiàn)在指定的主鍵中,這 個(gè)外鍵的內(nèi)容就是正確的。
在主表的表名后面指定列名或列名的組合。這個(gè)列或列的組合必須是主表的主鍵或候選鍵。
外鍵中列的數(shù)目必須和主表的主鍵中列的數(shù)目相同。
外鍵中列的數(shù)據(jù)類型必須和主表主鍵中對(duì)應(yīng)列的數(shù)據(jù)類型相同。

操作
建立外鍵約束
create database mydb3; use mydb3; create table if not exists dep ( pid int primary key, name varchar(20) ); create table if not exists per ( id int primary key, name varchar(20), age int, depid int, constraint fok foreign key(depid) references dep(pid) ); create table if not exists dep3 ( pid int primary key, name varchar(20) ); create table if not exists per3 ( id int primary key, name varchar(20), age int, depid int ); alter table per3 add constraint fok3 foreign key(depid) references dep3(pid);

數(shù)據(jù)插入
必須先給主表添加數(shù)據(jù),且從表外鍵列的值必須依賴于主表的主鍵列
insert into dep3 values('1001','研發(fā)部');
insert into dep3 values('1002','銷售部');
insert into dep3 values('1003','財(cái)務(wù)部');
insert into dep3 values('1004','人事部');
-- 給per3表添加數(shù)據(jù)
insert into per3 values('1','喬峰',20, '1001');
insert into per3 values('2','段譽(yù)',21, '1001');
insert into per3 values('3','虛竹',23, '1001');
insert into per3 values('4','阿紫',18, '1001');
insert into per3 values('5','掃地僧',85, '1002');
insert into per3 values('6','李秋水',33, '1002');
insert into per3 values('7','鳩摩智',50, '1002');
insert into per3 values('8','天山童姥',60, '1003');
insert into per3 values('9','慕容博',58, '1003');數(shù)據(jù)刪除
主表數(shù)據(jù)被從表依賴時(shí)不能刪除,否則可以刪除;從表的數(shù)據(jù)可以隨便刪除。
如下,第一句和第二句執(zhí)行成功,第三句執(zhí)行失敗
delete from per3 where depid=1003; delete from dep3 where pid=1004; delete from dep3 where pid=1002;
刪除外鍵約束
語法:alter table 從表drop foreign key 關(guān)鍵詞名;
alter table per3 drop foreign key fok3;
多表聯(lián)合查詢
概念

操作
交叉連接查詢

select * from dept,emp;
內(nèi)連接查詢

注釋;上面是隱式內(nèi)連接,下面是顯式內(nèi)連接
select * from dept,emp where dept.deptno=emp.dept_id;
select * from dept join emp on dept.deptno=emp.dept_id;
select * from dept join emp on dept.deptno=emp.dept_id and name='研發(fā)部';
select * from dept join emp on dept.deptno=emp.dept_id and name='研發(fā)部';
select * from dept join emp on dept.deptno=emp.dept_id and (name='研發(fā)部' or name='銷售部');
select * from dept join emp on dept.deptno=emp.dept_id and (name='研發(fā)部' or name ='銷售部');
select * from dept join emp on dept.deptno=emp.dept_id and name in ('研發(fā)部','銷售部');
select a.name,a.deptno,count(*) from dept a join emp on a.deptno=emp.dept_id group by dept_id;
select a.name,a.deptno,count(*) total from dept a join emp on a.deptno=emp.dept_id group by dept_id having total >=3 order by total desc;外連接查詢
若是對(duì)應(yīng)的外表沒有數(shù)據(jù)就補(bǔ)NULL

select * from dept a left join emp b on a.deptno=b.dept_id; select * from dept a right join emp b on a.deptno=b.dept_id; -- select * from dept a full join emp b on a.deptno=b.dept_id; --不能執(zhí)行 -- 用下面的方法代替上面的full join select * from dept a left join emp b on a.deptno=b.dept_id union select * from dept a right join emp b on a.deptno=b.dept_id; -- 對(duì)比union all,發(fā)現(xiàn)union all沒有去重過濾 select * from dept a left join emp b on a.deptno=b.dept_id union all select * from dept a right join emp b on a.deptno=b.dept_id;
子查詢

select * from emp where age<(select avg(age) from emp);
select * from emp a where a.dept_id in (select deptno from dept where name in ('研發(fā)部','銷售部'));
-- 對(duì)比關(guān)聯(lián)查詢和子查詢?nèi)缦?
select * from emp a join dept b on a.dept_id=b.deptno and (b.name='研發(fā)部' and age<30);
select * from (select * from dept where name='研發(fā)部') a join (select * from emp where age<30) b on b.dept_id=a.deptno;子查詢關(guān)鍵字
all關(guān)鍵字的用法

select * from emp where age>all(select age from emp where dept_id='1003'); select * from emp a where a.dept_id!=all(select deptno from dept);
any(some)關(guān)鍵字的用法

select * from emp where age>any(select age from emp where dept_id='1003') and dept_id!='1003';
in關(guān)鍵字的用法

select ename,eid from emp where dept_id in (select deptno from dept where name in ('研發(fā)部','銷售部'));exists關(guān)鍵字的用法

select * from emp a where a.age<30; select * from emp a where exists(select * from emp where a.age<30); select * from emp a where a.dept_id in (select deptno from dept b); select * from emp a where exists (select * from dept b where a.dept_id = b.deptno);
自關(guān)聯(lián)查詢


多表操作總結(jié)

到此這篇關(guān)于Mysql多表操作方法講解教程的文章就介紹到這了,更多相關(guān)Mysql多表操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySql?字符集不同導(dǎo)致?left?join?慢查詢的問題解決
當(dāng)兩個(gè)表的字符集不一樣,在使用字符型字段進(jìn)行表連接查詢時(shí),就需要特別注意下查詢耗時(shí)是否符合預(yù)期,本文主要介紹了MySql?字符集不同導(dǎo)致?left?join?慢查詢的問題解決,感興趣的可以了解一下2024-05-05
mysql登錄報(bào)錯(cuò)提示:ERROR 1045 (28000)的解決方法
這篇文章主要介紹了mysql登錄報(bào)錯(cuò)提示:ERROR 1045 (28000)的解決方法,詳細(xì)分析了出現(xiàn)MySQL登陸錯(cuò)誤的原因與對(duì)應(yīng)的解決方法,需要的朋友可以參考下2016-04-04
MYSQL必知必會(huì)讀書筆記第三章之顯示數(shù)據(jù)庫(kù)
MySQL是一種開放源代碼的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)(RDBMS),MySQL數(shù)據(jù)庫(kù)系統(tǒng)使用最常用的數(shù)據(jù)庫(kù)管理語言--結(jié)構(gòu)化查詢語言(SQL)進(jìn)行數(shù)據(jù)庫(kù)管理。接下來通過本文給大家介紹MYSQL必知必會(huì)讀書筆記第三章之顯示數(shù)據(jù)庫(kù),感興趣的朋友參考下吧2016-05-05
Mysql用戶權(quán)限分配實(shí)戰(zhàn)項(xiàng)目詳解
用戶是數(shù)據(jù)庫(kù)的使用者和管理者,MySQL通過用戶的設(shè)置來控制數(shù)據(jù)庫(kù)操作人員的訪問與操作范圍,這篇文章主要給大家介紹了關(guān)于Mysql用戶權(quán)限分配實(shí)戰(zhàn)項(xiàng)目的相關(guān)資料,需要的朋友可以參考下2023-12-12
windows環(huán)境下mysql數(shù)據(jù)庫(kù)的主從同步備份步驟(單向同步)
本文主要是向大家描述的是在windows環(huán)境之下實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)的主從同步備份的正確操作方案,以下就是文章的詳細(xì)內(nèi)容描述2011-05-05
Mysql 刪除數(shù)據(jù)庫(kù)drop database詳細(xì)介紹
在mysql中,我們可以使用DROP DATABASE來刪除數(shù)據(jù)庫(kù),并且數(shù)據(jù)庫(kù)中所有表也隨之刪除。本文通過實(shí)例向各位碼農(nóng)介紹DROP DATABASE的使用方法,需要的朋友可以參考下2016-11-11
MySql創(chuàng)建分區(qū)的方法實(shí)例
mysql分區(qū)相對(duì)于mysql分庫(kù)分表便利很多,可以對(duì)現(xiàn)有的mysql大表添加分區(qū),也可以對(duì)已有分區(qū)的表擴(kuò)充分區(qū),下面這篇文章主要給大家介紹了關(guān)于MySql創(chuàng)建分區(qū)的相關(guān)資料,需要的朋友可以參考下2022-04-04
逐步分析MySQL從庫(kù)com_insert無變化的原因
大家都知道com_insert等com_xxx參數(shù)可以用來監(jiān)控?cái)?shù)據(jù)庫(kù)實(shí)例的訪問量,也就是我們常說的QPS。并且基于MySQL的復(fù)制原理,所有主庫(kù)執(zhí)行的操作都會(huì)在從庫(kù)重放一遍保證數(shù)據(jù)一致,那么主庫(kù)的com_insert和從庫(kù)的com_insert理論上應(yīng)該是相等的。2014-05-05

