资源描述:
《数据库系统基础教程.ppt》由会员上传分享,免费在线阅读,更多相关内容在PPT专区-天天文库。
1、AlgebraicandLogicalQueryLanguagesInthischapter,weextendtheset-basedalgebratobags,whichbetterreflectthewaytherelationalmodelisimplementedinpractice.Wealsoextendthealgebrasoitcanhandleseveralmoreoperations.Contents2Whatisabag(包)?Abagmeansarelationthatmay(ormaynot)haveduplicatetuples.Asetmeansarel
2、ationwithoutduplicatetuples.Whybags?SQL,themostimportantquerylanguageforrelationaldatabases,isactuallyabaglanguage.Someoperations,likeprojectionandunion,aremuchmoreefficientonbagsthansets.5.1RelationalOperationsonBags3SupposethatRandSarebags,andthattupletappearsntimesinRandmtimesinS.BagUnion(并)I
3、nthebagunionR∪S,tupletappearsn+mtimes.Example:{1,2,1}∪{1,1,2,3,1}={1,1,1,1,1,2,2,3}5.1.2Union,Intersection,andDifferenceofBags4BagIntersection(交)InthebagintersectionR∩S,tupletappearsmin(n,m)times.Example:{1,2,1,1}∩{1,2,1,3}={1,1,2}5.1.2Union,Intersection,andDifferenceofBags5BagDifference(差)Inthe
4、bagDifferenceR–S,tupletappearsmax(0,n-m)times.Example:{1,2,1,1}–{1,2,3}={1,1}{1,2,3}–{1,2,1,1}={3}5.1.2Union,Intersection,andDifferenceofBags6AlgebraicLawsforBagsSome,butnotallalgebraiclawsthatholdforsetsalsoholdforbags.Example:R∪S=S∪R.However,thereareanumberofotherlawsthatholdwhenrelationalalge
5、braisappliedtosetsbutthatdonotholdwhenrelationsareinterpretedasbags.Example:S∪S=S(R∪S)–T=(R–T)∪(S–T)5.1.2Union,Intersection,andDifferenceofBags7RAB125612πA(R)=A151Projectionappliestoeachtuple,butasabagoperator,wedonoteliminate(消除)duplicates.Example:5.1.3ProjectionofBags8RAB125612σA+B<5(R)=AB1212
6、Selectionappliestoeachtuple,soitseffectonbagsislikeitseffectonsets.Example:5.1.4SelectiononBags9Eachtupleofonerelationispairedwitheachtupleoftheother,regardlessofwhetheritisaduplicateornot.IfatuplerappearsinarelationRmtimes,andtuplesappearsntimesinrelationS,thenintheproductR×S,thetuplerswillappe
7、arm×ntimes.5.1.5ProductofBags10Example:5.1.5ProductofBagsRABSBC1234567812R×S=AR.BS.BC12341278563456781234127811Wecompareeachtupleofonerelationwitheachtupleoftheother,decidewhetherornotthispairoftuplesjoinssuccessfully,andifs