時間:2022-12-06來源:www.djmaza-info.com作者:電腦系統城
定義一個外鍵時,需要遵守下列規則:
主表必須已經存在于數據庫中,或者是當前正在創建的表。
必須為主表定義主鍵。
主鍵不能包含空值,但允許在外鍵中出現空值。也就是說,只要外鍵的每個非空值出現在指定的主鍵中,這 個外鍵的內容就是正確的。
在主表的表名后面指定列名或列名的組合。這個列或列的組合必須是主表的主鍵或候選鍵。
外鍵中列的數目必須和主表的主鍵中列的數目相同。
外鍵中列的數據類型必須和主表主鍵中對應列的數據類型相同。
建立外鍵約束
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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); |
數據插入
必須先給主表添加數據,且從表外鍵列的值必須依賴于主表的主鍵列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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' ); |
數據刪除
主表數據被從表依賴時不能刪除,否則可以刪除;從表的數據可以隨便刪除。
如下,第一句和第二句執行成功,第三句執行失敗
1 2 3 |
delete from per3 where depid=1003; delete from dep3 where pid=1004; delete from dep3 where pid=1002; |
刪除外鍵約束
語法:alter table 從表drop foreign key 關鍵詞名;
1 | alter table per3 drop foreign key fok3; |
交叉連接查詢
1 | select * from dept,emp; |
內連接查詢
注釋;上面是隱式內連接,下面是顯式內連接
1 2 3 4 5 6 7 8 9 |
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
1 2 3 4 5 6 7 |
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; |
子查詢
1 2 3 4 5 |
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關鍵字的用法
1 2 |
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)關鍵字的用法
1 | select * from emp where age> any ( select age from emp where dept_id= '1003' ) and dept_id!= '1003' ; |
in關鍵字的用法
1 | select ename,eid from emp where dept_id in ( select deptno from dept where name in ( '研發部' , '銷售部' )); |
exists關鍵字的用法
1 2 3 4 |
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多表操作方法講解教程的文章就介紹到這了
2022-12-06
MySQL權限控制和用戶與角色管理實例分析講解2022-12-06
Mysql的DQL查詢操作全面分析講解2022-12-06
MySQL索引與事務定義到使用詳解1. NULL約束 2. UNIQUE(唯一約束) 3. DEFAULT(默認值約束) 4. PRIMARY KEY(主鍵約束) 5. FOREIGN KEY(外鍵約束)...
2022-12-06
我們做數據分析的時候經常會遇到去重問題,下面總結 sql 去重的幾種方式,后續如果還有再補充,大數據分析層面包括 hive、clickhouse 也可參考。...
2022-12-06