turn in hard copy in class, pdf through canvasdjmvfb/courses/cs2300/static/media... ·...

24
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 4 th Midterm Exam: Tuesday, Oct 9 th 1

Upload: others

Post on 13-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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

Page 2: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

2

CS2300: File Structures and

Introduction to Database Systems

Lecture 12: Relational Algebra

Doug McGeehan

Page 3: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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

Page 4: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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

Page 5: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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

Page 6: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

6

Division

Page 7: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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)

Page 8: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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

Page 9: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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?

Page 10: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

Division

• Consider: – A(X1,…,Xk,Y1,…,Ym)

– B(Y1,…,Ym)

• Then AB =

10

Page 11: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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

Page 12: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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

Page 13: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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

Page 14: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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

Page 15: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

Aggregate Function and

Grouping

15

Page 16: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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

Page 17: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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

Page 18: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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

Page 19: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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

Page 20: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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))

Page 21: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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

Page 22: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

Generalized Projection

2222

Page 23: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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

Page 24: Turn in hard copy in class, PDF through Canvasdjmvfb/courses/cs2300/static/media... · 2018-11-28 · PetAnimal(tagno, name, type, breed) • Find the ID of the veterinarian who checked

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