oracle多表查询

上传人:飞****9 文档编号:131889712 上传时间:2020-05-10 格式:PPT 页数:41 大小:548KB
返回 下载 相关 举报
oracle多表查询_第1页
第1页 / 共41页
oracle多表查询_第2页
第2页 / 共41页
oracle多表查询_第3页
第3页 / 共41页
oracle多表查询_第4页
第4页 / 共41页
oracle多表查询_第5页
第5页 / 共41页
点击查看更多>>
资源描述

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

1、多表查询 目标 通过本章学习 您将可以 使用等值和不等值连接在SELECT语句中查询多个表中的数据 使用外连接查询不满足连接条件的数据 使用自连接 从多个表中获取数据 EMPLOYEES DEPARTMENTS 笛卡尔集 笛卡尔集会在下面条件下产生 省略连接条件连接条件无效所有表中的所有行互相连接为了避免笛卡尔集 可以在WHERE加入有效的连接条件 笛卡尔集 EMPLOYEES 20行 DEPARTMENTS 8行 EquijoinNon equijoinOuterjoinSelfjoin 连接的类型 CrossjoinsNaturaljoinsUsingclauseFullortwoside

2、douterjoinsArbitraryjoinconditionsforouterjoins 适用于SQL 1999的连接 Oracle提供的连接 8i或更早 Oracle连接 使用连接在多个表中查询数据 在WHERE字句中写入连接条件 在表中有相同列时 在列名之前加上表名前缀 SELECTtable1 column table2 columnFROMtable1 table2WHEREtable1 column1 table2 column2 等值连接 EMPLOYEES DEPARTMENTS SELECTemployees employee id employees last name

3、 employees department id departments department id departments location idFROMemployees departmentsWHEREemployees department id departments department id 等值连接 多个连接条件与AND操作符 EMPLOYEES DEPARTMENTS 区分重复的列名 使用表名前缀在多个表中区分相同的列 使用表名可以提高效率 在不同表中具有相同列名的列可以用别名加以区分 SELECTe employee id e last name e department

4、id d department id d location idFROMemployeese departmentsdWHEREe department id d department id 表的别名 使用别名可以简化查询 使用表名前缀可以提高执行效率 连接多个表 EMPLOYEES LOCATIONS DEPARTMENTS 连接n个表 至少需要n 1个连接条件 例如 连接三个表 至少需要两个连接条件 非等值连接 EMPLOYEES JOB GRADES 非等值连接 SELECTe last name e salary j grade levelFROMemployeese job grad

5、esjWHEREe salaryBETWEENj lowest salANDj highest sal 外连接 EMPLOYEES DEPARTMENTS 外连接语法 使用外连接可以查询不满足连接条件的数据 外连接的符号是 SELECTtable1 column table2 columnFROMtable1 table2WHEREtable1 column table2 column SELECTtable1 column table2 columnFROMtable1 table2WHEREtable1 column table2 column SELECTe last name e de

6、partment id d department nameFROMemployeese departmentsdWHEREe department id d department id 外连接 自连接 EMPLOYEES WORKER EMPLOYEES MANAGER 自连接 SELECTworker last name worksfor manager last nameFROMemployeesworker employeesmanagerWHEREworker manager id manager employee id 使用SQL 1999语法连接 使用连接从多个表中查询数据 SEL

7、ECTtable1 column table2 columnFROMtable1 CROSSJOINtable2 NATURALJOINtable2 JOINtable2USING column name JOINtable2ON table1 column name table2 column name LEFT RIGHT FULLOUTERJOINtable2ON table1 column name table2 column name 叉集 使用CROSSJOIN子句使连接的表产生叉集 叉集和笛卡尔集是相同的 SELECTlast name department nameFROMem

8、ployeesCROSSJOINdepartments 自然连接 NATURALJOIN子句 会以两个表中具有相同名字的列为条件创建等值连接 在表中查询满足等值条件的数据 如果只是列名相同而数据类型不同 则会产生错误 SELECTdepartment id department name location id cityFROMdepartmentsNATURALJOINlocations 自然连接 使用USING子句创建连接 在NATURALJOIN子句创建等值连接时 可以使用USING子句指定等值连接中需要用到的列 使用USING可以在有多个列满足条件时进行选择 不要给选中的列中加上表名前

9、缀或别名 NATURALJOIN和USING子句经常同时使用 SELECTe employee id e last name d location idFROMemployeeseJOINdepartmentsdUSING department id USING子句 使用ON子句创建连接 自然连接中是以具有相同名字的列为连接条件的 可以使用ON子句指定额外的连接条件 这个连接条件是与其它条件分开的 ON子句使语句具有更高的易读性 SELECTe employee id e last name e department id d department id d location idFROMem

10、ployeeseJOINdepartmentsdON e department id d department id ON子句 使用ON子句创建多表连接 SELECTemployee id city department nameFROMemployeeseJOINdepartmentsdONd department id e department idJOINlocationslONd location id l location id 内连接与外连接 在SQL 1999中 内连接只返回满足连接条件的数据 两个表在连接过程中除了返回满足连接条件的行以外还返回左 或右 表中不满足条件的行 这种

11、连接称为左 或右 外联接 两个表在连接过程中除了返回满足连接条件的行以外还返回两个表中不满足条件的行 这种连接称为满外联接 SELECTe last name e department id d department nameFROMemployeeseLEFTOUTERJOINdepartmentsdON e department id d department id 左外联接 SELECTe last name e department id d department nameFROMemployeeseRIGHTOUTERJOINdepartmentsdON e department i

12、d d department id 右外联接 SELECTe last name e department id d department nameFROMemployeeseFULLOUTERJOINdepartmentsdON e department id d department id 满外联接 SELECTe employee id e last name e department id d department id d location idFROMemployeeseJOINdepartmentsdON e department id d department id ANDe manager id 149 增加连接条件

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

最新文档


当前位置:首页 > IT计算机/网络 > 其它相关文档

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