Presentation is loading. Please wait.

Presentation is loading. Please wait.

 CS 405G: Introduction to Database Systems Lecture 7: Relational Algebra II Instructor: Chen Qian Spring 2014.

Similar presentations


Presentation on theme: " CS 405G: Introduction to Database Systems Lecture 7: Relational Algebra II Instructor: Chen Qian Spring 2014."— Presentation transcript:

1  CS 405G: Introduction to Database Systems Lecture 7: Relational Algebra II Instructor: Chen Qian Spring 2014

2 (1) 7 points. 0.5 for each mistake 6/9/20152

3 (2) 3 points. No. If starting and ending dates are recorded in multi- valued attributes, we cannot determine which starting date is corresponding to which ending data. 6/9/20153

4 Review: Summary of core operators Selection: Projection: Cross product: Union: Difference: Renaming: 6/9/2015Chen Qian @ University of Kentucky σp Rσp R πL RπL R R X SR X S R SR S R - S ρ S(A 1, A 2, …) R 4

5 Review Summary of derived operators Join: Natural join: Intersection: 6/9/2015Chen Qian @ University of Kentucky R p S R S Many more Outer join, Division, Semijoin, anti-semijoin, … 5

6 6/9/2015Chen Qian @ University of Kentucky Using Join What are the ids of Lisa’s classes? Student(sid: string, name: string, gpa: float) Course(cid: string, department: string) Enrolled(sid: string, cid: string, grade: character) An Answer: Student_Lisa  σ name = “Lisa” Student Lisa_Enrolled  Student_Lisa Enrolled Lisa’s classes  π CID Lisa_Enrolled Or: Student_Enrolled  Student Enrolled Lisa_Enrolled  σ name = “Lisa” Student_Enrolled Lisa’s classes  π CID Lisa_Enrolled 6

7 6/9/2015Chen Qian @ University of Kentucky Join Example sidnameagegpa 1234John213.5 1123Mary223.8 1012Lisa222.6 sidnameagegpa 1012Lisa222.6 sidcidgrade 1123108A 1012647A 1012108B sidnameagegpacidgrade 1012Lisa222.6647A 1012Lisa222.6108B σ name=“Lisa” π cid cid 647 108 7

8 6/9/2015Chen Qian @ University of Kentucky IDs of Lisa’s Classes π CID ( ( σ name = “Lisa” Student) Enrolled) Enroll π CID IDs of Lisa’s classes Student σ name = “Lisa” Who’s Lisa? 8

9 6/9/2015Chen Qian @ University of Kentucky Students in Lisa’s Classes SID of Students in Lisa’s classes Student_Lisa  σ name = “Lisa” Student Lisa_Enrolled  Student_Lisa Enrolled Lisa’s classes  π CID Lisa_Enrolled Enrollment in Lisa’s classes  Lisa’s classes Enrolled Students in Lisa’s class  π SID Enrollment in Lisa’s classes Students in Lisa’s classes Enroll π SID Enroll π CID Lisa’s classes Student σ name = “Lisa” Who’s Lisa? 9

10 6/9/2015Chen Qian @ University of Kentucky Tips in Relational Algebra Use temporary variables Use foreign keys to join tables 10

11 6/9/2015Chen Qian @ University of Kentucky An exercise Names of students in Lisa’s classes Students in Lisa’s classes Student π name Their names Enroll π SID Enroll π CID Lisa’s classes Student σ name = “Lisa” Who’s Lisa? 11

12 6/9/2015Chen Qian @ University of Kentucky Set Minus (difference) Operation CID’s of the courses that Lisa is NOT taking CID’s of the courses that Lisa IS taking All CID’s - π CID Course Enroll Student σ name = “Lisa” π CID 12

13 6/9/2015Chen Qian @ University of Kentucky Renaming Operation  Enrolled1(SID1, CID1,Grade1) Enrolled sidcidgrade 1234647A 1123108A sid1cid1grade1 1234647A 1123108A  Enroll1(SID1, CID1,Grade1) 13

14 6/9/2015Chen Qian @ University of Kentucky Example We have the following relational schemas Student(sid: string, name: string, gpa: float) Course(cid: string, department: string) Enrolled(sid: string, cid: string, grade: character) SID’s of students who take at least two courses Enrolled π SID (Enrolled Enrolled.SID = Enrolled.SID & Enrolled.CID  Enrolled.CID Enrolled) 14

15 6/9/2015Chen Qian @ University of Kentucky Example (cont.)  Enroll1(SID1, CID1,Grade1) Enrolled  Enroll2(SID2, CID2,Grade2) Enrolled π SID (Enroll1 SID1 = SID2 & CID1  CID2 Enroll2) ρ Enroll1(SID1, CID1,Grade1) ρ Enroll2(SID2, CID2, Grade2) Enroll SID1 = SID2 & CID1  CID2 π SID1 Expression tree syntax: 15

16 How does it work? 6/9/2015Chen Qian @ University of Kentucky sidcidgrade 1123108A 1012647A 1012108B sid2cid2grade2 1123108A 1012647A 1012108B Enroll1 SID1 = SID2 Enroll2 sidcidgradesid2cid2grade2 1123108A1123108A 1012647A1012647A 1012647A1012108B 1012108B1012647A 1012108B1012108B 16

17 6/9/2015Chen Qian @ University of Kentucky sidcidgrade 1123108A 1012647A 1012108B sid2cid2grade2 1123108A 1012647A 1012108B Enroll1 SID1 = SID2 & CID1  CID2 Enroll2 sidcidgradesid2cid2grade2 1123108A1123108A 1012647A1012647A 1012647A1012108B 1012108B1012647A 1012108B1012108B 17

18 6/9/2015Chen Qian @ University of Kentucky Tips in Relational Algebra A comparison is to identify a relationship 18

19 6/9/2015Chen Qian @ University of Kentucky A trickier exercise Who has the highest GPA? Who has a GPA? Who does NOT have the highest GPA? Whose GPA is lower than somebody else’s? π SID Student - ρ Student1 ρ Student2 Student1.GPA < Student2.GPA π Student1.SID A deeper question: When (and why) is “-” needed? 19

20 6/9/2015Chen Qian @ University of Kentucky Review: Summary of core operators Selection: Projection: Cross product: Union: Difference: Renaming: σp Rσp R πL RπL R R X SR X S R SR S R - S ρ S(A 1, A 2, …) R 20

21 6/9/2015Chen Qian @ University of Kentucky Review: Summary of derived operators Join: Natural join: Intersection: R p S R S 21

22 6/9/2015Chen Qian @ University of Kentucky Review Relational algebra Use temporary variable Use foreign key to join relations A comparison is to identify a relationship 22

23 Exercises of R. A. Reserves Sailors Boats Chen Qian @ University of Kentucky6/9/201523

24 Problem 1 Find names of sailors who’ve reserved boat #103 Solution: Sailors π sname Who reserved boat #103? Reserves σ bid = “103” Boat #103 Chen Qian @ University of Kentucky6/9/201524

25 Information about boat color only available in Boats; so need an extra join: Problem 2: Find names of sailors who’ve reserved a red boat Names of sailors who reserved red boat Sailors π sname Reserve π SID Who reserved red boats? Boat σ color = “red” Red boats Chen Qian @ University of Kentucky6/9/201525

26 Problem 3: Find names of sailors who’ve reserved a red boat or a green boat Can identify all red or green boats, then find sailors who’ve reserved one of these boats: Names of sailors who reserved red boat Sailors π sname Reserve π SID Who reserved red boats? Boat σ color = “red”  color = “green” Red boats Chen Qian @ University of Kentucky6/9/201526

27 Problem 4: Find names of sailors who’ve reserved only one boat Chen Qian @ University of Kentucky6/9/201527

28 6/9/2015Chen Qian @ University of Kentucky Monotone operators If some old output rows may need to be removed Then the operator is non-monotone Otherwise the operator is monotone That is, old output rows always remain “correct” when more rows are added to the input Formally, for a monotone operator op: R µ R’ implies op( R ) µ op( R’ ) RelOp Add more rows to the input... What happens to the output? 28

29 6/9/2015Chen Qian @ University of Kentucky Why is “-” needed for highest GPA? Composition of monotone operators produces a monotone query Old output rows remain “correct” when more rows are added to the input Highest-GPA query is non-monotone Current highest GPA is 4.1 Add another GPA 4.2 Old answer is invalidated  So it must use difference! 29

30 6/9/2015Chen Qian @ University of Kentucky Classification of relational operators Selection: σ p R Projection: π L R Cross product: R X S Join: R p S Natural join: R S Union: R U S Difference: R - S Intersection: R ∩ S Monotone Monotone w.r.t. R; non-monotone w.r.t S Monotone 30

31 6/9/2015Chen Qian @ University of Kentucky Why do we need core operator X? Cross product The only operator that adds columns Difference The only non-monotone operator Union The only operator that allows you to add rows? Selection? Projection? 31

32 Aggregate Functions and Operations Aggregation function takes a collection of values and returns a single value as a result. avg: average value min: minimum value max: maximum value sum: sum of values count: number of values Aggregate operation in relational algebra G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E) E is any relational-algebra expression G 1, G 2 …, G n is a list of attributes on which to group (can be empty) Each F i is an aggregate function Each A i is an attribute name 6/9/2015Chen Qian @ University of Kentucky32

33 Aggregate Operation – Example Relation r: AB   C 7 3 10 g sum(c) (r) sum-C 27 6/9/2015Chen Qian @ University of Kentucky33

34 Aggregate Operation – Example Relation account grouped by branch-name: branch-name g sum(balance) (account) branch-nameaccount-numberbalance Perryridge Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 700 branch-namebalance Perryridge Brighton Redwood 1300 1500 700 6/9/2015Chen Qian @ University of Kentucky34

35 Null Values It is possible for tuples to have a null value, denoted by null, for some of their attributes null signifies an unknown value or that a value does not exist. The result of any arithmetic expression involving null is null. Aggregate functions simply ignore null values For duplicate elimination and grouping, null is treated like any other value, and two nulls are assumed to be the same 6/9/2015Chen Qian @ University of Kentucky35

36 Null Values Comparisons with null values return the special truth value unknown If false was used instead of unknown, then not (A = 5 Three-valued logic using the truth value unknown: OR: (unknown or true) = true, (unknown or false) = unknown (unknown or unknown) = unknown AND: (true and unknown) = unknown, (false and unknown) = false, (unknown and unknown) = unknown NOT: (not unknown) = unknown Result of select predicate is treated as false if it evaluates to unknown 6/9/2015Chen Qian @ University of Kentucky36

37 6/9/2015Chen Qian @ University of Kentucky Review Expression tree Tips in writing R.A. Use temporary variables Use foreign keys to join tables A comparison is to identify a relationship Use difference in non-monotonic results 37

38 How to write answers to a R.A. problem Go ahead to write down a single expression as long as you think it is correct. However, the followings are recommended: Draw an expression tree Write down any intermediate expressions temporary variables renaming operations Because then you can get partial credits! 6/9/2015Chen Qian @ University of Kentucky38


Download ppt " CS 405G: Introduction to Database Systems Lecture 7: Relational Algebra II Instructor: Chen Qian Spring 2014."

Similar presentations


Ads by Google