introduction to database systems
play

Introduction to Database Systems Formal Relational Languages - PowerPoint PPT Presentation

CPSC 304 Introduction to Database Systems Formal Relational Languages Textbook Reference Database Management Systems: 4 - 4.2 (skip the calculii) Learning Goals Identify the basic operators in Relational Algebra (RA). Use RA to create


  1. CPSC 304 Introduction to Database Systems Formal Relational Languages Textbook Reference Database Management Systems: 4 - 4.2 (skip the calculii)

  2. Learning Goals Identify the basic operators in Relational Algebra (RA). Use RA to create queries that include combining RA operators. Given an RA query and table schemas and instances, compute the result of the query. 2

  3. Databases: the continuing saga When last we left databases… We learned that they’re excellent things We learned how to conceptually model them using ER diagrams We learned how to logically model them using relational schemas We knew how to normalize our database relations We’re almost ready to use SQL to query it, but first… 3

  4. Balance, Daniel-san, is key The mathematical foundations: Relational Algebra Clear way of describing core concepts partially procedural : describe what you want and how you want it, but the order of operations matters Datalog A logic-based language (basically a subset of Prolog) Coming up after this 4

  5. Relational Query Languages Allow data manipulation and retrieval from a DB Relational model supports simple, powerful QLs: Strong formal foundation based on logic Allows for much optimization via query optimizer Query Languages != Programming Languages QLs not intended for complex calculations QLs provide easy access to large datasets Users do not need to know how to navigate through complicated data structures 5

  6. Relational Algebra (RA) All in one place Basic operations: Selection ( σ ) : Selects a subset of rows from relation. Projection ( π ) : Deletes unwanted columns from relation. Cross-product (x): Allows us to combine two relations. Set-difference (-): Tuples in relation 1, but not in relation 2. Union (  ): Tuples in relation 1 and in relation 2. Rename ( ρ ): Assigns a (another) name to a relation Additional, inessential but useful operations: Intersection (  ) , join ( ⋈) , division (/) , assignment(  ) All operators take one or two relations as inputs and give a new relation as a result For the purposes of relational algebra, relations are sets Operations can be composed . (Algebra is “closed”) 6

  7. Example Movies Database Movie(MovieID, Title, Year) StarsIn(MovieID, StarID, Character) MovieStar(StarID, Name, Gender) 7

  8. Example Instances Movie: MovieID Title Year 1 Star Wars 1977 2 Gone with the Wind 1939 3 The Wizard of Oz 1939 4 Indiana Jones and the 1981 Raiders of the Lost Ark MovieID StarID Character StarsIn: 1 1 Han Solo 4 1 Indiana Jones Scarlett O’Hara 2 2 3 3 Dorothy Gale StarID Name Gender MovieStar: 1 Harrison Ford Male 2 Vivian Leigh Female 8 3 Judy Garland Female

  9. Selection ( σ (sigma)) Notation:  p ( r ) Set of p is called the selection predicate tuples of r  Defined as: satisfying p  p ( r ) = { t | t  r and p(t) } Where p is a formula in propositional calculus consisting of: connectives :  ( and ),  ( or ),  ( not ) and predicates: <attribute> op <attribute> or <attribute> op <constant> where op is one of: =,  , >,  , <,  9

  10. Selection Example Movie: MovieID Title Year 1 Star Wars 1977 2 Gone with the Wind 1939 3 The Wizard of Oz 1939 4 Indiana Jones and the 1981 Raiders of the Lost Ark σ year > 1940 (Movie) MovieID Title Year 1 Star Wars 1977 4 Indiana Jones and the 1981 Raiders of the Lost Ark 10

  11. Selection Example #2 Find all male stars StarID Name Gender 1 Harrison Ford Male 11

  12. Selection Example #2 Find all male stars StarID Name Gender 1 Harrison Ford Male  Gender = ‘male’ MovieStar 12

  13. Projection ( π (pi)) Notation: π A1, A2, …, Ak ( r ) where A1, …,Ak are attributes (the projection list) and r is a relation. The result: a relation of the k attributes A1, A2, …, AK obtained from r by erasing the columns that are not listed Duplicate rows removed from result (relations are sets) 13

  14. Projection Examples Movie:  Title, Year (Movie) MovieID Title Year Title Year 1 Star Wars 1977 Star Wars 1977 2 Gone with the Wind 1939 Gone with the Wind 1939 3 The Wizard of Oz 1939 The Wizard of Oz 1939 4 Indiana Jones and the 1981 Indiana Jones and the 1981 Raiders of the Lost Ark Raiders of the Lost Ark  Year (Movie) What is  Title,Year ( σ year > 1940 (Movie))? Year Title Year 1977 Star Wars 1977 1939 Indiana Jones and the 1981 1981 Raiders of the Lost Ark 14

  15. CPSC 304 – February 13, 2018 Administrative Notes Reminder: 2 nd project milestone due Friday Reminder: the midterm 1 regrade deadline is past Reminder: tutorial due Friday (as always) Reminder: next week is Reading Week This week’s tutorial will be due at the normal time 15

  16. Now where were we… We’d moved onto relational algebra In particular, we’d covered two operators: selection ( σ ) and projection (  ) Selecting allows you to say that you want specific rows . Projection allows you to say that you want specific columns . 16

  17. Projection Example #2 Find the IDs of actors who have starred in movies StarID 1 2 3 17

  18. Projection Example #2 Find the IDs of actors who have starred in movies π StarID (StarsIn) StarID 1 2 3 18

  19. Clicker Projection Example Suppose relation R(A,B,C) has the tuples: A B C 1 2 3 4 2 3 4 5 6 2 5 3 Compute the projection π C,B (R), and 1 2 6 identify one of its tuples from the list below. A. (2,3) B. (4,2,3) C. (6,4) D. (6,5) 19 E. None of the above

  20. Clicker Projection Example Suppose relation R(A,B,C) has the tuples: A B C 1 2 3 4 2 3 4 5 6 2 5 3 Compute the projection π C,B (R), and 1 2 6 identify one of its tuples from the list below. Wrong order A. (2,3) C B Not projected 3 2 B. (4,2,3) 6 5 Wrong attributes C. (6,4) 3 5 right D. (6,5) 6 2 20 E. None of the above

  21. Selection and Projection Example Find the ids of movies made prior to 1950 MovieID Title Year Movie: 1 Star Wars 1977 2 Gone with the Wind 1939 3 The Wizard of Oz 1939 4 Indiana Jones and the 1981 Raiders of the Lost Ark MovieID 2 3 21

  22. Selection and Projection Example Find the ids of movies made prior to 1950 MovieID Title Year Movie: 1 Star Wars 1977 2 Gone with the Wind 1939 3 The Wizard of Oz 1939 4 Indiana Jones and the 1981 Raiders of the Lost Ark π MovieID ( σ year < 1950 Movie) MovieID 2 3 22

  23. Union, Intersection, Set-Difference Notation: r  s r  s r – s Defined as: r  s = { t | t  r or t  s } r  s ={ t | t  r and t  s } r – s = { t | t  r and t  s } For these operations to be well-defined: 1. r, s must have the same arity (same number of attributes) 2. The attribute domains must be compatible (e.g., 2nd column of r has same domain of values as the 2nd column of s ) What is the schema of the result? 23

  24. Union, Intersection, and Set Difference Examples MovieStar Singer StarID Name Gender StarID SName Gender 1 Harrison Ford Male 3 Judy Garland Female 2 Vivian Leigh Female 4 Christine Lavin Female 3 Judy Garland Female MovieStar ∪ Singer MovieStar ∩ Singer StarID Name Gender StarID Name Gender 1 Harrison Ford Male 3 Judy Garland Female 2 Vivian Leigh Female MovieStar – Singer 3 Judy Garland Female StarID Name Gender 4 Christine Lavin Female 1 Harrison Ford Male 2 Vivian Leigh Female 24

  25. Set Operator Example MovieStar Singer StarID Name Gender StarID Name Gender 1 Harrison Ford Male 3 Judy Garland Female 2 Vivian Leigh Female 4 Christine Lavin Female 3 Judy Garland Female Find the names of stars that are Singers but not MovieStars Name Christine Lavin 25

  26. Set Operator Example MovieStar Singer StarID Name Gender StarID Name Gender 1 Harrison Ford Male 3 Judy Garland Female 2 Vivian Leigh Female 4 Christine Lavin Female 3 Judy Garland Female Find the names of stars that are Singers but not MovieStars π Name ( Singer – MovieStar) Name Christine Lavin 26

  27. Cartesian (or Cross)-Product Notation: r x s Defined as: r x s = { t q | t  r and q  s } It is possible for r and s to have attributes with the same name, which creates a naming conflict. In this case, the attributes are referred to solely by position. 27

  28. Cartesian Product Example MovieStar StarsIn StarID Name Gender MovieID StarID Character 1 Harrison Ford Male 1 1 Han Solo 2 Vivian Leigh Female 4 1 Indiana Jones Scarlett O’Hara 3 Judy Garland Female 2 2 3 3 Dorothy Gale MovieStar x StarsIn 1 Name Gender MovieID 5 Character 1 Harrison Ford Male 1 1 Han Solo 2 Vivian Leigh Female 1 1 Han Solo 3 Judy Garland Female 1 1 Han Solo 1 Harrison Ford Male 4 1 Indiana Jones 2 Vivian Leigh Female 4 1 Indiana Jones 3 Judy Garland Female 4 1 Indiana Jones 28 … … … … … …

  29. Rename ( ρ (rho)) Allows us to name results of relational-algebra expressions. Notation  (X , E ) returns the expression E under the name X We can rename part of an expression, e.g.,  ((StarID →ID ) , π StarID,Name (MovieStar) ) We can also refer to positions of attributes, e.g.,  ((1 →ID ) ) , π StarID,Name (MovieStar ) Is the same as above 29

Recommend


More recommend