Upload
joy-peters
View
222
Download
0
Embed Size (px)
DESCRIPTION
R ELATIONAL A LGEBRA Algebra for tables Transform 1 or more tables into another table Based on a small set of operators 3
Citation preview
1
RELATIONAL ALGEBRA
Joe Meehean
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
3
RELATIONAL ALGEBRA Algebra for tables Transform 1 or more tables into another table Based on a small set of operators
4
ALTERNATIVE TERMINOLOGYTable-Oriented(Database)
Set-Oriented (Mathematics)
Table RelationRow TupleColumn AttributeAND ΛOR V
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
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
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
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
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
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
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
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
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
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)
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
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
QUESTIONS?
17
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
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
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)
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
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
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
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
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)
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)
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 )
QUESTIONS?
28