Classroom Exercise 1. The Prescriptions-R

advertisement

Classroom Exercise

1. The Prescriptions-R-X chain of pharmacies has offered to give you a free lifetime supply of medicine if you design its database. Given the rising cost of health care, you agree. Here’s the information that you gather:

Patients are identified by an SSN, and their names, addresses, and ages must be recorded.

Doctors are identified by an SSN. For each doctor, the name, specialty, and years of experience must be recorded.

Each pharmaceutical company is identified by name and has a phone number.

For each drug, the trade name and formula must be recorded. Each drug is sold by a given pharmaceutical company, and the trade name identifies a drug uniquely from among the products of that company. If a pharmaceutical company is deleted, you need not keep track of its products any longer.

Each pharmacy has a name, address, and phone number.

Every patient has a primary physician. Every doctor has at least one patient.

Each pharmacy sells several drugs and has a price for each. A drug could be sold at several pharmacies, and the price could vary from one pharmacy to another.

Doctors prescribe drugs for patients. A doctor could prescribe one or more drugs for several patients, and a patient could obtain prescriptions from several doctors. Each prescription has a date and a quantity associated with it. You can assume that, if a doctor prescribes the same drug for the same patient more than once, only the last such prescription needs to be stored.

Pharmaceutical companies have long-term contracts with pharmacies. A pharmaceutical company can contract with several pharmacies, and a pharmacy can contract with several pharmaceutical companies. For each contract, you have to store a start date, an end date, and

 the text of the contract.

Pharmacies appoint a supervisor for each contract. There must always be a supervisor for each contract, but the contract supervisor can change over the lifetime of the contract. a.

Come up with the Schema diagram for the above problem. b.

Write SQL statements to create the relations including appropriate versions of all primary and foreign key integrity constraints as identified in the schema diagram in part a.

2. The following relations keep track of airline flight information:

Flights(flno: integer (PK), from: string, to: string, distance: integer, departs: time, arrives: time, price: real)

Aircraft(aid: integer (PK), aname: string, cruisingrange: integer)

Certified(eid: integer , aid: integer (PK))

Employees(eid: integer (PK), ename: string, salary: integer)

Note that the Employees relation describes pilots and other kinds of employees as well; every pilot is certified for some aircraft, and only pilots are certified to fly. Write each of the following queries in SQL a) Find the names of aircraft such that all pilots certified to operate them have salaries more than

$80,000. b) For each pilot who is certified for more than three aircraft, find the eid and the maximum cruisingrange of the aircraft for which she or he is certified. c) Find the names of pilots whose salary is less than the price of the cheapest route from Los

Angeles to Honolulu. d) For all aircraft with cruisingrange over 1000 miles, find the name of the aircraft and the average salary of all pilots certified for this aircraft. e) Find the names of pilots certified for some Boeing aircraft. f) Find the aids of all aircraft that can be used on routes from Los Angeles to Chicago. g) Identify the routes that can be piloted by every pilot who makes more than $100,000. h) Print the enames of pilots who can operate planes with cruising range greater than 3000 miles but are not certified on any Boeing aircraft. i) A customer wants to travel from Madison to New York with no more than two changes of flight.

List the choice of departure times from Madison if the customer wants to arrive in New York by

6 p.m. j) Compute the difference between the average salary of a pilot and the average salary of all employees (including pilots). k) Print the name and salary of every nonpilot whose salary is more than the average salary for pilots. l) Print the names of employees who are certified only on aircrafts with cruising range longer than

1000 miles. m) Print the names of employees who are certified only on aircrafts with cruising range longer than

1000 miles, but on at least two such aircrafts. n) Print the names of employees who are certified only on aircrafts with cruising range longer than

1000 miles and who are certified on some Boeing aircraft.

Download