0% found this document useful (0 votes)
46 views15 pages

Assignment 1: ER Model and Relational Model (Chapter 2, 3)

Uploaded by

eric carter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views15 pages

Assignment 1: ER Model and Relational Model (Chapter 2, 3)

Uploaded by

eric carter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Database Systems Fall Semester, 2008

Instructors: Winston Hsu , Hao-hua Chu

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.

SSN name address_string

live
musician address

phone

CREATE TABLE musician {


SSN INTEGER,
name CHAR(50),
PRIMARY KEY(SSN)
}
CREATE TABLE address {
phone CHAR(20),
address_string CHAR(100),
PRIMARY KEY(phone)
}
CREATE TABLE live {
SSN INTEGER,
phone CHAR(20),
PRIMARY KEY(SSN,phone),
FOREIGN KEY(SSN) REFERENCES musician(SSN),
FOREIGN KEY(phone) REFERENCES 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

CREATE TABLE instrument{


instrumentId INTEGER,
musical_key CHAR(10),
name CHAR(30),
PRIMARY KEY(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

CREATE TABLE album{


identifier INTEGER,
SSN INTEGER NOT NULL,
title CHAR(20),
copyrightdate TIMESTAMP,
format CHAR(20);
PRIMARY KEY(identifier),
FOREIGN KEY (SSN) REFERENCES musician(SSN)
} (在這裡已經結合了最後的小題的答案)
„ Each song recorded at Notown has a title and an author.

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.

musician play instrument

CREATE TABLE play_instrument{


SSN INTEGER,
instrumentId INTEGER,
PRIMARY KEY(SSN,instrumentId),
FOREIGN KEY (SSN) REFERENCES musician(SSN),
FOREIGN KEY (instrumentId) REFERENCES instrument(instrumentId)
}

„ Each album has a number of songs on it, but no song may appear on more than one
album.

appear song
album

CREATE TABLE appear_song {


songId INTEGER,
identifier INTEGER,
PRIMARY KEY(songId),
FOREIGN KEY(identifier) REFERENCES album(identifier),
FOREIGN KEY(songId) REFERENCES song(songId)
}

„ 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
把上面所有圖連起來:

SSN name address_string


phone

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。

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


假設每個pharmacy都有唯一的pharmcyId

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

patient prescribe doctor

date

„ 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.
text start_date

company contract pharmacy

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.

company contract pharmacy

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

doctor patient name


primary_physicain

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,即

pharmacy sell drug

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.

CREATE TABLE Dept(


did INTEGER,
dname CHAR(20),
budget REAL,
managerid INTEGER NOT NULL,
PRIMARY KEY(did),
FOREIGN KEY(managerid) REFERENCES Emp(eid)
)

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)

5. Write an SQL statement to give every employee a 10 percent raise.


UPDATE Emp E
SET E.salary = E.salary*1.1
6. Write an SQL statement to delete the Toy department. Given the referential integrity
constraints you chose for this schema, explain what happens when this statement is
executed.
DELETE
FROM Dept D
WHERE D.dname = ‘Toy’

<4> 請看<1>

You might also like