Published on

Introduction to database lecture# 4: This lecture is all about Relational Algebra and its theory etc

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Relational Algebra1Fall 2001 Database Systems 1Relational Algebra• A relation is a set of tuples. Each relationalalgebra operation takes as input a list ofrelations and produces a single relation.• General form:OperatorArguments (List of Relations) New Relation• After each operation, the remaining attributesare carried to the new relation. The attributesmay be renamed, but their domain remains thesame.Fall 2001 Database Systems 2Set Theoretic Operations• Regular set operations on relations• A set operation requires two participating relations R andS to be compatible– R and S should have the same attributes• R(Name:D1, Email:D2)• S(Name:D1, Email:D2, Address:D3)• T(Name:D1, Email:D4)• V(Name:D1, Email:D2)Which relations above are union (set operation)compatible?– Union compatibility may require type conversion(casting).
  2. 2. Relational Algebra2Fall 2001 Database Systems 3Set Operations ¢¡¤£¦¥ §©¨£ ¥¡ ! #¤$%¥()0¥$¡1$) 243©5 673SR ∪8 9 @BADC7E F©GIHQP©RDHSR©T¤FF 5H QPUFVT 5 3 8 6 T 9W 3 H¤FVTGQFVXYH 56 V3SR ∩8 9 @BADC7E F©GIHQP©RDHSR©T¤F` 6 HDP 5 3 8aR 3Yb 9c 5d Qd FVT¤F 3 XQFSR −8 9 @BADC7E F©GIHQP©RDHSR©T¤F 5 38e`¦AQH 36 H 5 3 9ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiFall 2001 Database Systems 4pYq¢rs0t%u pQvw r¤x€y¤ ‚yx0vƒ„Y… †(‡Yˆ‰  Smith@aol.com„Y‘ ’“€”€•—– Johns@geocities.com˜™ d0e “gf)• Brown@netmail.com˜h i1e j—k Gray@microsoft.coml0m)n)o0p0q l‚r s0tu vw‚xy o0x w0tz{%| })~  € Gray@microsoft,com{% ‚‚ƒ‚„g…‚~ † ‡ Robert432@aol.com{0ˆ ‰ƒgŠ‚‹—‡ Johns@somewhere.com{€Œ ‰—‡ ƒg‹ Jj@yahoo.com{0 Ž(g—… Duke@jazz.com‘g’¤“0”Q• ‘0–— ˜Q–— ™š‚›œ —–ž—Ÿ  g¡ œ ¢g£gž š€Ÿ—–£g¤¥%¦ §Y¦ ¨)© ª‚«¬‚­ ®€¯€­ ° ¯g±¥%² §Y² ³1´µgµ€´± ¶U®%´· ¸ ¹ ¯gº—­ ° ¯€±¥0» §Y² ¼¬%«¬‚­ © ¯gµ—¬ ½¤¬€¾¦¿¯€· À¥€Á §» ‚¯g䵗ª—­ ­ ³ª‚« «ª—­Ä0Å ÆÇ ÈÉ Ê€Ëg̤͂ʂΗςÐɀÑÒ Ó—Ô Ô Ê ÕÖÓ—× ×—ØÖÄ0Ù ÆYÚ Æ¤Û€Ñ˗ÜÝɀÑÛÖ ÞÊ€Ô × Êgßà‚áâ%ã à%äå â%æ€ç è á‚äå à0é—êgá€å ë1ì¤í—égî çï0ðñ%ò ó0ô—õ€ö ÷%ò ïø òùï0ðñ%ø ó0ô—õ€ö ÷gú ï%ù û0ùï0ðñ0õ ó0ô—õ%ò ÷%ò ï€ú òóï0ðñgú ü%ôgò ÷0ù ï%ù ý%ùï0ðñ0ù ü%ôgò ÷%ò ïø øgöï0ðñ0û ü%ôgø ÷gú ï€ú ý‚öï0ðñ‚ý ü%ôgø ÷%ø ïò úï0ðñ0ó ü%ô—õ ÷%ø ï%õ ù
  3. 3. Relational Algebra3Fall 2001 Database Systems 5Assignment and Alias• Given a relation R, head(R)=A1…An is the list of attributesin R.• For each attribute, dom(A) gives the domain of A,name(A) gives the name of the attribute• Suppose B1…Bn is a list of attributes such that for each i,dom(Bi) = dom(Ai)Then assignment (:=) changes the names of some or all ofthe attributes in R and results in a new relation S,S(B1…Bn) := R(A1…An)and dom(B1) = dom(A1) … dom(Bn) = dom(An).Fall 2001 Database Systems 6Set Operations• Find all people who are registered as buyers or owners ¢¡¤£¦¥¨§© ! ! #$% )(10 243 Smith@aol.com$65 798@3¤ACB Johns@geocities.comDE% F¦GIHCP Gray@microsoft.comQRQRQ QRQ@Q QRQRQBUYERS-2(oid,name,email) :=BUYERS(buyid,name,email)RESULT := BUYERS-2 ∪ OWNERS
  4. 4. Relational Algebra4Fall 2001 Database Systems 7Set Operations• Find all people who are owners but not buyers ¢¡¤£¦¥¨§© ! !¤ #$% )(0 132 Smith@aol.com$4 5768293@ Johns@geocities.com$¨A B¦CED3F Gray@microsoft.comG G GBUYERS-2(oid,name,email) :=BUYERS(buyid,name,email)RESULT:= OWNERS - BUYERS-2Fall 2001 Database Systems 8Set Operations• Find all people who are both owners and buyersHPI7QSRUTV WYX ` acb7dUe e7dUbfXhgBUYERS-2(oid,name,email) :=BUYERS(buyid,name,email)RESULT := OWNERS ∩ BUYERS-2
  5. 5. Relational Algebra5Fall 2001 Database Systems 9Projection• Select all tuples from relation R, restrict each tuple to theinput attributes– written as R[A1…An] orfor attributes A1…An from R– meaningnAA ...1πfor each tuple in Rconstruct a new tuple containingonly attributes A1…Anplace the new tuple in the resultend-forremove duplicate tuples to preservethe set propertyFall 2001 Database Systems 10Projection• Find the names and email addresses of people who areboth buyers and owners ¢¡¤£¦¥¨§© !#$%(0) G ra y @ m ic ro s o ft.c o mRESULT:= OWNERS[name,email] ∩ BUYERS[name,email]
  6. 6. Relational Algebra6Fall 2001 Database Systems 11Projection• Find all the items on which at least one bid was placed ¢¡¤£¦¥¨§© !#RESULT := BIDS[iid]Fall 2001 Database Systems 12Selection• Select certain tuples from a relation R based on a booleancondition C– written as R where C or– meaning:RCσfor each tuple t in Rif t makes C true thenreturn t in the resulting relationend-for
  7. 7. Relational Algebra7Fall 2001 Database Systems 13Selection• The selection condition C is built of– atomic conditions of the form Ai ∝ Aj or Ai ∝ c where• Ai, Aj are attributes and c is a constant of the same domain ofvalues, and• ∝ is a comparison predicate of the form: =, , , = , = , etc.• If C1, C2 are selection conditions, then so are• C1 AND C2• C1 OR C2• NOT C1Fall 2001 Database Systems 14Selection• Find the bid identifiers of bids for item number “I1”: ¢¡¤£¦¥¨§© ! $# ¤%I1-BIDS := BIDS WHERE iid = “I1”RESULT := I1-BIDS[bid]
  8. 8. Relational Algebra8Fall 2001 Database Systems 15Selection• Find the bid identifiers of bids for item number “I1” that arestrictly more than $15.I1-BIDS := BIDS WHEREiid = “I1” AND amount 15RESULT := I1-BIDS[bid] ¢¡¤£¦¥¨§© !#Fall 2001 Database Systems 16Selection• Find the dates in which a bid for item number “I1” or“I5” was placedI1orI5-BIDS := BIDS WHEREiid = “I1” OR iid = “i5”RESULT := I1orI5-BIDS[date]$%(#)¤021 35426879A@2BDC9A@2BFEG(@HE
  9. 9. Relational Algebra9Fall 2001 Database Systems 17Cartesian Product• Multiply the relations R and S, producing |R|x|S| manytuples with attributes head(R)+head(S)• Cartesian product combines two relations into one “big”relation• meaningfor each tuple r in R dofor each tuple s in S docompute a new tuple containingall attributes in r and send-forend-forFall 2001 Database Systems 18R := Bids WHERE Amount 20 T := Items WHERE oid=“O2”S := T[iid,name]P := R X S  ¡£¢ ¤ ¥§¦©¨ ¢ ¤ ¡¤ !#©$¨% ( )0213 465 %7 8£7% 5 9@0A 47 %7 BC7% @8 9@02( 465 %5 B3D EF G HPIQSRT@U VXW Y2YW `badc£W egfh@i p£q£r qstuY6qv w§x€y‚ ƒ w§x „£…†b‡ w§x ˆ ƒ w‰x€y2g‘2ˆ6ƒ w§x ’§“X”•–† —Sx ˆ ƒ —Sx ˜#…©“X‡™d e@f g2h6i6j kl ™m n‚m k2f oCd pp©d qgr™d ste™d e©l u2h©v k2m ™m w@m k2f oCd pp©d qgr™d ste™d en u2h©f kl ™l wj k2f oCd pp©d qgr™d ste™d e@f g2h6i6j kl ™m n‚m k©i x‚y@zy©{b|–}2pgy™d e©l u2h©v k2m ™m w@m k©i x‚y@zy©{b|–}2pgy™d en u2h©f kl ™l wj k©i x‚y@zy©{b|–}2pgy
  10. 10. Relational Algebra10Fall 2001 Database Systems 19Relational Algebra - Join• For all pairs of tuples r and s from relations R and S whereHead(R)=A1…AnB1…Bk and Head(S)=B1…BkC1…Cmsuch that r[B1…Bk] = s[B1…Bk],include a tuple in R ¢¡ S withattributes A1…AnB1…BkC1…Cm• The attributes in common between the two relations (B1…Bk) arecalled the “join attributes”• The join is a cartesian product followed by a selection on the joinattributes, and a projection to remove the duplication of the joinattributes [formally: you have to rename the join attributes in onerelation so that you can express the selection/projectionoperations]Also called equijoin sincethe main criteria is theequality of join attributesFall 2001 Database Systems 20Relational Algebra - Join• Find all people with the same name and email address from the“buyers” and “owners” relations– join attributes are “Name” and “Email” from the buyer andowners relations– the joined relation will have attributes “Buyid, Name, Email, Oid”– there are 5 × 4 pairs of tuples to compare on the attributes“Name” and “Email”£¥¤§¦©¨¥ ¨¥£!$#% )(1032 ¨ 03( %54 %67 8@9BADC G r a y @ m ic ro s o ft,c o m E3F
  11. 11. Relational Algebra11Fall 2001 Database Systems 21Relational Algebra - Join ¢¡¤£¦¥¢§¢¨  ¢© Gray@microsoft,com !# $% Smith@aol.com#)(1032345 6¤798A@B Johns@geocities.com4DC)E#F¢GIHPDQ RASUTWVYX Brown@netmail.com `badcDegf3hiqp r¤sUtvu Gray@microsoft.comixw€ybA‚¢ƒ„¢…¤†¦‡¢ˆ¢‰ „¢ ‘’“” Gray@microsoft,com„¢…¤†¦‡¢ˆ¢‰ „¢ ‘’“” Gray@microsoft,com•¢–¤—¦˜¢™¢d •¢e fghi Gray@microsoft,comExample below shows four of the twenty pairs of tuplesconsidered for the joinFall 2001 Database Systems 22Relational Algebra - Join• Find the names of items with bids over $20• Find the names of the owners of items with bids over $20and are located in BostonTemp1 := Bids WHERE Amount 20Temp2 := Items jlk Temp1Result := Temp2[Name]Temp1 := Bids WHERE Amount 20Temp3 := Items WHERE Location = “Boston”Temp2 := Temp3 jlk Temp1Temp4 := Temp2 jlk OwnersResult := Temp4[Owners.Name]
  12. 12. Relational Algebra12Fall 2001 Database Systems 23Relational Algebra - Division• Let head(R)=A1…AnB1…Bk and head(S)= B1…Bk• R ÷ S is the “maximal” set of tuples from R[A1…An] such thatR contains all tuples in (R ÷ S) x S.• Division is oftenused for “for-all”queries, e.g. “Findtuples in R that arein a relation with‘all’ tuples in S.”• All the attributes in the dividing relation S must exist in thedivided relation R!for all tuples r in R[A1…An]if for all tuples s in S, r+s is in Rplace r in R ÷ Send-ifend-forFall 2001 Database Systems 24Relational Algebra - Division  ¡ ¢ £¤¦¥ §¨¥ ©¥¤ §¨¥ ©¥¤¦¥ § ©¥¤¦¥ § ©¤ §¨¥ ©¤¦¥ § ©¤¦¥ § ©¤¦¥ §¨¥ ©!$#$% ()10 2304 5 6798 @387BA @38Find:• R ÷ S• R ÷ V• R ÷ YC DEGFIH¦PQSR TUR VXW÷ S
  13. 13. Relational Algebra13Fall 2001 Database Systems 25Relational Algebra - Division  ¡ ¢ £¤¦¥ §¨¥ ©¥¤ §¨¥ ©¥¤¦¥ § ©¥¤¦¥ § ©¤ §¨¥ ©¤¦¥ § ©¤¦¥ § ©¤¦¥ §¨¥ ©!$#$% ()10 2304 5 6798 @387BA @38Find:• R ÷ S• R ÷ V• R ÷ YFall 2001 Database Systems 26Relational Algebra - Division• Find identifiers of buyers who placed a bid on “all” the items• Find the identifiers of items that have a bid from “all” the buyersTemp := Items[Iid]Temp1:= Bids[Buyid,Iid]Result := Temp1 ÷ TempA := Buyers[BuyId]B := Bids[Iid, BuyId]Result := B ÷ A
  14. 14. Relational Algebra14Fall 2001 Database Systems 27Relational Algebra Operations– Union R ∪ S– Intersection R ∩ S– Difference R - S– Assignment S(B1…Bn) := R(A1…An)– Selection R where C– Projection R[A1…An]– Cartesian Product R x S– Join R ¢¡ S– Division R ÷ SFall 2001 Database Systems 28£¥¤§¦¥¨© £¥ ¦ !!#$¥% ()021 Smith@aol.com$¥3 465718@9 Johns@geocities.com$A BC@5EDF8 Brown@netmail.com$ G H(CPIRQ Gray@microsoft.comS!TFU(VWX SY2`bac d¥ef g Vfe!ahibp qsrut2v Gray@microsoft,comiw xy7€7!r@‚Pƒ Robert432@aol.comi„ …6y7†‡Pƒ Johns@somewhere.comi7ˆ …6tRƒRy!‡ Jj@yahoo.comi‰ (‘E’@ Duke@jazz.com “R”F•–— “˜™ de˜™ fgEh(i j2kRl2g2m˜konp7q req sbtuuvwRx yEz@x@{Pz7|p7} re} ~6€R€o|uy‚„ƒ …zo†PxP{Pz7|pE‡ re} ˆ!wvwox2t@zR€@w ‰FwoŠ‹zR‚@Œ2Ž  ‘7’o“s”@•2–P– —!•˜˜•@–E™ FŽ š› 2’Rœ2ž’oŸR 7¡›R¢£u•2–P–@’¤!¥ •@¦P¦o˜¥o§ e¨ ›R¢” ¥ © ’@–@¦P’7ª«!¬­(® «b¯° ­(±E²@³ ¬!¯° «´2µ¬E° ¶s· ¸7´E¹2²º»¼b½ ¾!¿7À7Á ½ ºÃ ½uĺ»¼bà ¾!¿7À7Á ÂRÅ ºÄ Æĺ»¼À ¾!¿7À½ ½ º7Å ½„¾º»¼Å Ç!¿½ ÂEÆ ºÄ Èbĺ»¼Ä Ç!¿½ ½ ºÃ ÃEÁº»¼Æ Ç!¿Ã ÂRÅ º7Å È!Áº»¼!È Ç!¿Ã Âà º½ ź»¼¾ Ç!¿7À Âà ºÀ ÄAuction Database
  15. 15. Relational Algebra15Fall 2001 Database Systems 29Problem 1Find the name and email of owners of items located in“Boston”A := Items WHERE Location = “Boston”B := A ¢¡ OwnersResult := B[Owners.Name, Owners.Email]Fall 2001 Database Systems 30Problem 2Find the identifiers and amount of bids placed bybuyer “Roberts”A := Buyers WHERE Name = “Roberts”B := A ¢¡ BidsResult := B[Bid, Amount]
  16. 16. Relational Algebra16Fall 2001 Database Systems 31Problem 3Find the names of buyers who placed a bid on an itemowned by “Brown”A := Owners WHERE Name = “Brown”B := (A ¢¡ Items) [Iid]C := B ¢¡ BidsResult := (C ¢¡ Buyers) [Buyers.Name]Fall 2001 Database Systems 32Problem 4Find the identifier of items with more than one bidA := Bids[Bid, Iid]B := A x Bids[Bid,Iid]C := B WHERE (A.Bid Bids.Bid)AND (A.Iid = Bids.Iid)Result := C[Iid]
  17. 17. Relational Algebra17Fall 2001 Database Systems 33Problem 5Find the names of items all buyers placed a bid onA := (Bids[Iid, BuyId]) ÷ (Buyers[Buyid])B := A ¢¡ ItemsResult := B[Name]