Ass1 - McGill, University of

advertisement

School of Computer Science, McGill University

COMP-421B Database Systems, Winter 2008

Written Assignment 1: Data Modeling / Relational Algebra

Due: Jan 30, 2008

Question 1.

[18 marks]

A prominent chain of pharmacies would like you to design a database to represent all the data that they need to store for their operations. The following are the requirements specified by the chain:

Patients are identified by a SIN; and their names, addresses and ages must be recorded.

Doctors are identified by a SIN. 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 amongst the products of that company. If a pharmaceutical company is deleted, we need not keep track of its products any longer.

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

Every patient has a primary physician. Every doctor has at least one patient, and possibly several.

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. At all times, there must be a supervisor for each contract, but the contract supervisor can change over the lifetime of the contract.

With this information, do the following initial steps in your database design process:

1.

Develop an ER diagram to represent the conceptual database scheme for the above “enterprise”.

2.

In the diagram, mark the various constraints (keys, cardinalities of the relationships, etc.). Identify any constraints that are not captured by the ER diagram.

3.

Convert your ER diagram into a relational database scheme. Make refinements to your scheme if possible. Identify the primary keys and the foreign keys in the relational schemes, and hence note the referential integrity constraints in the scheme.

4.

How would your design change if each drug is required to be sold at a fixed price by all pharmacies?

Question 2.

[16 marks]

Consider a database scheme consisting of the following relation schemes:

EMPLOYEE (eid, ename, age, salary)

DEPARTMENT(did, budget, manager_id)

WORKS (eid, did, hours_per_week)

The semantics of the scheme should be clear from the identifiers used. The key attributes are underlined. Department.manager_id is a subset of employee.eid. An employee may manage more than one department.

1.

Give an example of a foreign key constraint that involves the DEPARTMENT relation. What are the options for enforcing this constraint when a user attempts to delete a DEPARTMENT tuple?

2.

Now, express the following queries in Relational Algebra: a.

Get the id’s and names of employees who have a salary of more than 50 k. b.

Get details on employees who work in more than one department. c.

Who are the managers who work also in a department that they do not manage. d.

Get the id’s of employees who work in department 203 and also in department 302. e.

Find those employees who do not work in the department managed by

John Doe. f.

Find details on the employee(s) who work(s) the maximum number of hours per week in department 101.

Question 3.

[6 marks]

Consider a DB schema consisting of the following relation schemes:

Patient (pid, pname, age, city)

Doctor (did, dname, specialization, city)

Clinic (cid, cname, city)

Works_in (did, cid, hours_per_week)

Consults (pid, did, cid, date, illness)

Now, express the following queries in Relational Algebra:

1.

Get details on patients who have consulted Dr. Lee at least once.

2.

List those doctors’id who do not work in Victoria (clinic name) for all clinics of

Montreal.

3.

Find those patients’name who have never been to a clinic in a city other than the one he/she lives in.

Download