R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name...

28
RELATIONAL ALGEBRA Joe Meehean 1

description

R ELATIONAL A LGEBRA Algebra for tables Transform 1 or more tables into another table Based on a small set of operators 3

Transcript of R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name...

Page 1: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

1

RELATIONAL ALGEBRA

Joe Meehean

Page 2: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

2

TABLES FOR TODAY’S LECTURE

StdNo Last Name

First Name

Major GPA

732 Arnett Will Theatre 3.74967 Crumb Robert Art 2.3764 Vogelzan

gAnna Music 3.5

568 Park Phil CS 3.6587 Park Anna CS 3.9

OfferingID StdNo CreditsAA7 732 4BB8 732 3

OfferingID CourseName

AA7 Calculus IBB8 Art History

Students

Enrolled Offering

Page 3: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

3

RELATIONAL ALGEBRA Algebra for tables Transform 1 or more tables into another table Based on a small set of operators

Page 4: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

4

ALTERNATIVE TERMINOLOGYTable-Oriented(Database)

Set-Oriented (Mathematics)

Table RelationRow TupleColumn AttributeAND ΛOR V

Page 5: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

5

SELECTION OPERATOR Represented by σ symbol AKA restrict operator Produces a subset of the input relation

(table) subset of the tuples (rows)

Uses condition to indicate which rows to retain condition can include: =, ≠, <, >, ≥, ≤, V, Λ

e.g., for relation Students(stdno, last_name, first_name, major, gpa) σgpa > 3.5(Students) gives the students that have gpa greater than

3.5

Page 6: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

6

SELECTION OPERATOR Conceptual Picture

StdNo Last Name

First Name

Major GPA

732 Arnett Will Theatre 3.74967 Crumb Robert Art 2.3764 Vogelzan

gAnna Music 3.5

568 Park Phil CS 3.6587 Park Anna CS 3.9

Page 7: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

7

SELECTION OPERATOR Conceptual Picture

StdNo Last Name

First Name

Major GPA

732 Arnett Will Theatre 3.74967 Crumb Robert Art 2.3764 Vogelzan

gAnna Music 3.5

568 Park Phil CS 3.6587 Park Anna CS 3.9

Page 8: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

8

PROJECTION OPERATOR Represented by π Produces a subset of the input table

subset of the columns e.g., πlast_name,major Student

gives the Student relation without the first_name and stdnr attributes

Also removes duplicates projection removes attributes, so duplicate rows

can occur these duplicates are removed real database systems often do not remove

duplicates

Page 9: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

9

PROJECTION OPERATOR Conceptual Picture

StdNo Last Name

First Name

Major GPA

732 Arnett Will Theatre 3.74967 Crumb Robert Art 2.3764 Vogelzan

gAnna Music 3.5

568 Park Phil CS 3.6587 Park Anna CS 3.9

Page 10: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

10

PROJECTION OPERATOR Conceptual Picture

StdNo Last Name

First Name

Major GPA

732 Arnett Will Theatre 3.74967 Crumb Robert Art 2.3764 Vogelzan

gAnna Music 3.5

568 Park Phil CS 3.6587 Park Anna CS 3.9

Page 11: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

11

PROJECTION OPERATOR Conceptual Picture

StdNo Last Name

First Name

Major GPA

732 Arnett Will Theatre 3.74967 Crumb Robert Art 2.3764 Vogelzan

gAnna Music 3.5

568 Park Phil CS 3.6587 Park Anna CS 3.9

Page 12: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

12

PRODUCT OPERATOR Represented by × AKA extended cross product operator Produces all possible combination of tuples

(rows) between 2 relations (tables) # tuples (rows) in output

# tuples in input1 * # tuples in input2 # attributes (columns) in output

# attributes in input1 + #attributes in input2

Page 13: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

13

PRODUCT EXAMPLE Student × EnrolledStdNo Last

NameFirst Name

Major GPA

732 Arnett Will Theatre 3.74764 Vogelzan

gAnna Music 3.5

OfferingID StdNo CreditsAA7 732 4BB8 732 3StdNo Last

NameFirst Name

Major GPA OfferingID

StdNo Credits

732 Arnett Will Theatre 3.74 AA7 732 4732 Arnett Will Theatre 3.74 BB8 732 3764 Vogelzan

gAnna Music 3.5 AA7 732 4

764 Vogelzang

Anna Music 3.5 BB8 732 3

Page 14: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

14

JOIN OPERATOR Represented by Also combines 2 tables Requires a condition between tuples

all conditions from selection are OK Join is derived from product and selection

R c S = σc(R × S)

Page 15: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

15

JOIN OPERATOR Equi-join

matching condition is that 2 rows have same value for some attribute(s)

c = d

Natural join two relations have same attribute values of attributes must match for tuple to

be included duplicate attribute is not included in

resulting relation no need to include condition for natural join

Page 16: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

16

JOIN OPERATOR EXAMPLE Student Enrolled StdNo Last

NameFirst Name

Major GPA

732 Arnett Will Theatre 3.74764 Vogelzan

gAnna Music 3.5

OfferingID StdNo Credits

AA7 732 4BB8 732 3StdNo Last

NameFirst Name

Major GPA OfferingID

Credits

732 Arnett Will Theatre 3.74 AA7 4732 Arnett Will Theatre 3.74 BB8 3

Page 17: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

QUESTIONS?

17

Page 18: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

18

QUICK REVIEW Select operator (σ)

removes tuples (rows) from relation (table) Project operator (π)

removes attributes (columns) from relation (table)

Cross product operator (×) combines attributes from 2 relations matches every tuple from both input relations

Join operator ( ) combines attributes from 2 relations matches tuples based on condition often same value for an attribute

Page 19: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

19

RENAME OPERATOR Represented by ρ Renames relations or attributes (or both) Rename relation

ρ(Z, A) relation A is now called Z

Rename attributes ρ((old1 → new1, old2 → new2, old3 → new3), A) A’s attributes old1, old2, and old3 are renamed

Page 20: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

20

RENAME OPERATOR Rename both attributes and relation

ρ(Z, (old1 → new1, old2 → new2, old3 → new3), A)

Simplify expressions ρ( music_majors, σmajor = music student) πlast_name, first_name(σgpa > 3.5 music_majors)

Page 21: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

21

UNION OPERATOR Represented by U Combines two relations Result contains all tuples in both relations e.g., orange and green parts

σmajor = music students U σ major = theatre students

Page 22: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

22

INTERSECTION OPERATOR Represented by ∩ Combines two relations Results contains only tuples that are in both

input relations e.g., music majors with good gpa

σmajor = music students ∩ σ gpa > 3.5 students

Page 23: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

23

DIFFERENCE OPERATOR Represented by − Combines two relations Tuples in the first table, but not in second e.g. music majors that don’t have a good gpa

σmajor = music students − σ gpa > 3.5 students

Page 24: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

24

UNION COMPATIBLE Relations are union compatible if

they have the same number of attributes AND corresponding attributes have the same

data type (ints, chars, etc…) usually means relations have same attributes

Union, intersect, and difference require union compatible relations

e.g. red parts supplied by XYZ ρ(XYZparts, parts (catalog

σsname=XYZsupplier)) σcolor = red parts ∩ πpname, pid, color XYZparts

Page 25: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

25

DIVISION OPERATOR Represented by / Combines two relations, A & B

A has two attributes (x,y) B has one attribute (y)

A/B is all x’s such that (x,y) exists in A for all y’s in B

Result has only a singe atribute (x)

Page 26: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

26

DIVISION OPERATOR For each x value in A

consider the set of y values x maps to in A if this set contains all y values in B then x is in A/B

e.g. student enrolled in all offerings ρ(super_student_ids,

πstd_no,offering_id enrolled / πoffering_idofferings) πlastname, firstname(students super_student_ids)

Page 27: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

27

DIVISION OPERATOR Derive A/B

think about what doesn’t belong in result get set of all x,y mappings (πx A) × B remove those mappings that appear in A ((πx A) × B) − A what’s left must be x’s that don’t map to all y’s πx ( ((πx A) × B) − A ) remove them from the set of x’s πx A − πx ( ((πx A) × B) − A )

Page 28: R ELATIONAL A LGEBRA Joe Meehean 1. T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3.

QUESTIONS?

28