03. Relational Algebra

03. Relational Algebra

License c 2002-2010 T. Uyar, ¸S. O˘g¨ ud¨ uc¨ u¨ Database Systems Relational Algebra You are free: I to Share — to copy, distribute and transmit the work I to Remix — to adapt the work Under the following conditions: ¨ I Attribution — You must attribute the work in the manner specified by the author or licensor (but not in H. Turgut Uyar ¸Sule O˘gud¨ uc¨ u¨ any way that suggests that they endorse you or your use of the work). I Noncommercial — You may not use this work for commercial purposes. I Share Alike — If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one. 2002-2010 Legal code (the full license): http://creativecommons.org/licenses/by-nc-sa/3.0/ 1 / 115 2 / 115 Topics Closure Relational Algebra Introduction Selection Definition Joining closure: Set Operations the input and output of all operations are relations I the output of one operation can be the input of another Queries operation Introduction I operations can be nested Joining Subqueries Set Operations 3 / 115 4 / 115 Leap Data Definition Creating Relations I operator first, operands later: r e l a t i o n ( r e l a t i o n n a m e ) ( operator (operand1) (operand2) ... ( a t t r i b u t e name ,type ,length) I data types: [,...] I string, integer, boolean ) I no real numbers I dotted notation to distinguish attributes with identical names Deleting Relations d e l r e l ( r e l a t i o n n a m e ) 5 / 115 6 / 115 Relation Creation Example Data Manipulation Adding Tuples Example (creating the movie relation) add ( r e l a t i o n name) (value [, ...]) r e l a t i o n ( movie ) ( ( id , integer , 4 ) , I values must be given in the order as they were defined ( t i t l e , str ing , 8 0 ) , I no quotes around values ( yr , integer , 4 ) , ( score , integer , 3 ) , ( votes , integer , 5 ) , Deleting Tuples (directorid , integer , 4 ) delete ( r e l a t i o n name) (condition) ) I all values in quotes when writing conditions 7 / 115 8 / 115 Data Manipulation Examples Example Relations Example (MOVIE Relation) Example (adding a movie) Example (deleting movies) ID TITLE YR SCORE VOTES DIRECTORID 6 Usual Suspects 1995 87 35027 639 70 Being John Malkovich 1999 83 13809 1485 add ( movie ) ( delete ( movie ) 107 Batman & Robin 1997 35 10577 105 110 Sleepy Hollow 1999 75 10514 148 6 , ( score < ’30 ’) 112 Three Kings 1999 77 10319 0 151 Gattaca 1997 74 8388 2020 Usual Suspects , 213 Blade 1998 67 6885 0 228 Ed Wood 1994 78 6587 148 1995 , 251 End of Days 1999 55 6095 103 87 , 281 Dangerous Liaisons 1988 77 5651 292 373 Fear and Loathing in Las Vegas 1998 65 4658 59 35027 , 432 Stigmata 1999 61 4141 0 433 eXistenZ 1999 69 4130 97 639 573 Dead Man 1995 74 3333 175 1468 Europa 1991 76 1042 615 ) 1512 Suspiria 1977 71 1004 2259 1539 Cry-Baby 1990 59 972 364 9 / 115 10 / 115 Example Relations Example Relations Example (PERSON Relation) Example (CASTING Relation) ID NAME 9 Arnold Schwarzenegger 308 Gabriel Byrne MOVIEID ACTORID ORD 151 793 3 373 302 2 26 Johnny Depp 350 Jennifer Jason Leigh 6 308 2 213 745 6 432 308 2 59 Terry Gilliam 364 John Waters 6 302 3 213 3578 8 432 406 1 97 David Cronenberg 406 Patricia Arquette 70 282 2 228 26 1 433 350 1 103 Peter Hyams 503 John Malkovich 70 503 14 228 406 4 433 793 2 105 Joel Schumacher 615 Lars von Trier 107 9 1 251 9 1 573 26 1 138 George Clooney 639 Bryan Singer 107 138 2 251 308 2 573 308 12 148 Tim Burton 745 Udo Kier 107 243 4 251 745 10 573 1641 6 175 Jim Jarmusch 793 Jude Law 110 26 1 281 243 7 1468 745 3 187 Christina Ricci 1485 Spike Jonze 110 187 2 281 503 2 1512 745 9 243 Uma Thurman 1641 Iggy Pop 112 138 1 373 26 1 1539 26 1 282 Cameron Diaz 2020 Andrew Niccol 112 1485 4 373 187 6 1539 1641 5 292 Stephen Frears 2259 Dario Argento 151 243 2 373 282 8 1539 3578 7 302 Benicio Del Toro 3578 Traci Lords 11 / 115 12 / 115 Selection Selection Examples - 1 Definition Example (movies with more than 10000 votes) selection: creating a new relation from all the tuples that satisfy a condition s1 = s e l e c t (movie) (votes > ’10000 ’) Statement ID TITLE YR SCORE VOTES DIRECTORID s e l e c t ( r e l a t i o n name) (condition) 6 Usual Suspects 1995 87 35027 639 70 Being John Malkovich 1999 83 13809 1485 107 Batman & Robin 1997 35 10577 105 110 Sleepy Hollow 1999 75 10514 148 112 Three Kings 1999 77 10319 0 I also known as: restrict I output relation header = input relation header 13 / 115 14 / 115 Selection Examples - 2 Projection Example (movies older than 1992, with scores higher than 75) Definition projection: s2 = s e l e c t ( movie ) creating a new relation using the given attributes ( ( yr < ’1992 ’) and ( score > ’75 ’)) Statement ID TITLE YR SCORE VOTES DIRECTORID project ( r e l a t i o n name) (attribute [, ...]) 281 Dangerous Liaisons 1988 77 5651 292 1468 Europa 1991 76 1042 615 I output relation header = attribute list 15 / 115 16 / 115 Projection Examples - 1 Projection Examples - 2 Example (titles of all movies) Example (titles and years of all movies) p1 = project (movie) (title) p2 = project (movie) (title ,yr) TITLE TITLE YR Usual Suspects Usual Suspects 1995 Being John Malkovich Dangerous Liaisons Being John Malkovich 1999 Dangerous Liaisons 1988 Batman & Robin Fear and Loathing in Las Vegas Batman & Robin 1997 Fear and Loathing in Las Vegas 1998 Sleepy Hollow Stigmata Sleepy Hollow 1999 Stigmata 1999 Three Kings eXistenZ Three Kings 1999 eXistenZ 1999 Gattaca Dead Man Gattaca 1997 Dead Man 1995 Blade Europa Blade 1998 Europa 1991 Ed Wood Suspiria Ed Wood 1994 Suspiria 1977 End of Days Cry-Baby End of Days 1999 Cry-Baby 1990 17 / 115 18 / 115 Projection Examples - 3 Projection Examples - 4 Example (years of all movies) Example (titles of all movies with votes more than 5000 and p3 = project (movie) (yr) scores higher than 70) YR 1. all movies with votes more than 5000 and scores higher than 1995 1999 70 1997 1998 2. titles of all movies with votes more than 5000 and scores 1994 1988 higher than 70 1991 1977 1990 19 / 115 20 / 115 Projection Examples - 4 Projection Examples - 4 Example (all movies with votes more than 5000 and scores Example (titles of all movies with votes more than 5000 and higher than 70) scores higher than 70) p4a = s e l e c t ( movie ) p4 = project (p4a) (title) ( ( votes > ’5000 ’) and ( score > ’70 ’)) TITLE ID TITLE YR SCORE VOTES DIRECTORID Usual Suspects 6 Usual Suspects 1995 87 35027 639 Being John Malkovich 70 Being John Malkovich 1999 83 13809 1485 Sleepy Hollow 110 Sleepy Hollow 1999 75 10514 148 Three Kings 112 Three Kings 1999 77 10319 0 Gattaca 151 Gattaca 1997 74 8388 2020 Ed Wood 228 Ed Wood 1994 78 6587 148 Dangerous Liaisons 281 Dangerous Liaisons 1988 77 5651 292 21 / 115 22 / 115 Projection Examples - 4 Product Definition Example (titles of all movies with votes more than 5000 and product: relation 1 × relation 2 scores higher than 70) Carthesian product of two relations p4 = project ( s e l e c t ( movie ) Statement ( ( votes > ’5000 ’) and ( score > ’70 ’))) product ( r e l a t i o n 1) (relation 2 ) ( t i t l e ) I output relation header = relation1 header + relation2 header 23 / 115 24 / 115 Product Example Joining Example (s2 × p4) Definition x1 = product ( s2 ) ( p4 ) joining: creating a new relation by matching tuples of two relations over ID TITLE YR SCORE VOTES DIRECTORID p4.TITLE common values of one or more attributes 281 Dangerous Liaisons 1988 77 5651 292 Usual Suspects 281 Dangerous Liaisons 1988 77 5651 292 Being John Malkovich 281 Dangerous Liaisons 1988 77 5651 292 Sleepy Hollow Statement 281 Dangerous Liaisons 1988 77 5651 292 Three Kings 281 Dangerous Liaisons 1988 77 5651 292 Gattaca 281 Dangerous Liaisons 1988 77 5651 292 Ed Wood j o i n ( r e l a t i o n 1) (relation 2) (condition) 281 Dangerous Liaisons 1988 77 5651 292 Dangerous Liaisons 1468 Europa 1991 76 1042 615 Usual Suspects 1468 Europa 1991 76 1042 615 Being John Malkovich 1468 Europa 1991 76 1042 615 Sleepy Hollow I select (product ( relation 1 ) ( relation 2 )) ( condition ) 1468 Europa 1991 76 1042 615 Three Kings 1468 Europa 1991 76 1042 615 Gattaca 1468 Europa 1991 76 1042 615 Ed Wood I output relation header = relation1 header + relation2 header 1468 Europa 1991 76 1042 615 Dangerous Liaisons 25 / 115 26 / 115 Join Examples - 1 Join Examples - 1 Example (all movies and their directors) j 1 a = j o i n (movie) (person) (movie. directorid=person.id) Example (titles of all movies and the names of their directors) 1. all movies and their directors ID TITLE ... DIRECTORID PERSON.ID NAME 6 Usual Suspects ... 639 639 Bryan Singer 70 Being John Malkovich ... 1485 1485 Spike Jonze 2. titles of all movies and the names of their directors 107 Batman & Robin ..

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    20 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us