relational algebra - university of virginia school of ...up3f/cs4750/slides/4750meet20-21-ra.pdf ·...
Post on 18-Jan-2021
0 Views
Preview:
TRANSCRIPT
Fall 2020 – University of Virginia 1© Praphamontripong© Praphamontripong
Relational Algebra
CS 4750Database Systems
[H. Garcia-Molina, J.D. Ullman, J. Widom, Database Systems: The Complete Book, Ch.2]
[A. Silberschatz, H. F. Korth, S. Sudarshan, Database System Concepts, Ch.2.6][C.M. Ricardo, S.D. Urban, “Databases Illuminated, Ch.4.5]
Fall 2020 – University of Virginia 2© Praphamontripong
Relational Algebra (RA)• A data model is not just structure
• Needs a way to query the data • Needs a way to modify the data
• Relational algebra – “Procedural Query Language”
• Ways to build expressions by applying operators to atomic operands and/or other expressions of the algebra
• Atomic operands = variables that stand for relations or constants
• Mathematical foundation to define relationships and get data in and out of the system
• When a DBMS processes queries, a SQL query is translated into relational algebra
•Ways to construct •new relations from
given relations
Fall 2020 – University of Virginia 3© Praphamontripong
Why? (Query Designers’ Perspective)
SELECT …FROM …[WHERE …][GROUP BY …][HAVING …][ORDER BY …]
SQL “What” to get from the data
RA“How” to get
the data
Fall 2020 – University of Virginia 4© Praphamontripong
Why? (DBMS and Query Processors)
SELECT …FROM …[WHERE …][GROUP BY …][HAVING …][ORDER BY …]
SQL “What” to get from the data
1001010101100 0010111101010 001010100000 1010010100
RA“How” to get
the data
RDBMS
ComputersNeeds “how” to
get data
RA tree(logical plan) (physical plan)
Fall 2020 – University of Virginia 5© Praphamontripong
RA Operator Categories1. Operations that remove parts of a relation
• Selection• Projection
2. Operation that changes the schema of a relation• Renaming
3. Operations that combine the tuples of two relations• Set operations: Union, Intersection, Difference• Cartesian product• Natural join
4. Extended operation• Division
Additional operators: Assignment, Aggregate functions
Fall 2020 – University of Virginia 6© Praphamontripong
Selection ( σ )• Unary operation – take one operand
• Return all tuples that satisfy a condition (filter tuples)
• Conditions can be =, <, ≤, >, ≥, <> and combined with AND, OR, NOT
σC (R) where C is a set of conditions that involve the attribute(s) of R
Fall 2020 – University of Virginia 7© Praphamontripong
Example: Selection ( σ )
Student_lectureS_id Address Course TA1234 57 Hockanum Blvd Database Systems Minnie2345 1400 E. Bellows Database Systems Humpty3456 900 S. Detroit Cloud Computing Dumpty1234 57 Hockanum Blvd Web Programming Lang. Mickey5678 2131 Forest Lake Ln. Software Analysis Minnie
S_id Address Course TA1234 57 Hockanum Blvd Database Systems Minnie
Find all tuples in Student_lecture relation that have a “Database Systems” course and “Minnie” as TA
SELECT * FROM Student_lectureWHERE TA=‘Minnie’ AND Course=‘Database Systems’;
σTA=‘Minnie’ AND Course=‘Database Systems’ (Student_lecture)
No “wild card” in RA
Fall 2020 – University of Virginia 8© Praphamontripong
Example: Selection ( σ )
Student_lecture
σTA=‘Minnie’ ANDCourse =‘Database
Systems’
RA tree
Data source
Query output
Find all tuples in Student_lecture relation that have a “Database Systems” course and “Minnie” as TA
SELECT * FROM Student_lectureWHERE TA=‘Minnie’ AND Course=‘Database Systems’;
σTA=‘Minnie’ AND Course=‘Database Systems’ (Student_lecture)
Fall 2020 – University of Virginia 9© Praphamontripong
σDept=“CS” AND Course=2910 (Schedule)
Consider a simplified version of Lou’s List. Write RA to find all tuples in the Schedule relation that have “CS” and 2910 for Department and Course. Assume course is stored as integer.
Schedule
Let’s Try: Selection ( σ )CNumber Dept Course computingID DateTime Location15783 CS 1111 up3f MoWe 2:00pm - 3:15pm Thornton Hall E303… … … …16633 CS 2110 nb3f MoWeFr 1:00pm - 1:50pm Mechanical Engr 20516636 CS 2910 up3f TuTh 12:30pm - 1:45pm Rice 340
20294 CS 2910 up3f Th 11:15am – 12:00pm, Fr 3:00pm-3:45pm Rice 536, Olsson 001
… … …16639 CS 4640 up3f TuTh 2:00pm - 3:15pm Clark Hall 10816455 CS 4750 nb3f MoWe 3:30pm– 4:45pm Mechanical Engr 205… … …
Fall 2020 – University of Virginia 10© Praphamontripong
Schedule
Let’s Try: Selection ( σ ) cont.
σDept=“CS” AND Course=2910 (Schedule)
CNumber Dept Course computingID DateTime Location15783 CS 1111 up3f MoWe 2:00pm - 3:15pm Thornton Hall E303… … … …16633 CS 2110 nb3f MoWeFr 1:00pm - 1:50pm Mechanical Engr 20516636 CS 2910 up3f TuTh 12:30pm - 1:45pm Rice 340
20294 CS 2910 up3f Th 11:15am – 12:00pm, Fr 3:00pm-3:45pm Rice 536, Olsson 001
… … …16639 CS 4640 up3f TuTh 2:00pm - 3:15pm Clark Hall 10816455 CS 4750 nb3f MoWe 3:30pm– 4:45pm Mechanical Engr 205… … …
CNumber Dept Course computingID DateTime Location16636 CS 2910 up3f TuTh 12:30pm - 1:45pm Rice 340
20294 CS 2910 up3f Th 11:15am – 12:00pm, Fr 3:00pm-3:45pm Rice 536, Olsson 001
Fall 2020 – University of Virginia 11© Praphamontripong
• Unary operation – take one operand
• Return specified attributes of a relation
πA1, A2, …, Am (R) where Ai is attribute of a relation R
Projection ( π )
Fall 2020 – University of Virginia 12© Praphamontripong
Student_lecture
S_id Course1234 Database Systems2345 Database Systems3456 Cloud Computing1234 Web Programming Lang.5678 Software Analysis
S_id Address Course TA1234 57 Hockanum Blvd Database Systems Minnie2345 1400 E. Bellows Database Systems Humpty3456 900 S. Detroit Cloud Computing Dumpty1234 57 Hockanum Blvd Web Programming Lang. Mickey5678 2131 Forest Lake Ln. Software Analysis Minnie
Example: Projection ( π )Find all S_id and Course in Student_lecture relation
π S_ID, Course (Student_lecture)
SELECT S_id, Course FROM Student_lecture;
Fall 2020 – University of Virginia 13© Praphamontripong
Example: Projection ( π )Find all S_id and Course in Student_lecture relation
π S_ID, Course (Student_lecture)
SELECT S_id, Course FROM Student_lecture;
Student_lecture
π S_ID, Course
RA tree
Data source
Query output
Fall 2020 – University of Virginia 14© Praphamontripong
Student_lecture
TAMinnieHumptyDumptyMickey
S_id Address Course TA1234 57 Hockanum Blvd Database Systems Minnie2345 1400 E. Bellows Database Systems Humpty3456 900 S. Detroit Cloud Computing Dumpty1234 57 Hockanum Blvd Web Programming Lang. Mickey5678 2131 Forest Lake Ln. Software Analysis Minnie
π TA (Student_lecture)
More Example: Projection ( π )
Student_lecture
π TA
• Identical tuples collapse into a single tuple – no duplicates
Find all TAs in Student_lecture
relation
RA follows “Set” properties
Fall 2020 – University of Virginia 15© Praphamontripong
Consider a simplified version of Lou’s List (Spring 2019). Write RA to find all Departments and Courses in the Schedule relation that is taught by “up3f”
Schedule
Let’s Try: Projection ( π )
πDept, Course( σcomputingID=“up3f”(schedule) )
CNumber Dept Course computingID DateTime Location15783 CS 1111 up3f MoWe 2:00pm - 3:15pm Thornton Hall E303… … … …16633 CS 2110 nb3f MoWeFr 1:00pm - 1:50pm Mechanical Engr 20516636 CS 2910 up3f TuTh 12:30pm - 1:45pm Rice 340
20294 CS 2910 up3f Th 11:15am – 12:00pm, Fr 3:00pm-3:45pm Rice 536, Olsson 001
… … …16639 CS 4640 up3f TuTh 2:00pm - 3:15pm Clark Hall 10816455 CS 4750 nb3f MoWe 3:30pm– 4:45pm Mechanical Engr 205… … …
Fall 2020 – University of Virginia 16© Praphamontripong
CNumber Dept Course computingID DateTime Location15783 CS 1111 up3f MoWe 2:00pm - 3:15pm Thornton Hall E303… … … …16633 CS 2110 nb3f MoWeFr 1:00pm - 1:50pm Mechanical Engr 20516636 CS 2910 up3f TuTh 12:30pm - 1:45pm Rice 340
20294 CS 2910 up3f Th 11:15am – 12:00pm, Fr 3:00pm-3:45pm Rice 536, Olsson 001
… … …16639 CS 4640 up3f TuTh 2:00pm - 3:15pm Clark Hall 10816455 CS 4750 nb3f MoWe 3:30pm– 4:45pm Mechanical Engr 205… … …
Let’s Try: Projection ( π ) cont.
Dept CourseCS 1111CS 2910CS 4640
Schedule
πDept, Course( σcomputingID=“up3f”(schedule) )
Schedule
σcomputingID=‘up3f’
π Dept, Course
Notice: noduplicates
Fall 2020 – University of Virginia 17© Praphamontripong
ρfriend_contact(ID, primary_email, alternative_email)(contact)
Renaming ( ρ )
ID email1mi1y mickey@uva.edu
mi1y mi1y@uva.edu
email2mi1y@uva.edu
mickey@uva.edu
contact
=ID primary_email
mi1y mickey@uva.edu
mi1y mi1y@uva.edu
alternative_emailmi1y@uva.edu
mickey@uva.edu
friend_contact
• Unary operation – take one operand• Change the schema, not the instance• Rename the result of expression E to R’• Rename the resultant attributes to B1, B2, …, Bn
ρR’(B1, B2, …, Bn)(R) where Bi is new attribute name of a relation R’
Fall 2020 – University of Virginia 18© Praphamontripong
Union, Intersection, Difference
Binary operations – take left and right operands
Requirements: • Schemas of R and S must have same degree (number of attributes)
• Corresponding attributes of R and S must be based on the same domain / compatible data types
• Attributes are in the same order
Fall 2020 – University of Virginia 19© Praphamontripong
Union (R U S)
Each tuple of R and each tuple of S is put into the resultant relation
Fall 2020 – University of Virginia 20© Praphamontripong
Consider a simplified version of Lou’s List (Spring 2019). Find all Departments and Courses in the Schedule relation that is taught by “up3f”, “nb3f”, or both.
Schedule
Let’s Try: Union ( U )CNumber Dept Course computingID DateTime Location15783 CS 1111 up3f MoWe 2:00pm - 3:15pm Thornton Hall E303… … … …16633 CS 2110 nb3f MoWeFr 1:00pm - 1:50pm Mechanical Engr 20516636 CS 2910 up3f TuTh 12:30pm - 1:45pm Rice 340
20294 CS 2910 up3f Th 11:15am – 12:00pm, Fr 3:00pm-3:45pm Rice 536, Olsson 001
… … …16639 CS 4640 up3f TuTh 2:00pm - 3:15pm Clark Hall 10816455 CS 4750 nb3f MoWe 3:30pm– 4:45pm Mechanical Engr 205… … …
πDept, Course( σcomputingID=“up3f”(schedule) )
πDept, Course( σcomputingID=“nb3f”(schedule) )
U
Fall 2020 – University of Virginia 21© Praphamontripong
Schedule
Let’s Try: Union ( U ) cont.
πDept, Course( σcomputingID=“up3f”(schedule) )
πDept, Course( σcomputingID=“nb3f”(schedule) )
U
Dept courseCS 1111CS 2910CS 4640
Dept courseCS 2110CS 4750
Dept courseCS 1111CS 2910CS 4640CS 2110CS 4750
U =
CNumber Dept Course computingID DateTime Location15783 CS 1111 up3f MoWe 2:00pm - 3:15pm Thornton Hall E303… … … …16633 CS 2110 nb3f MoWeFr 1:00pm - 1:50pm Mechanical Engr 20516636 CS 2910 up3f TuTh 12:30pm - 1:45pm Rice 340
20294 CS 2910 up3f Th 11:15am – 12:00pm, Fr 3:00pm-3:45pm Rice 536, Olsson 001
… … …16639 CS 4640 up3f TuTh 2:00pm - 3:15pm Clark Hall 10816455 CS 4750 nb3f MoWe 3:30pm– 4:45pm Mechanical Engr 205… … …
Fall 2020 – University of Virginia 22© Praphamontripong
Intersection(R∩S)
A tuple is in the resultant relation if and only if it is in both R and S
Fall 2020 – University of Virginia 23© Praphamontripong
Consider a simplified version of Lou’s List (Spring 2019). Find all Departments in the Schedule relation that offer courses taught by “up3f” and “nb3f”.
Schedule
Let’s Try: Intersection (∩)CNumber Dept Course computingID DateTime Location15783 CS 1111 up3f MoWe 2:00pm - 3:15pm Thornton Hall E303… … … …16633 CS 2110 nb3f MoWeFr 1:00pm - 1:50pm Mechanical Engr 20516636 CS 2910 up3f TuTh 12:30pm - 1:45pm Rice 340
20294 CS 2910 up3f Th 11:15am – 12:00pm, Fr 3:00pm-3:45pm Rice 536, Olsson 001
… … …16639 CS 4640 up3f TuTh 2:00pm - 3:15pm Clark Hall 10816455 CS 4750 nb3f MoWe 3:30pm– 4:45pm Mechanical Engr 205… … …
πDept ( σcomputingID=“up3f”(schedule) )
πDept ( σcomputingID=“nb3f”(schedule) )
∩
Fall 2020 – University of Virginia 24© Praphamontripong
Schedule
Let’s Try: Intersection (∩) cont.
πDept ( σcomputingID=“up3f”(schedule) )
CNumber Dept Course computingID DateTime Location15783 CS 1111 up3f MoWe 2:00pm - 3:15pm Thornton Hall E303… … … …16633 CS 2110 nb3f MoWeFr 1:00pm - 1:50pm Mechanical Engr 20516636 CS 2910 up3f TuTh 12:30pm - 1:45pm Rice 340
20294 CS 2910 up3f Th 11:15am – 12:00pm, Fr 3:00pm-3:45pm Rice 536, Olsson 001
… … …16639 CS 4640 up3f TuTh 2:00pm - 3:15pm Clark Hall 10816455 CS 4750 nb3f MoWe 3:30pm– 4:45pm Mechanical Engr 205… … …
πDept ( σcomputingID=“nb3f”(schedule) )
∩
DeptCS
DeptCS∩ =
Dept
CS
Fall 2020 – University of Virginia 25© Praphamontripong
Difference (R − S)
A tuple is in the resultant relation if and only if it is in R but not in S
Fall 2020 – University of Virginia 26© Praphamontripong
ID name
go9y Goofy
mi2e Minnie
do3d Donald
pl4o Pluto
do5d Donald
pe6e Pete
Example: Difference (−)
CS1_students
ID name
mi1y Mickey
mi2e Minnie
do3d Donald
da4y Daisy
do5d Donald
Find tuples that are in one relation but are not in another
Same number of attributes
Attribute names match, Compatible type
CS1_students – CS2_students
ID name
mi1y Mickey
da4y Daisy
− =
CS2_students
Fall 2020 – University of Virginia 27© Praphamontripong
Let’s Try: Difference (−)
πmaker( σtype=“laptop”(Product) )
πmaker( σtype=“PC”(Product) )
-
Consider the following schema statements. Write RA to find manufacturers (makers) that sell Laptops but not PC’s
Product (maker, model, type) -- type is stored as “laptop” or “PC”
Fall 2020 – University of Virginia 28© Praphamontripong
Cartesian Product (R × S)
R x S where R(a1, a2, ..., an) and S(b1, b2, ..., bk)
ID Namehm1y Humpty
dm2y Dumpty
• Binary operations – take two operand• So-called “cross-product” or “product”• Combine two relations• Usually not meaningful when it is performed alone
R x S ≠ S x R
Employee
EID DeptNamehm1y CS
dm2y EE
Department
ID Name EID DeptNamehm1y Humpty hm1y CS
hm1y Humpty dm2y EE
dm2y Dumpty hm1y CS
dm2y Dumpty dm2y EE
Employee x Department
Fall 2020 – University of Virginia 29© Praphamontripong
More Example: Product (×)
ID email
mi1y mickey@uva.edu
mi2e minnie@uva.edu
mi1y mi1y@uva.edu ×
C1× C2
=ID email
mi1y mickey@uva.edu
mi2e minnie@uva.edu
mi1y mi1y@uva.edu
Contact (Let’s call it C1) Contact (Let’s call it C2)
Usually not meaningfulwhen it is performedalone
ρC1(ID1, email1)(contact) ρC2(ID2, email2)(contact)
Fall 2020 – University of Virginia 30© Praphamontripong
More Example: Product (×)
×
C1× C2
=
Find all students who have more than one email
(2 steps: cross product, then select tuples)
Meaningful when it is followed by other operations.
ID email
mi1y mickey@uva.edu
mi2e minnie@uva.edu
mi1y mi1y@uva.edu
ID email
mi1y mickey@uva.edu
mi2e minnie@uva.edu
mi1y mi1y@uva.edu
Contact (Let’s call it C1) Contact (Let’s call it C2)ρC1(ID1, email1)(contact) ρC2(ID2, email2)(contact)
Fall 2020 – University of Virginia 31© Praphamontripong
Let’s Try: Product (×)Find all students who have more than one email(2 steps: cross product, then select tuples)
SELECT * FROM C1, C2WHERE C1.ID1 = C2.ID2AND C1.email1 <> C2.email2
σID1=ID2 ^ email1≠ email2 (C1× C2)
ID email
mi1y mickey@uva.edu
mi2e minnie@uva.edu
mi1y mi1y@uva.edu
ID email
mi1y mickey@uva.edu
mi2e minnie@uva.edu
mi1y mi1y@uva.edu
Contact (Let’s call it C1) Contact (Let’s call it C2)ρC1(ID1, email1)(contact) ρC2(ID2, email2)(contact)Write RA to solve this
problem
(for more practice, also write SQL to solve this problem)
Fall 2020 – University of Virginia 32© Praphamontripong
Natural Join (R ⋈ S)• Binary operations – take two operand• Merge relations on the specified condition
R ⋈ S where R and S has a set of attributes that are in common
R ⋈ S = πA ( σC (R x S) )
2. Check equality of all common attributes
(attributes with same names)
3. Eliminate duplicate common attributes
(attributes with same names)
1. Cross product
Fall 2020 – University of Virginia 33© Praphamontripong
A B C1 2 3
6 7 8
9 7 8
B C D A2 3 4 1
2 3 5 1
7 8 10 6
7 8 10 9
RB C D2 3 4
2 3 5
7 8 10
7 9 9
S
A B C1 2 3
6 7 8
9 7 8
Example: Natural Join ( ⋈ )
B C D2 3 4
2 3 5
7 8 10
7 9 9
R SA B C D1 2 3 4
1 2 3 5
6 7 8 10
9 7 8 10
R ⋈ S
⋈ =
S ⋈ R
⋈ =
Fall 2020 – University of Virginia 34© Praphamontripong
Let’s Try: Natural Join ( ⋈ )Consider the following schema statements. Write RA to find manufacturers (makers) that make laptops with a hard disk (hd) of at least 100GB
Product (maker, model, type) -- type is stored as “laptop” or “PC”
Laptop (model, speed, ram, hd, screen, price)-- hd stores size of hard disk
πmaker ( Product ⋈ ( σ hd ≥ 100 (Laptop) ) )
Fall 2020 – University of Virginia 35© Praphamontripong
Division (R ÷ S)
A B C D E D E A B CR S R ÷ S
=÷
• Binary operations – take two operand• Use to find “for all” queries• Find “A” for all “B” where “A” and “B” are sets of attributes
AB ÷ B = A
R ÷ S = πA-B (R) - πA-B ( (πA-B (R) × S) – R )
Fall 2020 – University of Virginia 36© Praphamontripong
Breakdown: Division (R ÷ S)R ÷ S = πA-B (R) - πA-B ( (πA-B (R) × S) – R )
1. Project attributes of R that are not in S, and then product with the divisor S
A B C Da 3 x ma 1 x oa 3 y ob 1 x mc 4 y ob 2 y nc 4 x m
C Dy ox m
R S πA-B (R) SA B C Da 3 y oa 3 x ma 1 y oa 1 x mb 1 y ob 1 x mc 4 y oc 4 x mb 2 y ob 2 x m
A Ba 3a 1b 1c 4b 2
C Dy ox m
where A and B are sets of attributes of R and S
× =
πA-B (R) x S
Fall 2020 – University of Virginia 37© Praphamontripong
Breakdown: Division (R ÷ S)R ÷ S = πA-B (R) - πA-B ( (πA-B (R) × S) – R )
2. Extract tuples from the product that were not in R
A B C Da 3 x ma 1 x oa 3 y ob 1 x mc 4 y ob 2 y nc 4 x m
C Dy ox m
R S
where A and B are sets of attributes of R and S
πA-B (R) x S (πA-B (R) x S) – RA B C Da 3 y oa 3 x ma 1 y oa 1 x mb 1 y ob 1 x mc 4 y oc 4 x mb 2 y ob 2 x m
A B C Da 1 y oa 1 x mb 1 y ob 2 y ob 2 x m
Fall 2020 – University of Virginia 38© Praphamontripong
Breakdown: Division (R ÷ S)R ÷ S = πA-B (R) - πA-B ( (πA-B (R) × S) – R )
3. Project only attributes that are in the original R but not in S
A B C Da 3 x ma 1 x oa 3 y ob 1 x mc 4 y ob 2 y nc 4 x m
C Dy ox m
R S
where A and B are sets of attributes of R and S
πA-B(πA-B (R) x S) – R)(πA-B (R) x S) – RA B C Da 1 y oa 1 x mb 1 y ob 2 y ob 2 x m
A Ba 1b 1b 2
Fall 2020 – University of Virginia 39© Praphamontripong
Breakdown: Division (R ÷ S)R ÷ S = πA-B (R) - πA-B ( (πA-B (R) × S) – R )
4. Extract only the tuples that are in the original R
A B C Da 3 x ma 1 x oa 3 y ob 1 x mc 4 y ob 2 y nc 4 x m
C Dy ox m
R S
where A and B are sets of attributes of R and S
πA-B(πA-B (R) x S) – R)πA-B (R)A Ba 3a 1b 1c 4b 2
– =A Ba 1b 1b 2
A Ba 3c 4
Fall 2020 – University of Virginia 40© Praphamontripong
A B C D
a 3 x m
a 1 x o
a 3 y o
b 1 x m
c 4 y o
b 2 y n
c 4 x m
C D
y o
x m
A B
a 3
c 4
R S R ÷ S=÷
Short cut: Division (R ÷ S)
Fall 2020 – University of Virginia 41© Praphamontripong
Let’s Try: Division ( ÷ )
πsname,bid ( Sailors ⋈ Reserves) ÷ (Boats)
sname bid
… …
bid bname color
… … …AB ÷ B = A
The divisor is B. Don’t care the
remaining columns.
Consider the following schema statements. Write RA to find the names of sailors who have reserved all the boats
Boats (bid, bname, color) Sailors (sid, sname, rating, age)Reserves (sid, bid, day)
÷
= sname
…
Fall 2020 – University of Virginia 42© Praphamontripong
Assignment ( ← )
Similar to assignment statement in programming
v ← E where v is a temporary variable representing a relation, E is an expression
Example: Find manufacturers (makers) that make laptops with a hard disk (hd) of at least 100GB
Product (maker, model, type) Laptop (model, speed, ram, hd, screen, price)
R1← σ hd ≥ 100 (Laptop) ) )
πmaker ( R2 )
R2← Product ⋈ ( R1)πmaker (Product ⋈ (σ hd ≥ 100 (Laptop)))
Fall 2020 – University of Virginia 43© Praphamontripong
Aggregate Function ( G )• Not relational operators• Use Group by to help summarize a column in some way• Five standard operators: sum, avg, count, min, and max
where
A1, A2, …, An are attributes of a relation RG1, G2, .., Gm are attributes on which to group;F1, F2, …, Fn are aggregation functions on an attribute(Ai)
𝐆34,36,…,38, (R)<4 =4 ,<6 =6 ,…,<>(=>)
Fall 2020 – University of Virginia 44© Praphamontripong
Example: Aggregate FunctionConsider the following schema statements. Write RA to find the number of each of the colors of the boats
Boats (bid, bname, color) Sailors (sid, sname, rating, age)Reserves (sid, bid, day)
color G count(bid) (Boats)
color count(bid)
… …Result in
Fall 2020 – University of Virginia 45© Praphamontripong
Let’s Try: Aggregate FunctionConsider the following schema statements. Write RA to find the the average and max loan amount of each customer
Loan (loan_number, branch_name, amount)Borrower (customer_name, loan_number)
customer_name G avg(amount), max(amount) (Loan ⋈ Borrower)
customer_name avg(amount) max(amount)
… …Result in
Fall 2020 – University of Virginia 46© Praphamontripong
Binaryoperations
Union rUs -- “or”
Intersection r∩ s-- “and”
Setdifference r− s
AdditionalExpressiveness
AssignmentAggregatefunctions
Takeonerelation, returnanewrelationUnaryoperations
Selection σp(r)
Project πA1, A2, …, Am (r)
Rename ρx(A1, A2, …, An)(E)
Findtuplesthatsatisfyagivencondition
Slicearelation,returnanewrelationwithcertainattributes
RenametheresultofexpressionEtox;renameresultantattributestoA1,A2,…
Combinetuplesfrom2relations[require:samenumberofattributes;compatibledomains][result:sameattributes]
Combinetuplesfrom2relations[require:samenumberofattributes;compatibledomains][result:sameattributes]
Findtuplesthatareinonerelationbutarenotinanother[require:samenumberofattributes;compatibledomains][result:sameattributes]
Cartesianproduct r × s
Naturaljoin r⋈ s
Division r÷ s
Taketworelation, returnanewrelation
Combine2relations,allcombination[result:combinedattributes]
Selecttuplesthatsatisfythematchingconditionsfromcombinedrelations[result:combinedattributes]
SimilartoAB÷ BFind“A”forall“B”[require:thereexistsB’sattributesinA][result:Aschema]
Summary RA Operators
Fall 2020 – University of Virginia 47© Praphamontripong
Wrap-UpRelational operators
• Selection, projection • Renaming• Set operations, Cartesian product, Natural join• Division
Additional operators• Assignment, aggregate function
What’s next?• Translating between SQL and RA• RA tree
top related