一. 一對多(多對一)
? ? ? ? 1. 例如;一個部門下有多個員工
? ? ? ? ????????在數據庫表中多的一方(員工表)、添加字段,來關聯一的一方(部門表)的主鍵
二. 外鍵約束
? ? ? ? 1.如將部門表的部門直接刪除,然而員工表還存在其部門下的員工,出現了數據的不一致問題,是因為在數據庫層面,員工表與部門表并未建立關聯,所以無法保證數據的一致性和完整性。此時就需要外鍵約束
? ? ? ?可以在創建表時 或 表結構創建完成后,為字段添加外鍵約束:
? ? ? ? 例如:
????????????????ALTER TABLE EMP ADD CONSTRAINT FK_EMP_DEPT_id foreign key (dept_id) references dept(id);
-- 創建表時指定
create table 表名(字段名 數據類型,...[constraint] [外鍵名稱] foreign key (外鍵字段名) references 主表 (字段名));-- 建表完成后,添加外鍵
alter table 表名 add constraint 外鍵名稱 foreign key (外鍵字段名) references 主表 (字段名);
? ? ? ? ?物理外鍵:
????????????????使用foreign key 定義外鍵關聯另外一張表(已經被邏輯外鍵取代)
? ? ? ? ? ? ? ? 缺點:① 影響增、刪、改的效率(需要檢查外鍵關系)
? ? ? ? ? ? ? ? ? ? ? ? ② 僅用于單節點數據庫,不適用與分布式、集群場景。
? ? ? ? ? ? ? ? ? ? ? ? ③ 容易引發數據庫的死鎖問題,消耗性能
? ? ? ? 邏輯外鍵:
????????????????在業務層邏輯中,解決外鍵關聯,通過邏輯外鍵,就可以很方便的解決上述問題-----推薦使用
三.一對一
? ? ? ? 1. 關系:一對一關系,多用于單表拆分,將一張表的基礎字段放在一張表中,其他字段放在另一張表中,以提升操作效率。
? ? ? ? 2. 在任意一方加入外鍵,關聯另外一方的主鍵,并且設置外鍵為唯一的(UNIQUE)
四.多對多
? ? ? ? 1. 例如;學生與課程之間的關系,一個學生可以選擇多門課程,一門課程也可以供多個學生選擇
? ? ? ? 2. 建立第三張中間表,中間表至少包含兩個外鍵,分別關聯兩方主鍵
五. 多表查詢
? ? ? ? 從多張表中查詢數據
? ? ? ? 1. 笛卡爾積
????????????????指在數學中,兩個集合(A集合和B集合)的所有組合情況--
????????????????select * from emp, dept;
????????????????在多表查詢時,需要消除無效的笛卡爾積。
????????????????select * from emp, dept where emp.dept_id = dept.id;
? ? ? ? 2. 連接查詢
? ? ? ? ? ? ? ? 內連接
? ? ? ? ? ? ? ? ? ? ? ? 相當于查詢A、B兩表交集的部分數據。
-- 1. 隱式內連接 (常用)
select 字段列表 from 表1, 表2 where 連接條件...;-- 2. 顯示內連接
select 字段列表 from 表1 [inner] join 表2 on 連接條件 ..;-- 給表起別名
select 字段列表 from 表1 [as] 別名1, 表2 [as] 別名2 where 條件...;
-- 例如
select emp.id, emp.name,dept.name from emp, dept where emp.dept_id = dept.id;
select emp.id, emp.name,dept.name from emp inner join dept on emp.dept_id = dept.id;
select emp.id, emp.name,dept.name from emp join dept on emp.dept_id = dept.id;select e.id, e.name,d.name from emp e, dept d where e.dept_id = d.id and e.salary > 5000;
select e.id, e.name,d.name from emp e inner join dept d on e.dept_id = d.id where e.salary > 5000;
? ? ? ? ? ? ? ? 外連接
? ? ? ? ? ? ? ? ? ? ? ? 左外連接
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 查詢左表所有數據(包括兩張表交集部分的數據)
? ? ? ? ? ? ? ? ? ? ? ? 右外連接
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 查詢右表所有數據(包括兩張表交集部分的數據)
-- 左外連接 (常見)
select 字段列表 from 表1 left [outer] join 表2 on 連接條件...;-- 右外連接
select 字段列表 from 表1 right [outer] join 表2 on 連接條件...;
-- 左外連接 包含左表所有數據
select e.name,d.name from emp e left join dept d on e.dept_id = d.id;
select e.name,d.name,e.salary from emp e left join dept d on e.dept_id = d.id where e.salary > 5000;-- 右外連接 包含右表表所有數據
select d.name,e.name from emp e right join dept d on d.id = e.dept_id
? ? ? ? ? ? ? ? ? ? ? ? ?對于外連接,常用的是左外連接,因為右外連接的SQL也可以改造成左外連接(兩張表換個順序)
? ? ? ? 3. 子查詢
? ? ? ? ? ? ? ? (1) SQL語句中嵌套select語句,稱為嵌套查詢,又稱子查詢
? ? ? ? ? ? ? ? (2) 格式:select * from 表1 where 字段 = (select 字段 for 表2...)
-- 子查詢
select * from emp where emp.entry_date = (select min(e.entry_date) from emp e) ;
? ? ? ? ? ? ? ? (3) 說明:子查詢外部的語句可以是insert / update /delete /select 的任何一個,常見的是select
? ? ? ? ? ? ? ? (4) 分類:
? ? ? ? ? ? ? ? ? ? ? ? ① 標量子查詢:子查詢返回的結果為單個值
? ? ? ? ? ? ? ? ? ? ? ? ② 列子查詢:子查詢返回的結果為一列
? ? ? ? ? ? ? ? ? ? ? ? ③ 行子查詢:子查詢返回的結果為一行
? ? ? ? ? ? ? ? ? ? ? ? ④ 表子查詢:子查詢返回的結果為多行多列
-- 例如-- 標量子查詢
select * from emp where emp.entry_date = (select min(e.entry_date) from emp e) ;
select * from emp where emp.entry_date > (select e.entry_date from emp e where e.name = '武松' ) ;-- 列子查詢
select e.* from emp e where e.dept_id in (select d.id from dept d where d.name in ('人事部','就業部'));-- 行子查詢
select * from emp where (salary, job) = (select salary, job from emp where emp.name = '武松');-- 表子查詢
select e.* from emp e, (select dept_id, max(salary) maxSa from emp group by dept_id) e2where e.dept_id = e2.dept_id and e.salary = e2.maxSa;