Mysql多表操作方法講解教程
外鍵約束
概念
特點(diǎn)
定義一個外鍵時,需要遵守下列規(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','財(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ù)據(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ù)就補(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; -- 對比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 order by與group by查詢優(yōu)化實(shí)現(xiàn)詳解
order by 子句盡量使用index方式排序(即using index),避免使用filesort方式排序(即using filesort)。Index方式效率高,它指MySQL掃描索引本身完成排序,filesort則效率低2022-11-11SQL使用WHERE條件語句的項(xiàng)目實(shí)踐
本文將介紹WHERE子句中使用的通用語法,它還將概述如何在單個WHERE子句中組合多個搜索條件謂詞以更細(xì)粒度的方式過濾數(shù)據(jù),以及如何使用NOT操作符排除而不是包含滿足給定搜索條件的行,感興趣的可以了解一下2023-09-09MySQL Workbench導(dǎo)出表結(jié)構(gòu)與數(shù)據(jù)的實(shí)現(xiàn)步驟
MySQL Workbench是一個強(qiáng)大的數(shù)據(jù)庫設(shè)計(jì)工具,提供了便捷的數(shù)據(jù)導(dǎo)入導(dǎo)出功能,本文就來介紹一下MySQL Workbench導(dǎo)出表結(jié)構(gòu)與數(shù)據(jù)的實(shí)現(xiàn)步驟,感興趣的可以了解一下2024-05-05詳解MySQL執(zhí)行原理、邏輯分層、更改數(shù)據(jù)庫處理引擎
在本文里我們給大家總結(jié)了關(guān)于MySQL執(zhí)行原理、邏輯分層、更改數(shù)據(jù)庫處理引擎的相關(guān)知識點(diǎn),需要的讀者們一起學(xué)習(xí)下。2019-02-02Can’t open file:''[Table]mytable.MYI''
也許很多人遇到過類似Can’t open file: ‘[Table]mytable.MYI’ 這樣的錯誤信息,卻不知道怎么解決他,下面我們做個介紹,2011-01-01MySQL 學(xué)習(xí)總結(jié) 之 初步了解 InnoDB 存儲引擎的架構(gòu)設(shè)計(jì)
這篇文章主要介紹了MySQL 學(xué)習(xí)總結(jié) 之 初步了解 InnoDB 存儲引擎的架構(gòu)設(shè)計(jì),文中給大家提到了mysql存儲引擎有哪些,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-02-02