Assignment 1: ER Model and Relational Model (Chapter 2, 3)
Assignment 1: ER Model and Relational Model (Chapter 2, 3)
Solutions
(註:在畫ER diagram中,有些問題問的比較模糊,故可做一些假設,不同的假設可能得出來的
答案不同,以下只供參考)
Chapter 2
<1> (結合<4>的答案)
Each musician that records at Notown has an SSN, a name, an address, and a phone
number. Poorly paid musicians often share the same address, and no address has more
than one phone.
live
musician address
phone
Each instrument used in songs recorded at Notown has a name (e.g., guitar, synthesizer,
flute) and a musical key (e.g., C, B-flat, E-flat).
instrumentId
musical_key
instrument
name
題目中沒有說明instrument的primary key是什麼,因此為了要確定每個instrument
的唯一性,在此假設每個instrument都一個唯一的instrumentId
Each album recorded on the Notown label has a title, a copyright date, a format (e.g.,
CD or MC), and an album identifier.
copyrightdate
title
format
album
identifier
title
song
author
songId
跟上面的instrument一樣,在此假設每個song都有唯一的songId
CREATE TABLE song{
songId INTEGER,
title CHAR(20),
author CHAR(20),
PRIMARY KEY(songId)
}
Each musician may play several instruments, and a given instrument may be played by
several musicians.
Each album has a number of songs on it, but no song may appear on more than one
album.
appear song
album
Each song is performed by one or more musicians, and a musician may perform a
number of songs.
perform
song musician
無法用基本的SQL表示song至少出現一次在”perform “relationship
Each album has exactly one musician who acts as its producer. A musician may produce
several albums, of course.
musician produce
album
請看上面的album
把上面所有圖連起來:
musician live
address
produce copyrightdate
format
play
album
identifier
perform title
appear
song title
musical_key
author
instrument songId
name
instrumentId
<2> 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.
SSN
patient
name
age
address
Doctors are identified by an SSN. For each doctor, the name, specialty, and years of
experience must be recorded.
SSN
doctor
specialty
years_of_e
xperience
name
Each pharmaceutical company is identified by name and has a phone number.
company name
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.
trade_name
make company
drug
formula
這裡要假設不同的company會製造出具有相同trade_name的drug,則trade_name會變
成一個partial key,如果是假設所有的公司製造出的drug的trade_name都完全不同,則
trade_name就可以變成是drug中的primary key。
pharmacyId
name
pharmacy
address
phone_number
Every patient has a primary physician. Every doctor has at least one patient.
primary_physicain
patient
doctor
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.
pharmacy sell drug
price
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.
quantity
date
end_date
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.
supervisor
1. Draw an ER diagram that captures the preceding information. Identify any constraints
not captured by the ER diagram.
把上面的圖全部連起來:
quantity
date
specialty prescribe
SSN
SSN
address
name
years_of_experience
age
formula
trade_name
make
price drug
company
sell
start_date
text
name
name
phone_number
pharmacy contract
supervisor
end_date
pharmacyId
address
phone_number
2. How would your design change if each drug must be sold at a fixed price by all
pharmacies?
price會變成drug的attribute,即
price
3. How would your design change if the design requirements change as follows: If a doctor
prescribes the same drug for the same patient more than once, several such prescriptions
may have to be stored
改變如下:
id
prescriptions date
quantity
patient
prescribe doctor
Chapter 3
<3> Answer each of the following questions briefly. The questions are based on the
following relational schema:
Emp( eid: integer, ename: string, age: integer, salary: real)
Works( eid: integer, did: integer, pct_time: integer)
Dept(did: integer, dname: string, budget: real, managerid: integer)
1. Give an example of a foreign key constraint that involves the Dept relation. What are
the options for enforcing this constraint when a user attempts to delete a Dept tuple?
Eg. CREATE TABLE Works{
eid INTEGER,
did INTEGER,
pct_time INTEGER,
PRIMARY KEY(eid,did),
FOREIGN KEY(eid) REFERENCES Emp(eid)
FOREIGN KEY(did) REFERENCES Dept(did)
}
Four options : NO ACTION, CASCADE, SET DEFAULT , SET NULL
2. Write the SQL statements required to create the preceding relations, including
appropriate versions of all primary and foreign key integrity constraints.
CREATE TABLE Emp (
eid INTEGER,
ename CHAR(50),
age INTEGER,
salary REAL,
PRIMARY KEY(eid) )
CREATE TABLE Works(
eid INTEGER,
did INTEGER,
pct_time INTEGER,
PRIMARY KEY(eid,did),
FOREIGN KEY (eid) REFERENCES Emp(eid)
ON DELETE CASCADE,
FOREIGN KEY (did) REFERENCES Dept(did)
ON DELETE CASCADE
)
CREATE TABLE Dept(
did INTEGER,
dname CHAR(20),
budget REAL,
managerid INTEGER,
PRIMARY KEY(did),
FOREIGN KEY(managerid) REFERENCES Emp(eid)
ON DELETE SET NULL
)
3. Define the Dept relation in SQL so that every department is guaranteed to have a
manager.
4. Write an SQL statement to add John Doe as an employee with eid = 101, age = 32 and
salary = 15,000.
INSERT
INTO Emp
VALUES (101,’John Doe’,32,15000)
<4> 請看<1>