1. 表连接基础
表连接是SQL中重要的操作,用于将多个表中的数据根据关联条件组合在一起。Oracle支持多种类型的表连接,包括内连接、外连接、交叉连接等。更多学习教程www.fgedu.net.cn
CREATE TABLE departments (
department_id NUMBER(4),
department_name VARCHAR2(30),
manager_id NUMBER(6),
location_id NUMBER(4)
);– 插入部门数据
INSERT INTO departments VALUES (60, ‘IT’, 103, 1400);INSERT INTO departments VALUES (90, ‘Executive’, 100, 1700);INSERT INTO departments VALUES (100, ‘Finance’, 108, 1700);– 员工表已在之前的示例中创建
SELECT * FROM departments;DEPARTMENT_ID DEPARTMENT_NAME MANAGER_ID LOCATION_ID
————- —————————— ———- ———–
60 IT 103 1400
90 Executive 100 1700
100 Finance 108 1700
2. 内连接
内连接(INNER JOIN)只返回两个表中满足连接条件的行。
SELECT e.employee_id, e.first_name, e.last_name, e.department_id, d.department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id;EMPLOYEE_ID EMP_NAME EMP_EMP_EMP_LAST_NAME DEPARTMENT_ID DEPARTMENT_NAME
———– ——————– ————————- ————- ——————————
100 Steven King 90 Executive
101 Neena Kochhar 90 Executive
102 Lex De Haan 90 Executive
103 Alexander Hunold 60 IT
104 Bruce Ernst 60 IT
— 使用WHERE子句实现内连接
SELECT e.employee_id, e.first_name, e.last_name, e.department_id, d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id;EMPLOYEE_ID EMP_NAME EMP_EMP_EMP_LAST_NAME DEPARTMENT_ID DEPARTMENT_NAME
———– ——————– ————————- ————- ——————————
100 Steven King 90 Executive
101 Neena Kochhar 90 Executive
102 Lex De Haan 90 Executive
103 Alexander Hunold 60 IT
104 Bruce Ernst 60 IT
3. 左连接
左连接(LEFT JOIN)返回左表中的所有行,以及右表中满足连接条件的行。如果右表中没有匹配的行,则返回NULL值。
SELECT e.employee_id, e.first_name, e.last_name, e.department_id, d.department_name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.department_id;EMPLOYEE_ID EMP_NAME EMP_EMP_EMP_LAST_NAME DEPARTMENT_ID DEPARTMENT_NAME
———– ——————– ————————- ————- ——————————
100 Steven King 90 Executive
101 Neena Kochhar 90 Executive
102 Lex De Haan 90 Executive
103 Alexander Hunold 60 IT
104 Bruce Ernst 60 IT
— 假设员工表中有未分配部门的员工
INSERT INTO employees VALUES (105, ‘David’, ‘Austin’, ‘DAUSTIN’, ‘590.423.4569’, TO_DATE(‘2005-06-25’, ‘YYYY-MM-DD’), ‘IT_PROG’, 4800, NULL, 103, NULL);SELECT e.employee_id, e.first_name, e.last_name, e.department_id, d.department_name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.department_id;EMPLOYEE_ID EMP_NAME EMP_EMP_EMP_LAST_NAME DEPARTMENT_ID DEPARTMENT_NAME
———– ——————– ————————- ————- ——————————
100 Steven King 90 Executive
101 Neena Kochhar 90 Executive
102 Lex De Haan 90 Executive
103 Alexander Hunold 60 IT
104 Bruce Ernst 60 IT
105 David Austin NULL
4. 右连接
右连接(RIGHT JOIN)返回右表中的所有行,以及左表中满足连接条件的行。如果左表中没有匹配的行,则返回NULL值。
SELECT e.employee_id, e.first_name, e.last_name, e.department_id, d.department_name
FROM employees e
RIGHT JOIN departments d ON e.department_id = d.department_id;EMPLOYEE_ID EMP_NAME EMP_EMP_EMP_LAST_NAME DEPARTMENT_ID DEPARTMENT_NAME
———– ——————– ————————- ————- ——————————
103 Alexander Hunold 60 IT
104 Bruce Ernst 60 IT
100 Steven King 90 Executive
101 Neena Kochhar 90 Executive
102 Lex De Haan 90 Executive
NULL 100 Finance
5. 全连接
全连接(FULL JOIN)返回左表和右表中的所有行。如果某一侧没有匹配的行,则返回NULL值。
SELECT e.employee_id, e.first_name, e.last_name, e.department_id, d.department_id AS dept_id, d.department_name
FROM employees e
FULL JOIN departments d ON e.department_id = d.department_id;EMPLOYEE_ID EMP_NAME EMP_EMP_EMP_LAST_NAME DEPARTMENT_ID DEPT_ID DEPARTMENT_NAME
———– ——————– ————————- ————- ———- ——————————
100 Steven King 90 90 Executive
101 Neena Kochhar 90 90 Executive
102 Lex De Haan 90 90 Executive
103 Alexander Hunold 60 60 IT
104 Bruce Ernst 60 60 IT
105 David Austin NULL
NULL 100 Finance
6. 交叉连接
交叉连接(CROSS JOIN)返回两个表的笛卡尔积,即左表中的每一行与右表中的每一行组合。
SELECT e.employee_id, e.first_name, e.last_name, d.department_id, d.department_name
FROM employees e
CROSS JOIN departments d;EMPLOYEE_ID EMP_NAME EMP_EMP_EMP_LAST_NAME DEPARTMENT_ID DEPARTMENT_NAME
———– ——————– ————————- ————- ——————————
100 Steven King 60 IT
100 Steven King 90 Executive
100 Steven King 100 Finance
101 Neena Kochhar 60 IT
101 Neena Kochhar 90 Executive
101 Neena Kochhar 100 Finance
102 Lex De Haan 60 IT
102 Lex De Haan 90 Executive
102 Lex De Haan 100 Finance
103 Alexander Hunold 60 IT
103 Alexander Hunold 90 Executive
103 Alexander Hunold 100 Finance
104 Bruce Ernst 60 IT
104 Bruce Ernst 90 Executive
104 Bruce Ernst 100 Finance
105 David Austin 60 IT
105 David Austin 90 Executive
105 David Austin 100 Finance
7. 自连接
自连接是指表与自身进行连接,通常用于处理具有层次结构的数据。
SELECT e.employee_id, e.first_name AS employee_name, e.manager_id, m.first_name AS manager_name
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.employee_id;EMPLOYEE_ID EMPLOYEE_NAME MANAGER_ID MANAGER_NAME
———– ——————– ———- ——————–
100 Steven NULL
101 Neena 100 Steven
102 Lex 100 Steven
103 Alexander 102 Lex
104 Bruce 103 Alexander
105 David 103 Alexander
8. 最佳实践
1. 明确指定连接类型,避免使用隐式连接
2. 使用表别名,提高查询的可读性
3. 为连接列创建索引,提高连接性能
4. 避免在连接条件中使用函数,影响索引的使用
5. 合理使用外连接,确保数据的完整性
6. 对于大型表,考虑使用分区连接或并行查询
7. 避免使用交叉连接,除非确实需要笛卡尔积
8. 对于自连接,使用适当的表别名区分不同的实例
9. 定期分析查询性能,优化连接操作
10. 遵循SQL编码规范,保持代码的可读性
更多视频教程www.fgedu.net.cn
学习交流加群风哥微信: itpux-com
学习交流加群风哥QQ113257174
更多学习教程公众号风哥教程itpux_com
from oracle:www.itpux.com
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
