首頁 > 軟體

Mysql多表操作方法講解教學

2022-12-05 14:01:31

外來鍵約束

概念

特點

定義一個外來鍵時,需要遵守下列規則:

主表必須已經存在於資料庫中,或者是當前正在建立的表。

必須為主表定義主鍵。

主鍵不能包含空值,但允許在外來鍵中出現空值。也就是說,只要外來鍵的每個非空值出現在指定的主鍵中,這 個外來鍵的內容就是正確的。

在主表的表名後面指定列名或列名的組合。這個列或列的組合必須是主表的主鍵或候選鍵。

外來鍵中列的數目必須和主表的主鍵中列的數目相同。

外來鍵中列的資料型別必須和主表主鍵中對應列的資料型別相同。

操作

建立外來鍵約束

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);

資料插入

必須先給主表新增資料,且從表外來鍵列的值必須依賴於主表的主鍵列

insert into dep3 values('1001','研發部');
insert into dep3 values('1002','銷售部');
insert into dep3 values('1003','財務部');
insert into dep3 values('1004','人事部');
-- 給per3表新增資料
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');

資料刪除

主表資料被從表依賴時不能刪除,否則可以刪除;從表的資料可以隨便刪除。

如下,第一句和第二句執行成功,第三句執行失敗

delete from per3 where depid=1003;
delete from dep3 where pid=1004;
delete from dep3 where pid=1002;

刪除外來鍵約束

語法:alter table 從表drop foreign key 關鍵詞名;

alter table per3 drop foreign key fok3;

多表聯合查詢

概念

操作

交叉連線查詢

select * from dept,emp;

內連線查詢

註釋;上面是隱式內連線,下面是顯式內連線

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='研發部';
select * from dept join emp on dept.deptno=emp.dept_id and name='研發部';
select * from dept join emp on dept.deptno=emp.dept_id and (name='研發部' or name='銷售部');
select * from dept join emp on dept.deptno=emp.dept_id and (name='研發部' or name ='銷售部');
select * from dept join emp on dept.deptno=emp.dept_id and name in ('研發部','銷售部');
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;

外連線查詢

若是對應的外表沒有資料就補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; --不能執行
-- 用下面的方法代替上面的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,發現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 ('研發部','銷售部'));
-- 對比關聯查詢和子查詢如下
select * from emp a join dept b on a.dept_id=b.deptno and (b.name='研發部' and age<30);
select * from (select * from dept where name='研發部') a join (select * from emp where age<30) b on b.dept_id=a.deptno;

子查詢關鍵字

all關鍵字的用法

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)關鍵字的用法

select * from emp where age>any(select age from emp where dept_id='1003') and dept_id!='1003';

in關鍵字的用法

select ename,eid from emp where dept_id in (select deptno from dept where name in ('研發部','銷售部'));

exists關鍵字的用法

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);

自關聯查詢

多表操作總結

到此這篇關於Mysql多表操作方法講解教學的文章就介紹到這了,更多相關Mysql多表操作內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com