27.01.2015 Views

Solutions to Midterm Examination

Solutions to Midterm Examination

Solutions to Midterm Examination

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Prof. Li-Yan Yuan<br />

CMPUT 291: File and Database Management Systems<br />

<strong>Solutions</strong> <strong>to</strong> <strong>Midterm</strong> <strong>Examination</strong><br />

Section B1, February 24, 2004<br />

1. Construct an E-R diagram for a car-insurance company that has a set of cus<strong>to</strong>mers, each of<br />

whom owns one or more cars. Each car has associated with it zero <strong>to</strong> any number of recorded<br />

accidents. The company also maintains the statistical information for each type of vehicle,<br />

such as the accident and car theft ratio.<br />

If you have any doubt about the specification, make a reasonable assumption. Document all<br />

your assumptions. Please use the textbook notations <strong>to</strong> specify the mapping constraints. [18]<br />

2. The following ER diagram characterizes information about an on-line book s<strong>to</strong>re. Cus<strong>to</strong>mer,<br />

S<strong>to</strong>re, Book s<strong>to</strong>re information about cus<strong>to</strong>mers, s<strong>to</strong>res, and books respectively. Order represents<br />

each order placed by a cus<strong>to</strong>mer. Relationship set supply indicates which s<strong>to</strong>re supplies<br />

which book <strong>to</strong> which order; and ordered indicates who placed the order.<br />

Cus<strong>to</strong>mer<br />

S<strong>to</strong>re<br />

✻<br />

❅<br />

ordered<br />

❅<br />

❅❅<br />

❅<br />

✗<br />

Date<br />

✖<br />

✔<br />

✕<br />

❅<br />

supply<br />

❅<br />

❅❅<br />

❅<br />

✲<br />

Order<br />

Book<br />

The attributes for all entity sets are Cus<strong>to</strong>mer(c id, c name, c address), Order(o id, o data),<br />

S<strong>to</strong>re(s id, s address), Book(isbn, title, price), with c id, o id, s id, isbn as respective<br />

keys. Note that the arrow indicates the one side of the corresponding relationship set.<br />

Use the SQL’s CREATE TABLE statement <strong>to</strong> create the database schema from the ER<br />

diagram, specifying all the essential constraints. State any reasonable assumptions you may<br />

have. [12]<br />

CREATE TABLE s<strong>to</strong>re (<br />

s_id int,<br />

s_address VARCHAR(200),<br />

PRIMARY KEY (s_id)<br />

)<br />

1


CREATE TABLE book (<br />

isbn VARCHAR(24),<br />

title VARCHAR(48),<br />

price NUMERIC(10,2),<br />

PRIMARY KEY (isbn)<br />

)<br />

CREATE TABLE cus<strong>to</strong>mer (<br />

c_id int,<br />

c_name VARCHAR(128),<br />

c_address VARCHAR(200),<br />

PRIMARY KEY (c_id)<br />

)<br />

CREATE TABLE order (<br />

o_id int,<br />

o_data VARCHAR(500),<br />

c_id int,<br />

o_date DATE,<br />

PRIMARY KEY (o_id),<br />

FOREIGN KEY c_id REFERENCES cus<strong>to</strong>mer<br />

)<br />

CREATE TABLE supply (<br />

s_id int,<br />

isbn VARCHAR(24),<br />

o_id int,<br />

PRIMARY KEY ( s_id, isbn, o_id),<br />

FOREIGH KEY s_id REFERENCES s<strong>to</strong>re,<br />

FOREIGN KEY isbn REFERENCES book,<br />

FOREIGH KEY o_id REFERENCES order<br />

)<br />

3. Consider a university registration database with the following tables.<br />

student(s id, s name, status, phone, major)<br />

professor(p id, p name, dept)<br />

teach(p id, c id, term)<br />

transcript(s id, c id, term, grade)<br />

The first two tables s<strong>to</strong>re information about students and professors; the third table indicates<br />

who teaches which course at which term; and the last table s<strong>to</strong>res information about course<br />

registration.<br />

Use SQL <strong>to</strong> express the following queries. (You results may contain duplicate tuples.)<br />

2


(a) Find the names (s name) of all students who have taken a course taught by a professor<br />

in the ’CS’ department.<br />

SELECT s_name<br />

FROM student s, transcript t, teach c, professor p<br />

WHERE s.s_id = t.s_id AND t.c_id = c.c_id AND<br />

c.p_id = p.p_id AND p.dept = ’CS’ AND<br />

t.term = c.term<br />

(b) Find the names of all students who have taken more than one course taught by a professor<br />

from the ’CS’ department. You cannot not use aggregate functions with or without the<br />

group by clause for this question.<br />

SELECT s_name<br />

FROM student s, transcript t1, transcript t2, teach c1, teach c2,<br />

professor p1, professor p2<br />

WHERE s.s_id = t1.s_id AND t1.c_id = c1.c_id AND t1.term = c1.term AND<br />

c1.p_id = p1.p_id AND p1.dept = ’CS’ AND<br />

s.s_id = t2.s_id AND t2.c_id = c2.c_id AND t2.term = c2.term AND<br />

c2.p_id = p2.p_id AND p2.dept = ’CS’ AND<br />

(t1.c_id t2.c_id OR t1.term t2.term)<br />

(c) List the names of all students who have taken a course from every professor in the<br />

’MUSIC’ department.<br />

SELECT s_name<br />

FROM student s, transcript t, teach c, professor p<br />

WHERE s.s_id = t.s_id AND t.c_id = c.c_id AND t.term = c.term AND<br />

c.p_id = p.p_id AND p.dept = ’MUSIC’<br />

GROUP BY s_name<br />

HAVING COUNT( DISTINCT p_id) >= ANY ( SELECT count(*)<br />

FROPM professor<br />

WHERE dept = ’MUSIC’<br />

)<br />

SELECT s_name<br />

FROM student s<br />

WHERE NOT EXIST ( SELECT *<br />

FROM professor p<br />

WHERE p.dept = ’MUSIC’ AND<br />

NOT EXIST ( SELECT *<br />

FROM transacript t, teach c<br />

WHERE s.s_id = t.s_id AND<br />

t.c_id = c.c_id AND<br />

p.c_id = c.c_id AND<br />

3


)<br />

)<br />

t.term = c.term<br />

(d) List the names of students whose average grade is higher than that of all junior students.<br />

A junior student is a student whose status is ’junior’.<br />

SELECT s_name<br />

FROM student s, transcript t<br />

WHERE s.s_id = t.s_id<br />

GROUP BY s_name, s.s_id<br />

HAVING AVG(grade) > ALL ( SELECT AVG(grade)<br />

FROM student s2, transacript t2<br />

WHERE s2.s_id = t2.s_id AND s2.status = ’junior’<br />

GROUP BY s2.s_id<br />

)<br />

(e) List the course id and the average grade taught by each professor in the ’CS’ department<br />

in the ’winter03’ term. That is, you are asked <strong>to</strong> list p id, c id, and the average grade<br />

for each course c id taught by p id in ’winter03’ term.<br />

SELECT p.p_id, c.c_id, AVG(t.grade)<br />

FROM teach c, transcript t, professor p<br />

WHERE c.c_id = t.c_id AND c.p_id = p.p_id AND p.dept = ’CS’ AND<br />

c.term = ’winter03’<br />

GROUP BY p.p_id, c.c_id<br />

(f) List p id and p name of all professors who have never taught a course taken by a junior<br />

student.<br />

SELECT p_id, p_name<br />

FROM professor<br />

EXCEPT<br />

SELECT p.p_id, p.p_name<br />

FROM professor p, teach c, transacript t, student s<br />

WHERE p.p_id = c.p_id AND c.c_id = t.c_id AND c.term = t.term AND<br />

t.s_id = s.s_id AND s.status = ’junior’<br />

(g) Assume that a major always coincides with a department name. list, for each department,<br />

the number of professors who are working for the department, the number of<br />

students who major in the department, and the number of courses taught by professors<br />

from the department.<br />

Note that a course taught in two different terms is counted as one, not two, course taught<br />

by the department.<br />

SELECT p.dept, count(distinct p.p_id), count(distinct s.s_id),<br />

count(distinct c.c_id)<br />

4


FROM professor p, teach c, student s<br />

WHERE p.dept = s.major AND p.p_id = c.p_id<br />

GROUP BY p.dept<br />

4. Consider a relational database about a university with the following three relations<br />

teach(Prof, Course)<br />

take(Student, Course, Grade)<br />

advise (Prof, Student)<br />

The first relation indicates the courses a prof teaches; the second tells what courses each<br />

student takes and the corresponding grades; and the last indicates advisers of a student.<br />

Write a relational algebra expression <strong>to</strong> retrieve<br />

(a) all students who do not take a course from their own advisers.<br />

Π Student (take) − Π Student (teach ✶ take ✶ advise)<br />

(b) A <strong>to</strong>ugh course is a course in which no one receives a grade higher than ’6’. List the<br />

names of all students who take only <strong>to</strong>ugh courses. [14]<br />

Π Student (take) − Π Student1 (σ C1=C2∧G2>6 (take[S1, C1, G] × take[S2, C2, G2]))<br />

Or, alternatively,<br />

Π Student (take)−Π take1.Student (σ take1.Course=take2.Course∧take2.Course.grade>6 (take1×take2))<br />

Note that both take1 and take2 are take.<br />

5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!