CS 3630 Database Design and Implementation
Retrieving Data From Database Relational Algebra Procedural How to get the data Relational Calculus Non-Procedural What data to retrieve We use Relational Algebra Could have more materials
Relational Algebra Operations Selection Projection Union Set difference Intersection Cartesian product Join
Relational Algebra Operations Applied to relation instances (tables) The result is still a relation (table) Syntax
Selection (predicate) (R) R is a relation The result is a subset of relation R
Selection (II) Relation Schema: R (Att1, Att2, Att3) Relation Instance 100 10 x 90 20 y 100 30 x (att1 < 100) (R) 90 20 y (att1 < 100) (R) = {(90, 20, y)} The result is still a relation (table).
Selection (III) ((att1 >= 100 and att2 <= 20) or att3 x) (R) Table Schema R (Att1, Att2, Att3) Table Instance Att1 Att2 Att3 100 10 x 90 20 y 100 30 x ((att1 >= 100 and att2 <= 20) or att3 x) (R) = {(100, 10, x), (90, 20, y)} As a table Att1 Att2 Att3 100 10 x 90 20 y
Notations ((att1 >= 100 and att2 <= 20) or att3 x) (R) Correct! ((att1 >= 100 att2 <= 20) att3 x) (R) Incorrect! ((att1 >= 100 && att2 <= 20) || att3 x) (R)
Projection Att1, Att3 (R) R is a relation The result is a relation with specified attributes all or some attributes same or different order The schema of the result is usually different No equivalent set operation No duplicates
Projection (II) (Att1, Att3) (R) Att1 Att2 Att3 100 10 x 90 20 y
Projection (III) (Att3, Att1) (R) (Att1, Att3) (R) Order of Attributes (Att3, Att1) (R) (Att1, Att3) (R)
Projection (IV) All attributes: all records * (R) (Att1, Att2, Att3) (R) (Att1, Att3, Att2) (R)
Union R (Att1, Att2, Att3) S (Att1, Att2, Att4) Att1 Att2 Att3 Att1 Att2 Att4 100 10 x 101 40 x 90 20 y 90 20 y 100 30 x R S Union compatible (Same schema) T (Att1, Att2, Att4, Att5) R T ?
Union (II) R (Att1, Att2, Att3) S (Att1, Att2, Att4) Att1 Att2 Att3 Att1 Att2 Att4 100 10 x 101 40 x 90 20 y 90 20 y 100 30 x R S Att1 Att2 Att4(Att3) 100 10 x 90 20 y 100 30 x 101 40 x
Set Difference R - S Union compatible (same schema) Att1 Att2 Att3 Att1 Att2 Att4 100 10 x 101 40 x 90 20 y 90 20 y 100 30 x Att1 Att2 Att3 100 10 x 100 30 x
Intersection R S Union compatible (same schema) Att1 Att2 Att3 Att1 Att2 Att4 100 10 x 101 40 x 90 20 y 90 20 y 100 30 x Att1 Att2 Att4(Att3) 90 20 y
Cartesian Product R S Cardinality: |R S| = |R| * |S| Degree : (Degree of R) + (Degree of S) R (Att1, Att2, Att3) Att1 Att2 Att3 100 10 x 90 20 y 101 15 z S (Att2, Att4) Att2 Att4 20 a 15 b R S Att1 R.Att2 Att3 S.Att2 Att4 100 10 x 20 a 100 10 x 15 b 90 20 y 20 a 90 20 y 15 b 101 15 z 20 a 101 15 z 15 b
Theta Join Based on Cartesian Product R p S = (p) (R S) R (R.Att2 >= S.Att2) S = (R.Att2 >= S.Att2) (R S) R: Att1 Att2 Att3 100 10 x 90 20 y 101 15 z S: Att2 Att4 20 a 15 b Att1 R.Att2 Att3 S.Att2 Att4 100 10 x 20 a 100 10 x 15 b 90 20 y 20 a 90 20 y 15 b 101 15 z 20 a 101 15 z 15 b Att1 R.Att2 Att3 S.Att2 Att4 90 20 y 20 a 90 20 y 15 b 101 15 z 15 b
Natural Join R: Att1 Att2 Att3 S: Att2 Att4 100 10 x 20 a 90 20 y 15 b 101 15 z Common attribute Att2 R S = (Att1, R.Att2, Att3, Att4) (R.Att2 = S.Att2) (R S) Att1 R.Att2 Att3 S.Att2 Att4 100 10 x 20 a 100 10 x 15 b 90 20 y 20 a 90 20 y 15 b 101 15 z 20 a 101 15 z 15 b Att1 R.Att2 Att3 S.Att2 Att4 90 20 y 20 a 101 15 z 15 b Att1 R.Att2 Att3 Att4 90 20 y a 101 15 z b
Natural Join R S = (Att1, R.Att2, Att3, Att4) (R.Att2 = S.Att2) (R S) Att1 Att2 Att3 Att2 Att4 100 10 x 20 a 90 20 y 15 b 101 15 z Att1 R.Att2 Att3 S.Att2 Att4 Att1 R.Att2 Att3 S.Att2 Att4 100 10 x 20 a 101 15 z 15 b 100 10 x 15 b 90 20 y 20 a 90 20 y 20 a 90 20 y 15 b Att1 R.Att2 Att3 Att4 101 15 z 20 a 101 15 z b 101 15 z 15 b 90 20 y a Remove the common attribute of the 2nd table Keep all attributes in the original order
Outer Join R S Left Outer-Join Keep non-matching tuples of R (Natural unless otherwise stated) Att1 Att2 Att3 Att2 Att4 100 10 x 20 a 90 20 y 15 b 101 15 z Att1 R.Att2 Att3 Att4 100 10 x null 90 20 y a 101 15 z b
Left Outer Join (II) R S How to implement it? For each r of R Joined = False For each s of S If (Same value on common attribute) Generate a tuple Joined = True If Not Joined Generate a tuple from r with null values
Other Joins Right Outer Join Full Outer Join Semi-Join
Exercise: Natural Join R S Att1 Att2 Att3 Att2 Att4 100 10 x 20 a 90 20 y 15 b 101 15 z 20 b 99 30 w 15 c 30 d Att1 R.Att2 Att3 Att4 (first record of R does not join with any record of S) 90 20 y a 90 20 y b 101 15 z b 101 15 z c 99 30 w d
Exercise: Left Outer Join R S Att1 Att2 Att3 Att2 Att4 100 10 x 20 a 90 20 y 15 b 101 15 z 20 b 99 30 w 15 c 30 d Att1 R.Att2 Att3 Att4 100 10 x null (does not join) 90 20 y a 90 20 y b 101 15 z b 101 15 z c 99 30 w d
Exercise: Right Outer Join R S Att1 Att2 Att3 Att2 Att4 100 10 x 20 a 90 20 y 15 b 101 15 z 20 b 99 30 w 15 c 80 20 y 30 d Att1 R.Att2 Att3 Att4 90 20 y a 80 20 y a 101 15 z b 90 20 y b 80 20 y b 101 15 z c 99 30 w d
Aggregation COUNT SUM AVG MIN MAX
Grouping
Relational Calculus
Assignment 1 Due Wednesday (week 2), Jan 31, at 5 pm D2L It will be late after 5:00 pm
Due Wednesday (week 3), Feb 7 Assignment 2 Due Wednesday (week 3), Feb 7