Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 3630 Database Design and Implementation

Similar presentations


Presentation on theme: "CS 3630 Database Design and Implementation"— Presentation transcript:

1 CS 3630 Database Design and Implementation

2 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

3 Relational Algebra Operations
Selection Projection Union Set difference Intersection Cartesian product Join

4 Relational Algebra Operations
Applied to relation instances (tables) The result is still a relation (table) Syntax

5 Selection  (predicate) (R) R is a relation
The result is a subset of relation R

6 Selection (II) Relation Schema: R (Att1, Att2, Att3) Relation Instance
x y x  (att1 < 100) (R) y (att1 < 100) (R) = {(90, 20, y)} The result is still a relation (table).

7 Selection (III)  ((att1 >= 100 and att2 <= 20) or att3  x) (R)
Table Schema R (Att1, Att2, Att3) Table Instance Att1 Att2 Att3 x y x  ((att1 >= 100 and att2 <= 20) or att3  x) (R) = {(100, 10, x), (90, 20, y)} As a table Att1 Att2 Att3 x y

8 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)

9 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

10 Projection (II) (Att1, Att3) (R) Att1 Att2 Att3 100 10 x 90 20 y

11 Projection (III) (Att3, Att1) (R) (Att1, Att3) (R)
Order of Attributes (Att3, Att1) (R) (Att1, Att3) (R)

12 Projection (IV) All attributes: all records * (R)
(Att1, Att2, Att3) (R) (Att1, Att3, Att2) (R)

13 Union R (Att1, Att2, Att3) S (Att1, Att2, Att4)
Att1 Att2 Att Att1 Att2 Att4 x x y y x R S Union compatible (Same schema) T (Att1, Att2, Att4, Att5) R T ?

14 Union (II) R (Att1, Att2, Att3) S (Att1, Att2, Att4)
Att1 Att2 Att Att1 Att2 Att4 x x y y x R S Att1 Att2 Att4(Att3) x y x x

15 Set Difference R - S Union compatible (same schema)
Att1 Att2 Att Att1 Att2 Att4 x x y y x Att1 Att2 Att3 x x

16 Intersection R S Union compatible (same schema)
Att1 Att2 Att Att1 Att2 Att4 x x y y x Att1 Att2 Att4(Att3) y

17 Cartesian Product R S Cardinality: |R S| = |R| * |S|
Degree : (Degree of R) + (Degree of S) R (Att1, Att2, Att3) Att1 Att2 Att3 x y z S (Att2, Att4) Att2 Att4 a b R S Att R.Att Att S.Att Att4 x a x b y a y b z a z b

18 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 x y z S: Att2 Att4 a b Att1 R.Att2 Att3 S.Att2 Att4 x a x b y a y b z a z b  Att1 R.Att2 Att3 S.Att2 Att4 y a y b z b

19 Natural Join R: Att1 Att2 Att3 S: Att2 Att4 100 10 x 20 a 90 20 y 15 b
z Common attribute Att2 R S =  (Att1, R.Att2, Att3, Att4)  (R.Att2 = S.Att2) (R S)   Att1 R.Att2 Att3 S.Att2 Att4 x a x b y a y b z a z b  Att1 R.Att2 Att3 S.Att2 Att4 y a z b  Att1 R.Att2 Att3 Att4 y a z b 

20 Natural Join R S =  (Att1, R.Att2, Att3, Att4)  (R.Att2 = S.Att2) (R S)   Att1 Att2 Att Att2 Att4 x a y b z Att1 R.Att2 Att3 S.Att2 Att Att1 R.Att2 Att3 S.Att2 Att4 x a z b x b y a y a y b Att1 R.Att2 Att3 Att4 z a z b z b  y a Remove the common attribute of the 2nd table Keep all attributes in the original order

21 Outer Join R S Left Outer-Join Keep non-matching tuples of R
(Natural unless otherwise stated) Att1 Att2 Att Att2 Att4 x a y b z Att1 R.Att2 Att3 Att4 x null y a z b 

22 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

23 Other Joins Right Outer Join Full Outer Join Semi-Join

24 Exercise: Natural Join
R S   Att1 Att2 Att Att2 Att4 x a y b z b w c d Att1 R.Att2 Att3 Att4 (first record of R does not join with any record of S) y a y b z b z c w d

25 Exercise: Left Outer Join
R S   Att1 Att2 Att Att2 Att4 x a y b z b w c d Att1 R.Att2 Att3 Att4 x null (does not join) y a y b z b z c w d

26 Exercise: Right Outer Join
R S   Att1 Att2 Att Att2 Att4 x a y b z b w c y d Att1 R.Att2 Att3 Att4 y a y a z b y b y b z c w d

27 Aggregation COUNT SUM AVG MIN MAX

28 Grouping

29 Relational Calculus

30 Assignment 1 Due Wednesday (week 2), Jan 31, at 5 pm D2L
It will be late after 5:00 pm

31 Due Wednesday (week 3), Feb 7
Assignment 2 Due Wednesday (week 3), Feb 7


Download ppt "CS 3630 Database Design and Implementation"

Similar presentations


Ads by Google