Download presentation
Presentation is loading. Please wait.
Published byJoy Peters Modified over 9 years ago
1
R ELATIONAL A LGEBRA Joe Meehean 1
2
T ABLES FOR T ODAY ’ S L ECTURE 2 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3 764VogelzangAnnaMusic3.5 568ParkPhilCS3.6 587ParkAnnaCS3.9 OfferingIDStdNoCredits AA77324 BB87323 OfferingIDCourseName AA7Calculus I BB8Art History Students Enrolled Offering
3
R ELATIONAL A LGEBRA Algebra for tables Transform 1 or more tables into another table Based on a small set of operators 3
4
A LTERNATIVE T ERMINOLOGY Table-Oriented (Database) Set-Oriented (Mathematics) TableRelation RowTuple ColumnAttribute ANDΛ ORV 4
5
S ELECTION O PERATOR 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 5
6
S ELECTION O PERATOR Conceptual Picture 6 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3 764VogelzangAnnaMusic3.5 568ParkPhilCS3.6 587ParkAnnaCS3.9
7
S ELECTION O PERATOR Conceptual Picture 7 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3 764VogelzangAnnaMusic3.5 568ParkPhilCS3.6 587ParkAnnaCS3.9
8
P ROJECTION O PERATOR 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 8
9
P ROJECTION O PERATOR Conceptual Picture 9 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3 764VogelzangAnnaMusic3.5 568ParkPhilCS3.6 587ParkAnnaCS3.9
10
P ROJECTION O PERATOR Conceptual Picture 10 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3 764VogelzangAnnaMusic3.5 568ParkPhilCS3.6 587ParkAnnaCS3.9
11
P ROJECTION O PERATOR Conceptual Picture 11 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 967CrumbRobertArt2.3 764VogelzangAnnaMusic3.5 568ParkPhilCS3.6 587ParkAnnaCS3.9
12
P RODUCT O PERATOR 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 12
13
P RODUCT E XAMPLE Student × Enrolled 13 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 764VogelzangAnnaMusic3.5 OfferingIDStdNoCredits AA77324 BB87323 StdNoLast NameFirst Name MajorGPAOffering ID StdNoCredits 732ArnettWillTheatre3.74AA7 7324 ArnettWillTheatre3.74BB8 7323 764VogelzangAnnaMusic3.5AA7 7324 764VogelzangAnnaMusic3.5BB8 7323
14
J OIN O PERATOR 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) 14
15
J OIN O PERATOR 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 15
16
J OIN O PERATOR E XAMPLE Student Enrolled 16 StdNoLast Name First Name MajorGPA 732ArnettWillTheatre3.74 764VogelzangAnnaMusic3.5 OfferingIDStdNoCredits AA77324 BB87323 StdNoLast NameFirst Name MajorGPAOffering ID Credits 732ArnettWillTheatre3.74AA7 4 732ArnettWillTheatre3.74BB8 3
17
Q UESTIONS ? 17
18
Q UICK R EVIEW 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 18
19
R ENAME O PERATOR 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 19
20
R ENAME O PERATOR 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) 20
21
U NION O PERATOR 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 21
22
I NTERSECTION O PERATOR 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 22
23
D IFFERENCE O PERATOR 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 23
24
U NION C OMPATIBLE 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=XYZ supplier)) σ color = red parts ∩ π pname, pid, color XYZparts 24
25
D IVISION O PERATOR 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) 25
26
D IVISION O PERATOR 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_id offerings) π lastname, firstname (students super_student_ids) 26
27
D IVISION O PERATOR 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 ) 27
28
Q UESTIONS ? 28
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.