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

特點
定義一個外鍵時,需要遵守下列規(guī)則:
主表必須已經(jīng)存在于數(shù)據(jù)庫中,或者是當(dāng)前正在創(chuàng)建的表。
必須為主表定義主鍵。
主鍵不能包含空值,但允許在外鍵中出現(xiàn)空值。也就是說,只要外鍵的每個非空值出現(xiàn)在指定的主鍵中,這 個外鍵的內(nèi)容就是正確的。
在主表的表名后面指定列名或列名的組合。這個列或列的組合必須是主表的主鍵或候選鍵。
外鍵中列的數(shù)目必須和主表的主鍵中列的數(shù)目相同。
外鍵中列的數(shù)據(jù)類型必須和主表主鍵中對應(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','財務(wù)部');
insert into dep3 values('1004','人事部');
-- 給per3表添加數(shù)據(jù)
insert into per3 values('1','喬峰',20, '1001');
insert into per3 values('2','段譽',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ù)據(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;外連接查詢
若是對應(yīng)的外表沒有數(shù)據(jù)就補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; -- 對比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ā)部','銷售部'));
-- 對比關(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySql?字符集不同導(dǎo)致?left?join?慢查詢的問題解決
當(dāng)兩個表的字符集不一樣,在使用字符型字段進行表連接查詢時,就需要特別注意下查詢耗時是否符合預(yù)期,本文主要介紹了MySql?字符集不同導(dǎo)致?left?join?慢查詢的問題解決,感興趣的可以了解一下2024-05-05
mysql登錄報錯提示:ERROR 1045 (28000)的解決方法
這篇文章主要介紹了mysql登錄報錯提示:ERROR 1045 (28000)的解決方法,詳細(xì)分析了出現(xiàn)MySQL登陸錯誤的原因與對應(yīng)的解決方法,需要的朋友可以參考下2016-04-04
MYSQL必知必會讀書筆記第三章之顯示數(shù)據(jù)庫
MySQL是一種開放源代碼的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)(RDBMS),MySQL數(shù)據(jù)庫系統(tǒng)使用最常用的數(shù)據(jù)庫管理語言--結(jié)構(gòu)化查詢語言(SQL)進行數(shù)據(jù)庫管理。接下來通過本文給大家介紹MYSQL必知必會讀書筆記第三章之顯示數(shù)據(jù)庫,感興趣的朋友參考下吧2016-05-05
windows環(huán)境下mysql數(shù)據(jù)庫的主從同步備份步驟(單向同步)
本文主要是向大家描述的是在windows環(huán)境之下實現(xiàn)MySQL數(shù)據(jù)庫的主從同步備份的正確操作方案,以下就是文章的詳細(xì)內(nèi)容描述2011-05-05
Mysql 刪除數(shù)據(jù)庫drop database詳細(xì)介紹
在mysql中,我們可以使用DROP DATABASE來刪除數(shù)據(jù)庫,并且數(shù)據(jù)庫中所有表也隨之刪除。本文通過實例向各位碼農(nóng)介紹DROP DATABASE的使用方法,需要的朋友可以參考下2016-11-11

