版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、小结: 在关系数据库中,要实现对一个问题的查询,一般首先写出对问题查询的关系代数式,然后再将关系代数式转换成计算机可以接受的SQL语言,以实现在具体数据库管理系统(DBMS)上的数据查询 。 关系代数的八种操作:INTERSECTION(交), UNION(并), DIFFERENCE(差), PRODUCT(笛卡尔积X), SELECTION(选择), PROJECTION(投影 ), JOIN(连接 ), and DIVIDSION(除) 可以完成基本的查询要求。,请举例:用SQL语句描述: SELECT (where)、 PROJECT( )、 PRODUCT (X)、 JOIN ( )
2、操作。,含义?,所有订了货的cname,(customers orders) cname,Select cname from customers c, orders o where c.cid=o.cid,10,(customers orders) where c.cid=o.cid ) cname,用带 exists 的子查询语句可以完成 INTERSECTION()运 算 EXAMPLE 3.4.11 To set cid values of customers who order both products p01 and p07.,The query used was: (Orders
3、 where pid=p01)cid(Orders where pid=p07) cid select distinct cid from orders x where pid =p01 and exists (select * from orders where cid=x.cid and pid = p07 ); select distinct x.cid from orders x, orders y where x.pid =p01 and x.cid =y.cid and y.pid=P07.,or,幻灯片 67,用带not exists的子查询语句可以完成DIFFERENCE(差)
4、 EXAMPLE 3.4.12 To find cid values of customers who do not place any order through agent a03.,CUSTMERScid - (ORDERS where aid= a03 ) cid,select c.cid from customers c where c.cid all (select cid from orders where aid=a03 );,Select c.cid from customers c where not exists (select * from orders x where
5、 x.cid=c.cid and x.aid=a03);,select c.cid from customers c where c.cid not in (select cid from orders where aid=a03 );,幻灯片 67,INTERSECTION(交), DIFFERENCE(差), PRODUCT(笛卡尔积X), SELECTION(选择), PROJECTION(投影 ), and JOIN(连接 ).,UNION(并) and DIVIDSION(除),现在再来看看如何用SQL描述:,我们已经可以很方便地用SQL描述如下关系代数操作:,3.5 UNION O
6、perators and FOR ALL Conditions In Section 3.3 we saw how the SQL Select statement is capable of implementing the relational algebra operations of projection, selection, and product(join). In Section 3.4, we developed the power of the search condition, providing a number of new predicate (in,=all,=s
7、ome,) tests to create search-conditions involving Subqueries, and adding ways to emulate the relational operators of difference(not exist) and intersection (exist) to our bag of tricks for the Select statement.,In the current section, we will see how to perform union and division (which are new capa
8、bilities). This completes the set of relational operators, so it seems we can now express any relational algebra query in SQL form. However, in the next section we will introduce some new SQL operators for performing intersection, difference, and special join operations .,PROJECTION(select ), PRODUC
9、T(from), SELECTION(where), and JOIN,UNION(并) and DIVIDSION(除),现在再来看看如何用SQL描述:,我们已经可以很方便地用SQL描述如下关系代数操作:,INTERSECTION( exists ), DIFFERENCE( not exists ).,The UNION Operator To provide the (UNION operator) of relational algebra, SQL requires a new type of Select syntax. Any number of Subqueries that
10、produce compatible tables can be combined with repeated use of the UNION syntax below: Subquery UNION ALL Subquery,(并)运算,EXAMPLE 3.5.1 We wish to create a list of cities where either a customer or an agent, or both, is based.,This can be accomplished by the following Select Statement:,Dallas Duluth
11、Kyoto New York Newark Tokyo,Customerscity,agents city,select city from customers union select city from agents;,It is conceivable that we would want to see the same city named twice if it fills both roles (more information is in such a result). and in that case we would use the statement select city
12、 from customers union all select city from agents;,Dallas Dallas Duluth Duluth Kyoto,New York Newark Tokyo New York Duluth Dallas,CUSTOMERS,AGENTS,As usual, we allow parenthesis around any expressions, and parentheses might be needed to differentiate between UNION and UNION ALL where three Subquerie
13、s or more are involved. For example consider the query (select city from customers union select city from agents) union all select city from products;,Dallas Newark Duluth Duluth Dallas Dallas Newark,Dallas Duluth Kyoto New York Newark Tokyo,Dallas Duluth Kyoto New York Newark Tokyo,(,),CUSTOMERS,AG
14、ENTS,products,In this case, if the city Chicago appears in customers and products but not in agents, it will appear twice in the result. But it would not appear twice in a Select statement, with the parentheses moved. select city from customers Union select city from agents union all select city fro
15、m products ;,Chicago Dallas Duluth Kyoto New York Newark Tokyo,Chicago Dallas Duluth Kyoto New York Newark Tokyo,Dallas Newark Duluth Duluth Dallas Dallas Newark Chicago,(,),(,),(,),(,),INTERSECTION(交), DIFFERENCE(差), PRODUCT(笛卡尔积X), SELECTION(选择), PROJECTION(投影 ), and JOIN(连接 ), UNION(并).,and DIVID
16、SION(除),现在再来看看如何用SQL描述:,我们已经可以很方便地用SQL描述如下关系代数操作:,Division: SQL FOR ALL. Condition. Suppose that we need to find the cid values of customers who place orders with all agents based in New York.,请些出关系代数式,幻灯片 20,In relational algebra this query is solved by the following expression involving division:
17、ORDERScid,aid(AGENTS where city=NewYork )aid cid,aid(ORDERS) aid(City=New York (AGENTS) Unfortunately, there is no equivalent DIVIDEBY operator in SQL.,交(), 并(), 差(), 笛卡尔积(X), 选择(where), 投影( )和 JOIN 关系代数运算能很容易地转换成 SQL命令,只有除()运算,转换成 SQL命令比较困难。因为SQL没有提供专门描述除()运算的谓词,所以,必须用其它的SQL谓词来表示除()运算。,ORDERS,C001
18、a01, a02,a03, a04,a05, a06 C002 a03, a05 C003 a03 C004 a06 C006 a01, a03,a06,ORDERScid,aid(AGENTS where city=NewYork )aid,AGENTS,C001 a01,a02,a03,a04,a05,a06 C002 a03,a05 C003 a03 C004 a06 C006 a01,a03,a06,all agents based in New York,For readers who are familiar with mathematical logic, the approach we are taking, is based on the following tautology: 3.5.1 z(y p(z,y) z(y p(z,y) To restate 3.5.l in words, the following two statements are equivalent: (1 ) for all z, there exists a y such that the statement p(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年税务系统遴选面试练习题带答案详解(理论版)
- 2026年康复科康复评定与效果分析考核模拟试题及答案解析
- 2027届温州市数学六上期末联考模拟试题含解析
- 贵州省黔南布依族苗族自治州福泉市2027届数学四上期末统考试题含解析
- 2027届泽库县六上数学期末学业水平测试模拟试题含解析
- 安徽省芜湖市镜湖区2027届数学四上期末质量检测试题含解析
- 2027届山东省临沂市郯城县第一实验小学六年级数学第一学期期末质量检测试题含解析
- 2027届江苏省常州市金坛市六年级数学第一学期期末监测模拟试题含解析
- 2027届沁阳市数学六年级第一学期期末调研模拟试题含解析
- 2027届西藏林芝地区察隅县三上数学期末联考模拟试题含解析
- 成都蒲江城市运营管理集团有限公司2026年招聘资产运营岗等岗位的笔试参考试题及答案详解
- 2026年湖北专升本汉语言文学专业(现代汉语中国古代文学)题库附答案
- 2026年四川省拟任县处级领导干部理论(任职资格考试)全真模拟试题及答案
- 2026山东财金富田人才发展有限公司政务服务辅助工作人员招聘1人笔试备考题库及答案解析
- 2026年医用X射线诊断与介入放射学考试题(附答案)
- 江苏盐城市亭湖区2025-2026学年第一学期期末考试八年级物理试题(含答案)
- 2026年吉林公务员考试《行测》题库及答案
- 护理健康教育方法与技巧
- 山东能源集团权属企业兖矿能源集团股份有限公司招聘笔试题库2026
- SD22推土机使用说明书完整版
- 2026潍坊第二人民医院招聘(3人)建设笔试备考试题及答案解析
评论
0/150
提交评论