oracle中文版教材

上传人:shaoy****1971 文档编号:112202375 上传时间:2019-11-05 格式:PPT 页数:41 大小:716KB
返回 下载 相关 举报
oracle中文版教材_第1页
第1页 / 共41页
oracle中文版教材_第2页
第2页 / 共41页
oracle中文版教材_第3页
第3页 / 共41页
oracle中文版教材_第4页
第4页 / 共41页
oracle中文版教材_第5页
第5页 / 共41页
点击查看更多>>
资源描述

《oracle中文版教材》由会员分享,可在线阅读,更多相关《oracle中文版教材(41页珍藏版)》请在金锄头文库上搜索。

1、4 Copyright Oracle Corporation, 2001. All rights reserved. 多表查询 4-2Copyright Oracle Corporation, 2001. All rights reserved. 目标 通过本章学习,您将可以: 使用等值和不等值连接在SELECT 语句中查询多个表中 的数据。 使用外连接查询不满足连接条件的数据。 使用自连接。 4-3Copyright Oracle Corporation, 2001. All rights reserved. 从多个表中获取数据 EMPLOYEES DEPARTMENTS 4-4Copyri

2、ght Oracle Corporation, 2001. All rights reserved. 笛卡尔集 笛卡尔集会在下面条件下产生: 省略连接条件 连接条件无效 所有表中的所有行互相连接 为了避免笛卡尔集, 可以在 WHERE 加入有效的连接条 件。 4-5Copyright Oracle Corporation, 2001. All rights reserved. 笛卡尔集 笛卡尔集: 20x8=160行 EMPLOYEES (20行)DEPARTMENTS (8行) 4-6Copyright Oracle Corporation, 2001. All rights reserve

3、d. Equijoin Non-equijoin Outer join Self join 连接的类型 Cross joins Natural joins Using clause Full or two sided outer joins Arbitrary join conditions for outer joins 适用于SQL: 1999的连接:Oracle 提供的连接 (8i 或 更早): 4-7Copyright Oracle Corporation, 2001. All rights reserved. Oracle 连接 使用连接在多个表中查询数据。 在 WHERE 字句中写

4、入连接条件。 在表中有相同列时,在列名之前加上表名前缀。 SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2; 4-8Copyright Oracle Corporation, 2001. All rights reserved. 等值连接 EMPLOYEES DEPARTMENTS 外键主键 4-9Copyright Oracle Corporation, 2001. All rights reserved. SELECT employees.employee_i

5、d, employees.last_name, employees.department_id, departments.department_id, departments.location_id FROM employees, departments WHERE employees.department_id = departments.department_id; 等值连接 4-10Copyright Oracle Corporation, 2001. All rights reserved. 多个连接条件与 AND 操作符 EMPLOYEES DEPARTMENTS 4-11Copyr

6、ight Oracle Corporation, 2001. All rights reserved. 区分重复的列名 使用表名前缀在多个表中区分相同的列。 使用表名可以提高效率。 在不同表中具有相同列名的列可以用别名加以区分。 4-12Copyright Oracle Corporation, 2001. All rights reserved. SELECT e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id FROM employees e , departments d WHERE e.

7、department_id = d.department_id; 表的别名 使用别名可以简化查询。 使用表名前缀可以提高执行效率。 4-13Copyright Oracle Corporation, 2001. All rights reserved. 连接多个表 EMPLOYEES LOCATIONS DEPARTMENTS 连接 n个表,至少需要 n-1个连接条件。 例如:连接三个 表,至少需要两个连接条件。 4-14Copyright Oracle Corporation, 2001. All rights reserved. 非等值连接 EMPLOYEESJOB_GRADES EMPL

8、OYEES表中的列工资 应在JOB_GRADES表中的最高 工资与最低工资之间 4-15Copyright Oracle Corporation, 2001. All rights reserved. 非等值连接 SELECT e.last_name, e.salary, j.grade_level FROM employees e, job_grades j WHERE e.salary BETWEEN j.lowest_sal AND j.highest_sal; 4-16Copyright Oracle Corporation, 2001. All rights reserved. 外连

9、接 EMPLOYEESDEPARTMENTS 190号部门没有员工 4-17Copyright Oracle Corporation, 2001. All rights reserved. 外连接语法 使用外连接可以查询不满足连接条件的数据。 外连接的符号是 (+)。 SELECT table1.column, table2.column FROMtable1, table2 WHEREtable1.column(+) = table2.column; SELECT table1.column, table2.column FROMtable1, table2 WHEREtable1.colu

10、mn = table2.column(+); 4-18Copyright Oracle Corporation, 2001. All rights reserved. SELECT e.last_name, e.department_id, d.department_name FROM employees e, departments d WHERE e.department_id(+) = d.department_id ; 外连接 4-19Copyright Oracle Corporation, 2001. All rights reserved. 自连接 EMPLOYEES (WORK

11、ER)EMPLOYEES (MANAGER) WORKER 表中的MANAGER_ID 和 MANAGER 表中的 MANAGER_ID相等 4-20Copyright Oracle Corporation, 2001. All rights reserved. 自连接 SELECT worker.last_name | works for | manager.last_name FROM employees worker, employees manager WHERE worker.manager_id = manager.employee_id ; 4-21Copyright Oracl

12、e Corporation, 2001. All rights reserved. 使用SQL: 1999 语法连接 使用连接从多个表中查询数据: SELECTtable1.column, table2.column FROMtable1 CROSS JOIN table2 | NATURAL JOIN table2 | JOIN table2 USING (column_name) | JOIN table2 ON(table1.column_name = table2.column_name) | LEFT|RIGHT|FULL OUTER JOIN table2 ON (table1.c

13、olumn_name = table2.column_name); 4-22Copyright Oracle Corporation, 2001. All rights reserved. 叉集 使用CROSS JOIN 子句使连接的表产生叉集。 叉集和笛卡尔集是相同的。 SELECT last_name, department_name FROM employees CROSS JOIN departments ; 4-23Copyright Oracle Corporation, 2001. All rights reserved. 自然连接 NATURAL JOIN 子句,会以两个表中具

14、有相同名字的 列为条件创建等值连接。 在表中查询满足等值条件的数据。 如果只是列名相同而数据类型不同,则会产生错误。 4-24Copyright Oracle Corporation, 2001. All rights reserved. SELECT department_id, department_name, location_id, city FROM departments NATURAL JOIN locations ; 自然连接 4-25Copyright Oracle Corporation, 2001. All rights reserved. 使用 USING 子句创建连接

15、 在NATURAL JOIN 子句创建等值连接时,可以使用 USING 子句指定等值连接中需要用到的列。 使用 USING 可以在有多个列满足条件时进行选择。 不要给选中的列中加上表名前缀或别名。 NATURAL JOIN 和 USING 子句经常同时使用。 4-26Copyright Oracle Corporation, 2001. All rights reserved. SELECT e.employee_id, e.last_name, d.location_id FROM employees e JOIN departments d USING (department_id) ;

16、USING 子句 4-27Copyright Oracle Corporation, 2001. All rights reserved. 使用ON 子句创建连接 自然连接中是以具有相同名字的列为连接条件的。 可以使用 ON 子句指定额外的连接条件。 这个连接条件是与其它条件分开的。 ON 子句使语句具有更高的易读性。 4-28Copyright Oracle Corporation, 2001. All rights reserved. SELECT e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id FROM employees e JOIN departments d ON

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 职业教育

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号