资源描述:
《南开大学数据库原理课件lecture6-Relational Algebra.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、关系查询语言(RelationalQueryLanguages)Formal:RelationalAlgebra,RelationalCalculus,DatalogPractical:SQL,QUEL,QBE(Query-by-Example)Whatisarelationalquery?Input:anumberofrelationsinyourdatabaseOutput:onerelationastheanswer关系代数(RelationalAlgebra)Union,intersection,anddifferenceSele
2、ction:pickingcertainrows.Projection:pickingcertaincolumns.Productsandjoins:compositionsofrelations.Renamingofrelationsandattributes.BasicSetOperationsUnionR:=R1∪R2IntersectionR:=R1∩R2DifferenceR:=R1-R2OperandsmusthavethesameschemaProductPaireachtuplet1ofR1witheachtuplet2o
3、fR2.Concatenationt1t2isatupleofR3.SchemaofR3istheattributesofR1andthenR2,inorder.ButbewareattributeAofthesamenameinR1andR2:useR1.AandR2.A.ExampleR(ABC)257269367S(ABC)269289367R∪S(ABC)257269367289RS(ABC)257R∩S(ABC)269367RS(ABCABC)25726925728925736926926926928926936736726
4、9367289367367SelectionCisacondition(asin“if”statements)thatreferstoattributesofR2.R1isallthosetuplesofR2thatsatisfyC.TheExampleofSelectionSellsbarbeerpriceJoe’sBud2.50Joe’sMiller2.75Sue’sBud2.50Sue’sMiller3.00barbeerpriceJoe’sBud2.50Joe’sMiller2.75SelectOperation–ExampleR
5、elationrABCD15122377310A=B^D>5(r)ABCD123710CompositionofOperationsCanbuildexpressionsusingmultipleoperationsExample:A=C(rxs)rxsA=C(rxs)AB11112222CD1019201010102010EaabbaabbABCDE122102020aabProjectionLisalistofattributesfromtheschemaof
6、R2.R1isconstructedbylookingateachtupleofR2,extractingtheattributesonlistL,intheorderspecified,andcreatingfromthosecomponentsatupleforR1.Eliminateduplicatetuples,ifany.TheExampleofProjectionSellsbarbeerpriceJoe’sBud2.50Joe’sMiller2.75Sue’sBud2.50Sue’sMiller3.00beerpriceBud
7、2.50Miller2.75Miller3.00Theta-JoinTaketheproductThenapplytotheresult.Ccanbeanyboolean-valuedcondition.HistoricversionsofthisoperatorallowedonlyAB,whereis=,<,etc.;hencethename“theta-join.”CSells(bar,beer,price)Bars(name,addr)Joe’sBud2.50Joe’sMapleSt.Joe’sMiller2.75Sue’sR
8、iverRd.Sue’sBud2.50Sue’sCoors3.00BarInfo:=SellsSells.bar=Bars.nameBarsbar,beer,price,name,addrJo