hw2 Key
hw2 Key
hw2 Key
Fall 2005
Key to Assignment 2 – Relational Languages: SQL and
Functional Dependencies
Due: Tue-Oct 4
Some questions are intended only for students taking cs6530. Unless
explicitly stated in the question, cs5530 should not turn in these questions
– they will not be graded.
Regular Exercises
create table TA (
Sid int,
Cid int,
Year int,
foreign key (Sid) references Student(Sid),
foreign key (Cid) references Course(Cid)
);
The meaning of these tables and their attributes should be obvious. Write the
following queries in SQL: [60 points]
a. List the names of all the people in the university database whose
address contains the string “salt”.
(select name from student where saddr LIKE '%salt%')
UNION ALL
(select name from professor where saddr LIKE '%salt%')
Note here the use of UNION ALL, which will keep duplicated names in
the results.
b. List the names of all professors, and the names of all the courses
they have taught (if any).
select pname,cname
from professor P OUTERJOIN teaches T JOIN course C
where P.pid = T.pid AND T.cid = C.cid
select Pname
from Professor
where Pid not in (select Pid
from Teaches
where year > 2000)
order by Pname desc;
e. Using the GPAs computed in the previous query, update the GPA
column in the Student table.
UPDATE student
SET gpa = (SELECT (sum(grade)/count(grade))
FROM takes
WHERE Student.sid = Takes.sid);
f. List the name of all students whose GPA is different from 3.0.
(this should include students whose GPA is NULL)
g. List the name and address of students whose GPA are the highest
in the database.
Select name,gpa from student
Where gpa = (select max(gpa) from student)
h. List all CS courses and for each course, compute the average score
obtained by the students each time the course was taught. A
sample result could be:
i. List the names of students and the names of the courses they
TA’ed but did not take.
j. [cs6530, and extra credit for 5530] List the names of all students
who have taken all the required CS courses.
(this requires division)
SELECT S.sname
FROM Student S
WHERE NOT EXISTS
((SELECT C.cid
FROM Course C
WHERE C.required = 1)
MINUS
(SELECT T.cid
FROM Takes T
WHERE S.sid=T.sid ));
• You could specify primary keys for TA, Takes and Teaches
• You could create a separate relation that keeps track of course sections:
Assuming that a course is taught only once a year, i.e., the primary key for
CourseSection is (Cid,Year). To ensure the tuples in Takes are valid and
correspond to courses that were taught, you could create a foreign key from
Takes to CourseSection:
And to ensure the tuples in Teaches are valid and correspond to courses that had
actual sections, you could create a foreign key from Teaches to CourseSection:
b. Every faculty member must teach at least two courses every year.
Note that we also need to include professors who did not teach any
course and whose pid is not present in the Teaches table.
c. Only faculty in that do not live in Salt Lake City can teach fewer
than 2 courses per year.
b. Suppose your database system does not allow the definition of foreign
keys. How would you express the foreign keys above using the other
integrity constraints checking mechanisms in SQL?
You would need to use assertions. For example, to enforce the following
foreign key in the Owns relation:
Foreign key (studio_name) references Studios(name)
you could create the following assertion:
Unlike value- and tuple-based constraints, which are only checked when
the table in which they were defined is updated, assertions are checked
when any of the tables they refer to is modified.
c. [cs6530] Write an SQL query that lists all the sequels for the movie
Friday the 13th which was released in 1980.
(this requires a recursive query)
Another alternative:
WITH RECURSIVE AllSequels(Title_orig, Year_orig, Title_seq, Year_seq) AS
salary
year address
title
sequel
length name
original
Owns
Studios
name address
Practice Exercises
Consider the following schema from Assignment 1:
Suppliers(sid: integer, sname: string, address: string)
Parts(pid: integer, pname: string, color: string)
Catalog(sid: integer, pid: integer, cost: real)
The key fields are underlined, and the domain of each field is listed after the field
name. Therefore sid is the key for Suppliers, pid is the key for Parts, and sid and pid
together form the key for Catalog. The Catalog relation lists the prices charged for
Answer:
1. SELECT S.sname
FROM Suppliers S, Parts P, Catalog C
WHERE P.color=’red’ AND C.pid=P.pid AND C.sid=S.sid
2. SELECT C.sid
FROM Catalog C, Parts P
WHERE (P.color = ‘red’ OR P.color = ‘green’)
AND P.pid = C.pid
3. SELECT S.sid
FROM Suppliers S
WHERE S.address = ‘221 Packer street’
OR S.sid IN ( SELECT C.sid
FROM Parts P, Catalog C
WHERE P.color=’red’ AND P.pid = C.pid )
4. SELECT C.sid
FROM Parts P, Catalog C
WHERE P.color = ‘red’ AND P.pid = C.pid
AND EXISTS ( SELECT P2.pid
FROM Parts P2, Catalog C2
WHERE P2.color = ‘green’ AND C2.sid = C.sid
AND P2.pid = C2.pid )
5. SELECT C.sid
FROM Catalog C
WHERE NOT EXISTS (SELECT P.pid
FROM Parts P
WHERE NOT EXISTS (SELECT C1.sid
FROM Catalog C1
6. SELECT C.sid
FROM Catalog C
WHERE NOT EXISTS (SELECT P.pid
FROM Parts P
WHERE P.color = ‘red’
AND (NOT EXISTS (SELECT C1.sid
FROM Catalog C1
WHERE C1.sid = C.sid AND
C1.pid = P.pid)))
8. SELECT C.pid
FROM Catalog C
WHERE EXISTS (SELECT C1.sid
FROM Catalog C1
WHERE C1.pid = C.pid AND C1.sid <> C.sid )
9. SELECT C.pid
FROM Catalog C, Suppliers S
WHERE S.sname = ‘Yosemite Sham’ AND C.sid = S.sid
AND C.cost ≥ ALL (Select C2.cost
FROM Catalog C2, Suppliers S2
WHERE S2.sname = ‘Yosemite Sham’
AND C2.sid = S2.sid)