oracle多表查询PPT课件_第1页
oracle多表查询PPT课件_第2页
oracle多表查询PPT课件_第3页
oracle多表查询PPT课件_第4页
oracle多表查询PPT课件_第5页
已阅读5页,还剩36页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

多表查询 目标 通过本章学习 您将可以 使用等值和不等值连接在SELECT语句中查询多个表中的数据 使用外连接查询不满足连接条件的数据 使用自连接 从多个表中获取数据 EMPLOYEES DEPARTMENTS 笛卡尔集 笛卡尔集会在下面条件下产生 省略连接条件连接条件无效所有表中的所有行互相连接为了避免笛卡尔集 可以在WHERE加入有效的连接条件 笛卡尔集 EMPLOYEES 20行 DEPARTMENTS 8行 EquijoinNon equijoinOuterjoinSelfjoin 连接的类型 CrossjoinsNaturaljoinsUsingclauseFullortwosidedouterjoinsArbitraryjoinconditionsforouterjoins 适用于SQL 1999的连接 Oracle提供的连接 8i或更早 Oracle连接 使用连接在多个表中查询数据 在WHERE字句中写入连接条件 在表中有相同列时 在列名之前加上表名前缀 SELECTtable1 column table2 columnFROMtable1 table2WHEREtable1 column1 table2 column2 等值连接 EMPLOYEES DEPARTMENTS SELECTemployees employee id employees last name 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 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 gradesjWHEREe 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 department 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语法连接 使用连接从多个表中查询数据 SELECTtable1 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 nameFROMemployeesCROSSJOINdepartments 自然连接 NATURALJOIN子句 会以两个表中具有相同名字的列为条件创建等值连接 在表中查询满足等值条件的数据 如果只是列名相同而数据类型不同 则会产生错误 SELECTdepartment id department name location id cityFROMdepartmentsNATURALJOINlocations 自然连接 使用USING子句创建连接 在NATURALJOIN子句创建等值连接时 可以使用USING子句指定等值连接中需要用到的列 使用USING可以在有多个列满足条件时进行选择 不要给选中的列中加上表名前缀或别名 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 idFROMemployeeseJOINdepartmentsdON 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中 内连接只返回满足连接条件的数据 两个表在连接过程中除了返回满足连接条件的行以外还返回左 或右 表中不满足条件的行 这种连接称为左 或右 外联接 两个表在连接过程中除了返回满足连接条件的行以外还返回两个表中不满足条件的行 这种连接称为满外联接 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 id d department id 右外联接 SELECTe last name e department id d department nameFROMemployeeseFULLOUTERJOINdepartmentsdON e department id d depart

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论