从多个表中获取数据.ppt

  1. 1、本文档共26页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
从多个表中获取数据

四.从多个表中获取数据 目标 完成本节课的学习后,应当达到如下目标: 写出能从多个等连接或者非等连接的表中取出数据的SQL语句。 使用外连接来查找数据。 写出一个表与其自身连接的SQL语句。 从多个表中获取数据 什么是连接? 用一个连接来从多个表中获取数据. 在Where子句中书写连接的条件. 如果某个列的名字在多个表中出现了,那么需要在列的名字前面加上表名作为前缀. 笛卡尔积 以笛卡尔积连接的表具有下列特征: 连接条件被忽略。 第一个表中的所有的行与第二个表中的所有行相连接。 如果在WHERE子句中加入条件,那么可以避免笛卡尔积. 产生一个笛卡尔积 连接的类型 什么是等连接? 用等连接获取纪录 限定不明确的列名 用表名作为前缀来限定在多个表中都出现的列名字. 利用表的前缀可以提高性能. 使用不同的别名来标志在不同表中的相同的名字。 用 AND操作符增加查询条件 使用表的别名 使用的表的别名来简化查询 连接多个表 非等连接 用非等连接查询记录 外连接 外连接 可以用外连接来查出在一个表中,不匹配连接条件的行. 外连接的符号是: (+). 使用外连接 自连接 表与自身连接 总结 Cartesian Product When a join condition is invalid or omitted completely, the result is a Cartesian product in which all combinations of rows will be displayed. All rows in the first table are joined to all rows in the second table. A Cartesian product tends to generate a large number of rows, and its result is rarely useful. You should always include a valid join condition in a WHERE clause, unless you have a specific need to combine all rows from all tables. Instructor Note Cartesian product is useful for some tests when you need to generate a large number of rows to simulate a reasonable amount of data. Cartesian Product (continued) A Cartesian product is generated if a join condition is omitted. The example on the slide displays employee name and department name from EMP and DEPT tables. Because no WHERE clause has been specified, all rows (14 rows) from the EMP table are joined with all rows (4 rows) in the DEPT table, thereby generating 56 rows in the output. Instructor Note Demo: l4cart.sql Purpose: To illustrate executing a Cartesian product. SQL SELECT ename, dname 2 FROM emp, dept; ENAME DNAME ---------- -------------- KING ACCOUNTING BLAKE ACCOUNTING ... KING RESEARCH BLAKE RESEARCH ... 56 rows selected. Types of Joins There are two main types of join conditions: Equijoins Non-equijoins Additional join methods include the following: Outer joins Self joins Set operators Note: Set operators are not covered

文档评论(0)

xcs88858 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:8130065136000003

1亿VIP精品文档

相关文档