turn in hard copy in class, pdf through canvasdjmvfb/courses/cs2300/static/media... ·...
TRANSCRIPT
Announcements
• Homework 2 reminder
– Due Thursday, Oct 4 at beginning of class
– Turn in hard copy in class, PDF through
Canvas
• Quiz 2 also on Thursday, Oct 4th
• Midterm Exam: Tuesday, Oct 9th
1
2
CS2300: File Structures and
Introduction to Database Systems
Lecture 12: Relational Algebra
Doug McGeehan
3333
Brief Review
Relational Algebra
• Basic operations
– Selection (): Selects a subset of rows from relation.
– Projection (π) : Deletes unwanted columns from relation.
– Cross-product (X)
– Set-difference or Minus (-)
– Union (U)
• Additional operations– Renaming
– Intersection, Join
– Division
– Aggregation and grouping
– Generalized Projection
4
Question (Challenging)
Find the id of the sailors who reserved ALL boats.
Reserve
daybidsid
1/1/9910258
10/10/9610122
8/8/9610158
11/11/9610358
yearnamebid
1Victory101
3Wind102
1Dream103
Boat
5
Question (Challenging)
Find the id of the sailors who reserved ALL boats.
Step 1: create a table where everyone reserves all boats.
R1 (πsid Reserve) X (π bid Boat)
Step 2: find sailors who didn’t reserve all boats
R2 R1 – πsid, bid Reserve
Step 3: find sailors who reserve all boats
R3 (π sid Reserve) – (πsid R2)
Reserve (sid, bid, day) Boat (bid, name, year)
5
6
Division
7
Reconsider the Question
Find the id of the sailors who reserved ALL boats.
(πsid,bid Reserve) (πbid Boat)
Reserve (sid, bid, day)
Boat (bid, name, year)
Division
• Division is useful to express “for all” queries
• Examples:
– Find students who studied ALL of Dr Cohen’s
courses
– Find lecturers who have taught ALL of the
students
• We will use to denote division.
8
9
Division
• Can perform R S if S’s schema is a
subset of R’s schema
• The result is a relation with all attributes
appearing in R but not in S
• R(X1,…,Xk,Y1,…,Ym), S(Y1,…,Ym):
– R S : What attributes are in the result?
Division
• Consider: – A(X1,…,Xk,Y1,…,Ym)
– B(Y1,…,Ym)
• Then AB =
10
11
Find suppliers (sno) from A who
supply All parts (pno) from B
sno pno
S1
S1
S1
S1
S2
S2
S3
S4
S4
P1
P2
P3
P4
P1
P2
P2
P2
P4
A
P2
pno
B
sno =
S1
S2
S3
S4
11
12
Find suppliers (sno) from A who
supply All parts (pno) from B
sno pno
S1
S1
S1
S1
S2
S2
S3
S4
S4
P1
P2
P3
P4
P1
P2
P2
P2
P4
A
sno
=S1
S4
P2
P4
pno
B
12
13
Find suppliers (sno) from A who
supply All parts (pno) from B
sno pno
S1
S1
S1
S1
S2
S2
S3
S4
S4
P1
P2
P3
P4
P1
P2
P2
P2
P4
A
sno =S1P1
P2
P4
pno
B
13
14
Example for Division
• Consider the following two relations
VetCheckup(vid, tagno, timestamp)
PetAnimal(tagno, name, type, breed)
• Find the ID of the veterinarian who
checked on all huskies
Aggregate Function and
Grouping
15
Example
How many pancakes were purchased
over the past three months?
What is the average amount spent across
all customers on pancakes vs waffles?
16
Aggregate Function and Grouping
• Specify mathematical aggregate functions
on collections of values from the database.
• Common functions – SUM
– AVERAGE
– MAXIMUM and MINIMUM
– COUNT (for counting tuples)
• Tuples can be grouped before applying the aggregation functions.
17
Aggregate Function and Grouping
• Written as grouping_attributesℱF1,F2,…Fn(R)
• Group tuples having the same values for the
grouping attributes
(can be multiple attributes)
• Each function Fi is in the form:
– <function> <attribute>
– Example: Max salary; Count ssn
• Attributes in the result include grouping
attributes and all aggregation functions.18
Aggregate Function and Grouping
19
yearsnamesid
3white240
3jones202
1adams450
SyearℱCount sid (R)
Report the total number of students for each year
Count_sidyear
23
11
20
coursesidtid
os20220
calculus45010
db 20220
db24020
depttnametid
mathcohen10
cslevy20
Report the number of courses
taught by Dr. Levy.
T
R
Count_course
2
20
ℱCount course (course(R*(tname=`levy’T))
21
More Examples
• EMPLOYEE (SSN, Name, Salary, Year)
• ℱMAX Salary (EMPLOYEE)
– retrieves the maximum salary value from the EMPLOYEE
relation
• YearℱMIN Salary, AVG Salary (EMPLOYEE)
– retrieves the minimum and average Salary values from the
EMPLOYEE relation for each year.
• ℱSUM Salary (EMPLOYEE)
– retrieves the sum of the Salary from the EMPLOYEE relation
Generalized Projection
2222
Generalized Projection
• Consider the following relation and
report the net salary of an employee
• Employee(ssn, salary, tax, year)
• Net salary = salary – tax
salary-taxEmployee
2323
Generalized Projection
• Allows functions on attributes in the projection list.
F1,F2,…,Fn
(R)
• F1,F2,…Fn are functions over the attributes in R
and may involve constants.
• This is needed when developing reports where
computed values have to be produced
2424