




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、COMP2311COMP231Relational AlgebraCOMP2312IntroductionQuery languages are specialized languages for asking questions or queries, that involve the data in a databaseRelational algebra: queries in terms of operatorsEvery operator in relational algebra accepts (one or two) relation instances as argument
2、s and returns a relation instance as the result. (1 + 2 = 3)A relational algebra expression is recursively defined to be a relation.Relational algebra is a procedural query languageDefines a step-by-step procedure for computing the answerCOMP2313Relational AlgebraBasic operations:ProjectionSelection
3、Set-differenceUnionCross-productRenameAdditional operations:Intersection, join, division: Not essential, but (very!) useful.Each operation returns a relation, and operations can be composed! COMP2314Projection L(R)Deletes attributes that are not in projection list L.Schema of result contains exactly
4、 the fields in the projection list, with the same names that they had in the (only) input relation.Projection operator eliminates duplicates! MakerModel_NoAirbusA310AirbusA320AirbusA330AirbusA340MDDC10MDDC9Maker(Plane)PlaneMakerAirbusMDCOMP2315Selection c(R)Selects rows (records/tuples) that satisfy
5、 a selection condition c.Schema of result identical to schema of (only) input relation. A condition c has the form: Term Op Termwhere Term is an attribute name or a constantOp is one of , =, , etc.Different conditions can be linked together with a boolean expression.(C1 C2), (C1 C2), ( C1) are condi
6、tions where C1 and C2 are conditions. means AND means OR means NOTCOMP2316Selection exampleThe resulting relation can be the input for another relational algebra operation! (Operator composition)MakerModel_NoAirbusA310AirbusA320AirbusA330AirbusA340MDDC10MDDC9PlaneMaker=“MD”(Plane)MakerModel_NoMDDC10
7、MDDC9MakerModel_NoAirbusA310AirbusA320A320AirbusA330AirbusA340MDDC10MDDC9PlaneModel_No(Maker=“MD”(Plane)Model_NoDC10DC9COMP2317Set OperationsUnion, Intersection, Set-Difference These three operations take two input relations, which must be union-compatible:Same number of fields.Corresponding fields
8、have the same type.Output is a single relation (that does not contain duplicates)COMP2318Set operations - UnionPlane1 Plane2MakerModel_NoAirbusA310AirbusA320AirbusA330AirbusA340MDDC10MDDC9MakerModel_NoBoeingB727BoeingB747BoeingB757MDDC10MDDC9=MakerModel_NoAirbusA310AirbusA320AirbusA330AirbusA340Boei
9、ngB727BoeingB747BoeingB757MDDC10MDDC9COMP2319Set operations Set differencePlane1 Plane2MakerModel_NoAirbusA310AirbusA320AirbusA330AirbusA340MDDC10MDDC9MakerModel_NoBoeingB727BoeingB747BoeingB757MDDC10MDDC9=MakerModel_NoAirbusA310AirbusA320AirbusA330AirbusA340COMP23110Set operations - IntersectionPla
10、ne1 Plane2MakerModel_NoAirbusA310AirbusA320AirbusA330AirbusA340MDDC10MDDC9MakerModel_NoBoeingB727BoeingB747BoeingB757MDDC10MDDC9=MakerModel_NoMDDC9MDDC10COMP23111Cartesian ProductCombines each row of one table with every row of another tableCan_fly PlaneEmp_NoModel_No1001B7271001B7471001DC101002A320
11、1002A3401002B7571002DC91003A3101003DC9MakerModel_NoAirbusA310AirbusA320AirbusA330AirbusA340BoeingB727BoeingB747BoeingB757MDDC10MDDC981 t-uples!Emp_NoModel_NoMakerModel_No1001B727AirbusA3101001B727AirbusA3201001B727AirbusA3301001B727AirbusA3401001B727BoeingB7271001B727BoeingB7471001B727BoeingB7571001
12、B727MDDC101001B727MDDC91001B747AirbusA3101001B747AirbusA3201001B747AirbusA3301001B747AirbusA3401001B747BoeingB7271001B747BoeingB7471001B747BoeingB7571001B747MDDC101001B747MDDC91001B727AirbusA3101001B727AirbusA320=COMP23112JoinGenerating all possible combinations of tuples is not usually meaningful.
13、In the previous example, it makes more sense to combine each tuple of Can_Fly with the corresponding record of the Plane. Join is a cartesian product followed by a selection: R1 R2 = R1.model_no = R2.model_no(R1 R2)Emp_noModel_NoMakerModel_NoCOMP23113Natural Join ExampleCan_fly Plane Emp_NoModel_No1
14、001B7271001B7471001DC101002A3201002A3401002B7571002DC91003A3101003DC9MakerModel_NoAirbusA310AirbusA320AirbusA330AirbusA340BoeingB727BoeingB747BoeingB757MDDC10MDDC9=Emp_NoModel_NoMaker1003A310Airbus1002A320Airbus1002A340Airbus1001B727Boeing1001B747Boeing1002B757Boeing1001DC10MD1002DC9MD1003DC9MDCOMP2
15、3114-Join ExampleWe have a Flight table that records the Flight Number, Origin, Destination, Departure Time and Arrival Time. We join this table with itself (self-join) using the condition:(Flight1.Dest = Flight2.Origin) ( Flight1.Arr_Time Flight2.Dept_Time)What should we get? NumOriginDestDep_TimeA
16、rr_Time334ORDMIA12:0014:14335MIAORD15:0017:14336ORDMIA18:0020:14337MIAORD20:3023:53394DFWMIA19:0021:30395MIADFW21:0023:43NumOriginDestDep_TimeArr_Time334ORDMIA12:0014:14335MIAORD15:0017:14336ORDMIA18:0020:14337MIAORD20:3023:53394DFWMIA19:0021:30395MIADFW21:0023:43 COMP23115-Join Example (cont)Flight
17、1.Dest = Flight2.Origin Flight1.Arr_Time N1, Nn - Nn), R)The new relation R has the same instance as R, but its schema has attribute Ni instead of attribute NiE.g., (Staff(Name - Family_Name, Salary - Gross_salary), Employee)Necessary if we need to perform a cartesian product or join of a table with
18、 itselfNameSalaryEmp_NoClark1500001006Gates50000001005Jones500001001Peters450001002Phillips250001004Rowe350001003Warnock5000001007Family_NameGross_SalaryEmp_NoClark1500001006Gates50000001005Jones500001001Peters450001002Phillips250001004Rowe350001003Warnock5000001007EmployeeStaffCOMP23117DivisionLet
19、A have two attributes x and yLet B have one attribute yA/B contains all x tuples, such that for every y tuple in B there is a xy tuple in Axys1p1s1p2s1p3s1p4s2p1s2p2s3p2s4p2s4p4Ap4p2yBs4s1xA/B/=COMP23118DivisionFind all student IDs (sids) of the students who took all courses in table Coursesidcid123
20、111701111100122312001317042314170170231cid41sid/=TakeCourseTake / CourseCOMP23119DivisionFind all student IDs (sids) of the students who took all courses provided by CSEsidcid123111701111100122312001317042314170TakeCourseTake / cid(dept = “COMP” (Course)ciddept231CSE170CSE001LANG111ECE123ECECOMP2312
21、0Additional Operators - Outer JoinAn extension of the join operation that avoids loss of information.Computes the join and then adds tuples from one relation that do not match tuples in the other relation to the result of the join.COMP23121Outer Join - Example Relation loan Relation borrowerbranch-n
22、ameloan-numberamountDowntownRedwoodPerryridgeL-170L-260L-230300017004000cust-nameloan-numberJonesHayesSmithL-170L-230L-155COMP23122Outer Join - Exampleloanbranch-nameloan-numberamountDowntownRedwoodPerryridgeL-170L-260L-230300017004000borrowercust-nameloan-numberJonesHayesSmithL-170L-230L-155branch-
23、nameloan-numberamountDowntownRedwoodL-170L-23030004000cust-nameJonesSmithLoan Borrower Join returns only the matching (or “good”) tuplesThe fact that loan L-260 has no borrower is not explicit in the resultHayes has borrowed an non-existent loan L-155 is also undetectedCOMP23123Left Outer Join -Exam
24、ple Left outer join: Loan borrowerKeep the entire left relation (Loan) and fill in informationfrom the right relation, use null if information is missing. branch-nameloan-numberamountDowntownRedwoodPerryridgeL-170L-260L-230300017004000cust-nameJonesSmithnullloanbranch-nameloan-numberamountDowntownRedwoodPerryridgeL-170L-260L-230300017004000borrowercust-nameloan-numberJonesHayesSmithL-170L-230L-155COMP23124Right Outer Join - exampleRight outer join: Loan Bo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 天津音乐学院《大数据安全》2023-2024学年第二学期期末试卷
- 河南科技职业大学《数据库原理及应用》2023-2024学年第二学期期末试卷
- 河北交通职业技术学院《中国传统音乐说唱与戏曲》2023-2024学年第一学期期末试卷
- 广东工商职业技术大学《临床药物治疗学》2023-2024学年第二学期期末试卷
- 2025年浙江省绍兴市诸暨市五下数学期末调研模拟试题含答案
- 海南健康管理职业技术学院《会计信息财务链系统》2023-2024学年第二学期期末试卷
- 江苏泰州地区2025届初三第三轮考试化学试题含解析
- 常州大学怀德学院《建筑与装饰工程计量与计价》2023-2024学年第二学期期末试卷
- 2025届上海市宝山区淞浦中学联考第一次诊断性考试物理试题含解析
- DB1407T 58-2025 核桃林下艾草种植技术规程
- 社工证笔试题库及答案
- 高考写作专项突破之核心概念阐释要诀 课件
- 2025年全国质量月活动总结参考(2篇)
- 口腔四手操作培训
- 2025年月度工作日历含农历节假日电子表格版
- 第37章 真菌学概论课件
- 总裁助理岗位职责
- 2024年封顶仪式发言稿模版(3篇)
- 癌症治疗协议书范例
- 《中华人民共和国机动车驾驶人科目一考试题库》
- 小学体育课件《立定跳远课件》课件
评论
0/150
提交评论