Top Banner
Review for Review for Midterm Midterm
29

Review for Midterm

Feb 05, 2016

Download

Documents

Barbie

Review for Midterm. name. age. ssn. address. date. qty. prescribes. Patients. price. tradeName. Drugs. name. specialty. formula. ssn. exp_year. stDate. address. name. phone. isContracted. Makes. endDate. Doctors. Pharmacies. name. txt. PharmCo. phone. super. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Review for Midterm

Review forReview forMidtermMidterm

Page 2: Review for Midterm

Problem 2.7Problem 2.7

HasPriPhyPatients

address

name

ssn

age

Doctors

exp_year

name

ssn

specialty

Pharmacies

name address phone

PharmCo

name

phone

Makes

stDate

endDate

txt

super

isContracted

date qty

Sell

prescribes

price

DrugstradeName

formula

Page 3: Review for Midterm

Relational Database SchemaDoctors(ssn:integer,name:string, specialty:string,year_exp:integer)PK: ssn

Patients(ssn:integer,name:string,age:integer,address:string,ssnDoctor:integer)PK: ssn FK: ssnDoctor references Doctors

Pharmacies(name:string, phone:integer, address:string)PK: name

PharmCo(name:string, phone:integer)PK: name

Drugs(tradeName: string,formula:string,pharmCoName: string)PK: pharmCoName, tradeName FK: pharmCoName references PharmCo

Prescribes(ssnPatient:integer, ssnDoctor:integer, pharmCoName: string , tradeName:sring, date:date, qty:integer)PK: FK: ssnPatient references Patients ssnPatient,ssnDoctor,pharCoName,tradeName FK: ssnDoctor references Doctors

FK: (pharCoName,tradeName) referencesDrugs

Sells(pharmCoName: string, tradeName: string, pharmacyName: string , price:float)PK: pharmCoName,tradeName,pharmacyName FK: (pharmCoName,tradeName) references Drugs

FK: pharmacyName references Pharmacies

Contracts(pharmCoName: string,pharmacyName: string , startDate:date, endDate:date, text:string, supervisor:string)PK: pharCoName,pharmacyName FK: pharmCoName references PharmCo

FK: pharmacyName references Pharmacies

Page 4: Review for Midterm

Each faculty is assigned to exactly one office room. An office room is assigned to at least one faculty. Is the following E-R diagram correct?If your answer is NO, indicate what is wrong in this E-R diagram.

Faculty Office AssignedTo

Answer: Correct!

Page 5: Review for Midterm

A faculty teaches at most three classes, but a class is taught by exactly one faculty. Is the following E-R diagram correct?If your answer is NO, indicate what is wrong in this E-R diagram.

Faculty Classesteaches

Total participation of Faculty w.r.t teaches.Key Constraint on Faculty w.r.t teaches.Partial participation of Classes w.r.t teaches.

Answer: NO

Page 6: Review for Midterm

Each faculty schedules office hours in exactly one office room at a weekday and time, and this information must be recorded. An office may be assigned to several faculties’ office hours at different days and times. All offices are assigned to faculty’s office hours. In the following E-R Diagram, complete the dashed lines to reflect the appropriated participation and key constraints.

Faculty Officescheduled

Time

WeekDay

Page 7: Review for Midterm

Each faculty schedules office hours in exactly one office room at a weekday and time, and this information must be recorded. An office may be assigned to several faculties’ office hours at different days and times. All offices are assigned to faculty’s office hours. In the following E-R Diagram, complete the dashed lines to reflect the appropriated participation and key constraints.

Faculty Officescheduled

Time

WeekDay

Page 8: Review for Midterm

Each faculty schedules office hours in exactly one office room at a weekday and time, and this information must be recorded. An office may be assigned to several faculties’ office hours at different days and times. All offices are assigned to faculty’s office hours. In the following E-R Diagram, complete the dashed lines to reflect the appropriated participation and key constraints.

Faculty Officescheduled

Time

WeekDay

Page 9: Review for Midterm

Given two relations R1 and R2, where R1 contains N1 tuples and R2 contains N2 tuples, with N2>N1>0, give the minimum and maximum possible sizes (in tuples) for the resulting relation produced by each of the following relational algebra expressions. In each case, state any assumptions about the schemas for R1 and R2 needed to make the expression meaningful.

Problem 4.2Problem 4.2

Page 10: Review for Midterm

Expression

Assumption Min Max

21 RR 21 RR

21 RR 21 RR 2*1 NN 2*1 NN)1(5 Ra

)1(Ra2/1 RR1/2 RR 1/2 NN

compatible-unionare and 21 RR

compatible-unionare and 21 RR

compatible-unionare and 21 RR

2N

1N

1N

1N

1N

21 NN

0

00

0

0

01

aR namedattribute an has 1

01,1 NaR namedattribute an has

12

RR

of attributes of setthe of subseta is of attributes of setThe

21

RR

of attributes of setthe of subseta is of attributes of setThe

none

210,22,11 NNNRNR

Page 11: Review for Midterm

Suppose R1 and R2 are multisets of tuples (rows);N1 = number of copies of a tuple t in R1;N2 = number of copies of a tuple t in R2.

What’s the number of copies of t in

R1 UNION ALL R2 ?

Answer: N1+N2

Page 12: Review for Midterm

Suppose R1 and R2 are multisets of tuples (rows);N1 = number of copies of a tuple t in R1;N2 = number of copies of a tuple t in R2.

What’s the number of copies of t in

R1 INTERSECT ALL R2 ?

Answer: min{N1,N2}

Page 13: Review for Midterm

Suppose R1 and R2 are multisets of tuples (rows);N1 = number of copies of a tuple t in R1;N2 = number of copies of a tuple t in R2.

What’s the number of copies of t in

R1 EXCEPT ALL R2 ?

Answer: otherwise

if 0

2121 NNNN

Page 14: Review for Midterm

Consider the following schema:Suppliers(sid:integer, sname:string, address:string)

Parts(pid:integer, pname:string, color:string)

Catalog(sid:integer, pid:integer, cost:real)

State what the following queries compute.

Problem 4.4Problem 4.4

The Catalog relation lists the prices charged for Parts by Suppliers.

Page 15: Review for Midterm

Suppliers(sid:integer, sname:string, address:string)

Parts(pid:integer, pname:string, color:string)

Catalog(sid:integer, pid:integer, cost:real)

Answer: Find the Supplier names of the suppliers who supply a red part that costs less then 100 dollars.

Page 16: Review for Midterm

Suppliers(sid:integer, sname:string, address:string)

Parts(pid:integer, pname:string, color:string)

Catalog(sid:integer, pid:integer, cost:real)

Answer: This Relational Algebra statement does not return anything because of the sequence of projection operators. Once the sid is projected, it is the only field in the set. Therefore, projecting on sname will not return anything.

Page 17: Review for Midterm

Suppliers(sid:integer, sname:string, address:string)

Parts(pid:integer, pname:string, color:string)

Catalog(sid:integer, pid:integer, cost:real)

Answer: Find the Supplier names of the suppliers who supply a red part that costs less than 100 dollars and a green part that costs less than 100 dollars.

Page 18: Review for Midterm

Suppliers(sid:integer, sname:string, address:string)

Parts(pid:integer, pname:string, color:string)

Catalog(sid:integer, pid:integer, cost:real)

Answer: Find the Supplier ids of the suppliers who supply a red part that costs less than 100 dollars and a green part that costs less than 100 dollars.

Page 19: Review for Midterm

Suppliers(sid:integer, sname:string, address:string)

Parts(pid:integer, pname:string, color:string)

Catalog(sid:integer, pid:integer, cost:real)

Answer: Find the Supplier names of the suppliers who supply a red part that costs less than 100 dollars and a green part that costs less than 100 dollars.

Page 20: Review for Midterm

Department (D-code, D-Name, Chair-SSn) Faculty (Ssn, F-Name, D-Code, Rank)

Course (D-code, C-no, Title, Units) Student (Ssn, S-Name, Major, Status)

Prereq (D-code, C-no, P-code, P-no) Enrollment (Class-no, Student-Ssn)

Class (Class-no, D-code, C-no, Instructor-SSn) Transcript (Student-Ssn, D-Code, C-no, Grade)

select F.SSn from Class Co, Faculty Fwhere F.Ssn = Co.Instructor- SSnand not exists(select * from Class C, Faculty F1 where Co.D-code = C.D-code and Co.C-no = C.C-no and C.Instructor-SSn = F1.Ssn and F1.D-code != F.D-Code);

(1) Find the ssn of faculty who are currently teaching courses taught by faculty of several departments.(2) Find the ssn of faculty who are not currently teaching any courses of his/her department.(3) Find the ssn of faculty who are currently teaching courses taught only by faculties of his/her department.(4) Find the ssn of faculty who are currently teaching some course of his/her department.(5) Find the ssn of faculty who are currently teaching exactly one course.

Page 21: Review for Midterm

Flight(flno:integer, from:string, to:string, distance: integer, departs:time, arrives:time)

Aircraft(aid:integer, aname:string, cruisingrange:integer)Certified(eid:integer, aid:integer)Employees(eid:integer, ename:string, salary:integer)

Employees relation describes pilots and other kinds of employees as well; Every pilot is certified for some aircraft (otherwise, he/she would not qualify as a pilot);Only pilots are certified to fly.

Write the following queries in relational algebra and SQL.

Page 22: Review for Midterm

Flight(flno:integer, from:string, to:string, distance: integer, departs:time, arrives:time)

Aircraft(aid:integer, aname:string, cruisingrange:integer)Certified(eid:integer, aid:integer)Employees(eid:integer, ename:string, salary:integer)

Find the aids of all aircrafts that can be used on non-stop flights from Bonn to Madrid.

idBonnToMadrAircraft

FlightidBonnToMadr

cedisgrangecruiaid

MadridtoBonnfromcedis

tansin

''''tan,

SELECT A.aidFROM Aircraft A, Flight FWHERE F.from=‘Bonn’ AND F.to=‘Madrid’, AND

A.cruisingrange >=F.distance

Page 23: Review for Midterm

Flight(flno:integer, from:string, to:string, distance: integer, departs:time, arrives:time)

Aircraft(aid:integer, aname:string, cruisingrange:integer)Certified(eid:integer, aid:integer)Employees(eid:integer, ename:string, salary:integer)

Identify the flights (flno) that can be piloted by each pilot whose salary is more than $100,000.

FlightAircraftTemp

CertifiedEmployeesTemp

grangecruicedisof

salaryaid

sintanln

000,100,

SELECT F.flnoFROM Aircraft A, Certified C, Employees E, Flight FWHERE E.eid=C.eid AND C.aid=A.aid AND F.distance<=A.cruisingrange AND E.salary>100,000

Page 24: Review for Midterm

Flight(flno:integer, from:string, to:string, distance: integer, departs:time, arrives:time)

Aircraft(aid:integer, aname:string, cruisingrange:integer)Certified(eid:integer, aid:integer)Employees(eid:integer, ename:string, salary:integer)

Find the names of pilots who can operate planes with a range greater than 3,000 miles but are not certified on any Boeing aircraft.

EmployeesTempTemp

AircraftCertifiedTemp

AircraftCertifiedTemp

ename

Boeinganameeid

grangecruieid

21

,2

,1

''

000,3sin

Page 25: Review for Midterm

SELECT E.enameFROM Employees EWHERE E.eid IN ((SELECT C1.eid FROM Certified C1, Aircraft A1 WHERE A1.aid=C1.aid AND A1.cruisingrange>3000) EXCEPT (SELECT C2.eid FROM Certified C2, Aircraft A2 WHERE A2.aid=C2.aid AND A2.aname=‘Boeing’))

Find the names of pilots who can operate planes with a range greater than 3,000 miles but are not certified on any Boeing aircraft. Another solution using EXCEPT:

Page 26: Review for Midterm

Flight(flno:integer, from:string, to:string, distance: integer, departs:time, arrives:time)

Aircraft(aid:integer, aname:string, cruisingrange:integer)Certified(eid:integer, aid:integer)Employees(eid:integer, ename:string, salary:integer)

Find the eids of employees who make the highest salary (RA only).The approach to take is: first find all employees who do not have the highest salary. Subtract these from the original list of employees and what is left is the highest paid employees.

31

21,4,3,2,1

.2.1

EE

EEeidEEmployeesEEmployeesE

eid

salaryEsalaryEeid

Page 27: Review for Midterm

Flight(flno:integer, from:string, to:string, distance: integer, departs:time, arrives:time)

Aircraft(aid:integer, aname:string, cruisingrange:integer)Certified(eid:integer, aid:integer)Employees(eid:integer, ename:string, salary:integer)

Find the eids of employees who make the second highest salary (RA only).The approach is similar to the previous one. First find all employees who do not have the highest salary. Remove these from the original list of employees and what is left is the highest paid employees. Remove the highest paid employees from the original list. What is left is the second highest paid employees together with the rest of the employees. Then find the highest paid employees of this new list. This is the list of the second highest paid employees.

Page 28: Review for Midterm

Flight(flno:integer, from:string, to:string, distance: integer, departs:time, arrives:time)

Aircraft(aid:integer, aname:string, cruisingrange:integer)Certified(eid:integer, aid:integer)Employees(eid:integer, ename:string, salary:integer)

Find the eids of employees who make the second highest salary (RA only).

63

54,4,632,532,4

21,4,3,2,1

.5.4

.2.1

EE

EEeidEEEEEEE

EEeidEEmployeesEEmployeesE

salaryEsalaryEeid

salaryEsalaryEeid

Page 29: Review for Midterm

Sample Midterm (with solutions);Solutions of HW1, HW2, HW3;You should:

Review the lectures’ slides and hws;Do the readings in the textbook;Do exercises at the end of chapters.

Material for you to get ready for Midterm…