版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
6.1therelationalalgebra
(oneofformalrelationalquerylanguages)TheformalrelationalquerylanguagesTheformallanguagesTherelationalalgebraTuplerelationalcalculusThedomainrelationalcalculusTherelationalgebraformsthebasisofthewidelyusedSQLquerylanguage.TherelationalalgebraTherelationalalgebraisaprocedurallanguage.Theuserinstructsthesystemtoperformasequenceofoperationsonthedatabasetocomputethedesiredresult.Nonprocedurallanguage:Theuserdescribesthedesiredinformationwithoutgivingaspecificprocedureforobtainingthatinformation.TherelationalAlgebraSixbasicoperatorsselect:project:union:setdifference:–
Cartesianproduct:xrename:AdditionalrelationalalgebraoperationsSet-intersection∩Natural-joinTherelationalAlgebraTheoperatorstakeoneortworelationsasinputsandproduceanewrelationasaresult.1)SelectOperationTheselectoperationselectstuplesthatsatisfyagivenpredicate(谓词).
σRelationrA=B^D>5
(r)1)SelectOperationNotation:
p(r),pisselectionpredicateDefinedas:
p(r)={t|trandp(t)}Wherepisaformulainpropositionalcalculus(命题演算)consistingoftermsconnectedby:(and),(or),(not)Eachtermisoneof:<attribute>op <attribute>or<constant>whereopisoneof:=,,>,.<.
1)SelectOperationExampleofselection:
dept_name=“Physics”(instructor)ExampleofselectionoperationSnoSnameSsexSageSdept95001李勇男20CS95002刘晨女19IS95003王敏女18MA95004张立男19IS
StudentEx1:RetrieveallstudentsfromIS.Ex2:Retrieveallstudentswhoareunder20yearsofage.Exampleofselectionoperation
Ex3:σ2>4∧1=′a′(R)2)ProjectOperationTheprojectoperationisaunaryoperationthatreturnsitsargumentrelation,withcertainattributesleftout.Example:
Relationr:A,C(r)π2)ProjectOperationNotation:
whereA1,A2,……..areattributenamesandrisarelationname.
ExampleofprojectionoperationSnoSnameSsexSageSdept95001李勇男20CS95002刘晨女19IS95003王敏女18MA95004张立男19IS
StudentEx1:Retrievenameanddeptofallstudents.Ex2:Retrievealldeptsinstudentrelation.
EX:π3,1(R)=Exampleofprojectionoperation3)UnionOperationTheunionoperationisabinaryoperationthatgettuplesthatappearineitherorbothofthetworelations.Example:
URS3)UnionOperationNotation:r
sDefinedas:
r
s={t|trorts}Forr
stobevalid.r,
smusthavethesamearity(数量)(samenumberofattributes)2.Thedomainsoftheithattributeofrandtheithattributeofsmustbethesame,foralli.3)UnionOperation–ExampleRelationsr,s:rs:Example:tofindallcoursestaughtintheFall2009semester,orintheSpring2010semester,orinbothsectioncourse_id(
semester=“Fall”Λyear=2009(section))
course_id(
semester=“Spring”Λyear=2010(section))Answer:Answer:4)SetDifferenceOperationTheset-differenceoperationallowsustofindtuplesthatareinonerelationbutarenotinanother.Theexpressionr-sproducesarelationcontainingthosetuplesinrbutnotins.4)SetDifferenceOperationNotation:r–sDefinedas:
r–s={t|t
randts}Setdifferencesmustbetakenbetweencompatiblerelations.randsmusthavethesamearityattributedomainsofrandsmustbecompatibleExample:tofindallcoursestaughtintheFall2009semester,butnotintheSpring2010semester.Example:
course_id(
semester=“Fall”Λyear=2009(section))−
course_id(
semester=“Spring”Λyear=2010(section))Example2Relationsr,s:r–s:5)Cartesian-ProductOperationTheCartesian-productoperationallowsustocombineinformationfromanytworelations.5)Cartesian-ProductOperationNotation:rxsDefinedas:
rxs={tq|trandqs}
Assumethatattributesofr(R)ands(S)aredisjoint.(Thatis,RS=).Ifattributesofr(R)ands(S)arenotdisjoint,thenrenamingmustbeused.5)Cartesian-ProductOperation–ExampleRelationsr,s:rx
s:exampleABCa1b1c1a1b2c2a2b2c1DEFa1b2c2a1b3c2a2b2c1RSABCa1b1c1a1b1c1a1b1c1a1b2c2a1b2c2a1b2c2a2b2c1a2b2c1a2b2c1DEFa1b2c2a1b3c2a2b2c1a1b2c2a1b3c2a2b2c1a1b2c2a1b3c2a2b2c1RxsR1∪R2,R1-R2,R2-R1,R1∩R2R1×R2,R1-R3,R1×R3
6)CompositionofOperationsCanbuildexpressionsusingmultipleoperationsExample:A=C(rxs)rxs7)RenameOperationAllowsustoname,andthereforetoreferto,theresultsofrelational-algebraexpressions.Allowsustorefertoarelationbymorethanonename.Example:
x(E)
returnstheexpressionEunderthenameX7)RenameOperationIfarelational-algebraexpressionEhasarityn,then
returnstheresultofexpressionEunderthenameX,andwiththeattributesrenamedtoA1
,A2
,….,An
.exampleFindthelargestsalaryintheuniversityAnswer:Step1:findinstructorsalariesthatarelessthansomeotherinstructorsalary(i.e.notmaximum)usingacopyofinstructorunderanewnamedinstructor.salary(
instructor.salary<d,salary
(instructorxd
(instructor)))Step2:Findthelargestsalarysalary(instructor)–
instructor.salary
(
instructor.salary<d,salary
(instructorxd
(instructor)))
Example2FindthenamesofallinstructorsinthePhysicsdepartment,alongwiththecourse_idofallcoursestheyhavetaughtQuery1:instructor.ID,course_id
(dept_name=“Physics”(
instructor.ID=teaches.ID(instructorxteaches)))Query2instructor.ID,course_id
(instructor.ID=teaches.ID
(
dept_name=“Physics”(instructor)xteaches))AdditionalOperationsWedefineadditionaloperationsthatdonotaddanypowertotherelationalalgebra,butthatsimplifycommonqueries.SetintersectionNaturaljoinAssignmentOuterjoin8)Set-IntersectionOperationNotation:r
sDefinedas:r
s={t|t
randt
s}Assume:r,shavethesamearity
attributesofrandsarecompatibleNote:r
s=r–(r–s)8)Set-IntersectionOperation–ExampleRelationr,s:r
s9)Natural-JoinOperationThenatural-joinisabinaryoperationthatallowsustocombinecertainselectionandacartesianproductintooneoperation.Thenatural-joinoperationformsaCartesianproductofitstwoarugments,performsaselectionforcingequalityonthoseattributesthatappearinbothrelationschemas,andfinallyremovesduplicateattributes.
Notation:rs9)Natural-JoinOperationrsisarelationonschemaR
Sobtainedasfollows:R
S={trts|trR∧tsS∧tr[B]=ts[B]}9)Natural-JoinOperationExample:R=(A,B,C,D)S=(E,B,D)Resultschema=(A,B,C,D,E)r
sisdefinedas:
r.A,r.B,r.C,r.D,s.E(r.B=s.Br.D=s.D(rxs))9)NaturalJoinExampleRelationsr,s:rsexampleFindthenamesofallinstructorsintheComp.Sci.departmenttogetherwiththecoursetitlesofallthecoursesthattheinstructorsteach.Analysis:Seediagraminword……
name,title(
dept_name=“Comp.Sci.”(instructor
teaches
course))9)NaturalJoinNaturaljoinisassociative(instructorteaches)courseisequivalentto
instructor(teachescourse)Naturaljoiniscommutativeinstructteachesisequivalentto
teachesinstructorexampleABCa1b15a1b26a2b38a2b412BEb13b27b310b32b52RSR
S
ABCEa1b153a1b267a2b3810a2b382ThethetajoinThethetajoinoperationisavariantofthenatural-joinoperationthatallowsustocombineaselectionandaCartesianproductintoasingleoperation.Notation:rs=ᶱ(rxs)ᶱexampleABCa1b15a1b26a2b38a2b412BEb13b27b310b32b52RSR
S
C<EAR.BCS.BEa1b15b27a1b15b310a1b26b27a1b26b310a2b38b310ModificationoftheDatabaseThecontentofthedatabasemaybemodifiedusingthefollowingoperations:DeletionInsertionUpdatingAlltheseoperationscanbeexpressedusingtheassignmentoperatorAssignmentoperationTheassignmentoperation()providesaconvenientwaytoexpresscomplexqueries.Writequeryasasequentialprogramconsistingofaseriesofassignmentsfollowedbyanexpressionwhosevalueisdisplayedasaresultofthequery.Assignmentmustalwaysbemadetoatemporaryrelationvariable.exampletemp1←RxStemp2←r.A1=s.A1^r.A2=s.A2^…^r.An=s.An(temp1)Result=
RUS(temp2)
OuterjoinAnextensionofthejoinoperationthatavoidslossofinformation.Computesthejoinandthenaddstuplesformonerelationthatdoesnotmatchtuplesintheotherrelationtotheresultofthejoin.Usesnullvalues:nullsignifiesthatthevalueisunknownordoesnotexistAllcomparisonsinvolvingnullare(roughlyspeaking)falsebydefinition.WeshallstudyprecisemeaningofcomparisonswithnullslaterOuterjoinexampleRelationinstructor1Relationteaches1IDcourse_id101011212176766CS-101FIN-201BIO-101Comp.Sci.FinanceMusicIDdept_name101011212115151nameSrinivasanWuMozartOuterjoinexampleLeftOuterJoininstructorteachesJoininstructorteachesIDdept_name1010112121Comp.Sci.Financecourse_idCS-101FIN-201nameSrinivasanWuIDdept_name101011212115151Comp.Sci.FinanceMusiccourse_idCS-101FIN-201nullnameSrinivasanWuMozartOuterjoinexampleFullOuterJoininstructorteachesRightOuterJoininstructorteachesIDdept_name101011212176766Comp.Sci.Financenullcourse_idCS-101FIN-201
BIO-101nameSrinivasanWunullIDdept_name10101121211515176766Comp.Sci.FinanceMusicnullcourse_idCS-101FIN-201
null
BIO-101nameSrinivasanWuMozartnullAggregationfunctionAggregationfunctiontakesacollectionofvaluesandreturnsasinglevalueasaresult.
avg:averagevalue
min:minimumvalue
max:maximumvalue
sum:sumofvalues
count:numberofvaluesAggregationfunctionAggregateoperationinrelationalalgebra
Eisanyrelational-algebraexpressionG1,G2…,Gnisalistofattributesonwhichtogroup(canbeempty)EachFi
isanaggregatefunctionEachAi
isanattributenameexampleRelationr:ABC77310
sum(c)(r)sum(c)27
avg(c)(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年秋新教科版八年级上册物理教学课件 第4章 光的世界 5 科学探究:凸透镜成像
- 2024年秋新冀教版三年级上册英语教学课件 U4L2
- 2024年秋新华师大版七年级上册数学课件 第1章 有理数 1.10 有理数的除法
- 王者荣耀策划书
- 学前儿童社会教育活动指导 课件 项目十学前儿童问题行为及干预技术
- 2024AI Agent行业研究报告
- 《 河北省突发公共事件信息管理问题研究》
- 部编版一年级语文上册期末检测卷二(全优考卷新编)
- 2024届湖南省天壹名校联盟高三下学期4月大联考物理试题(解析版)
- 2023年锡材项目分析评估报告
- 家电以旧换新风险识别与应对措施
- 人教版(新插图)六年级上册数学全册教学课件
- 幼儿生活活动保育(学前教育专业)PPT完整全套教学课件
- JJG 892-2022 验光仪检定规程-(高清最新版)
- 人教版八年级上册英语 Unit 1 单元测试卷(含听力音频)
- 北师大版九年级数学上册教案(全册完整版)教学设计含教学反思
- 高压旋喷桩止水帷幕施工方案
- 高速公路隧道施工方案及步骤(完整版)
- 租房协议简单一页
- 六年级上册书法练习指导教案
- 机床设备基础施工方案
评论
0/150
提交评论