COS 597A, Fall 2008 - Problem Set 3
Due at 5:00pm, Friday October 17, 2008
You may submit electronically
or by hardcopy: bring your hardcopy submission to my office or put it
in my mailbox in the CS mailroom on the 2nd floor.
Collaboration Policy
You may discuss problems with other students in the class. However,
each
student must write up his or her own solution to each problem
independently.
That is, while you may formulate the solutions to problems in
collaboration
with classmates, you must be able to articulate the solutions on your
own.
Late Penalties
- 20% of the earned score if submitted by 5pm on Monday 10/13/08.
- 40% of the earned score if submitted by 5pm on Wednesday
10/15/08.
- No credit if submitted
later than the 40% penalty deadline.
Problem 1. (left as an exercise in class 10/6/08)
Consider again Board Example 4 for the relational calculus (see slides
posted for 10/1):
students: (SS#,
name, PUaddr, homeAddr, classYr)
employees:
(SS#, name, addr, startYr)
assignment:
(position, division, SS#,
managerSS$ )
division foreign key referencing PUdivision
SS# foreign key referencing employees
managerSS# foreign key referencing employees
study:
(SS#, academic_dept., adviser)
SS# foreign key referencing students
PUdivision:
(division_name, address,
director)
The
problem was to express the query "Find academic departments that
have students working in all divisions" in the relational calculus.
Our board solution was:
{ T | ∃M ε study (
T[academic_dept]=M[academic_dept]
)
∧ ∀D
ε PUdivision ∃A
ε assignment ∃M2 ε study (
A[division]= D[division_name] ∧ A[SS#]=M2[SS#]
∧ M2[academic_dept] = T[academic_dept] )
Question: If
PUdivision is empty, what is the
result of this relational calculus query? Be precise.
Problem 2.
In slide # 13 of the SQL
presentation (posted for 10/6/08), we considered the SQL query "Find names of all branches with accts of
cust. who live in Rome":
SELECT
A.bname
FROM Acct A
WHERE A.acctn IN (SELECT D.acctn
FROM Owner D, Cust C
WHERE D.name = C.name AND C.city=‘Rome’)
Call the result of this query
Result_in_Rome.
Now consider query:
SELECT
A.bname
FROM Acct A
WHERE A.acctn NOT IN (SELECT D.acctn
WHERE D.name = C.name AND C.city=‘Rome’)
Call the result of this query
Result_not_in_Rome.
Is
Result_not_in_Rome =
πbname(A
) - Result_in_Rome
? Justify your answer.
Problem 3.
Consider again the SQL example in slide
#28 of the SQL presentation (posted for 10/6/08). The English
description of the query that we came up with in class on 10/8 is
wrong. See the correction
slides (pdf). What is the correct English
description of what the query produces?
Problem 4.
The Department of Computer Science maintains a
database
of technical reports using the software MySQL. Normally, this
is
accessed from the department's technical
reports server. However, the technical staff have prepared a SQL
Web interface so that you may play with a real database (or rather
a snapshot of it). This database
is somewhat different from the examples we have been using in that it
has
much more text and little numerical data. There are only two tables,
defined
as:
TABLE main (
id varchar(10),
entry date,
org varchar(75),
language varchar(20),
title tinytext,
date varchar(30),
pages varchar(15),
abstract text,
ps enum('Y','N'),
pdf enum('Y','N'),
PRIMARY KEY (id),
UNIQUE id (id)
);
TABLE authors (
id char(10),
author char(50),
ord tinyint(3) unsigned
);
The different types are extensions of the basic types and are described
in the Language Reference linked from the SQL Web interface.
However,
all you really need to know is that tinyint(3) unsigned is a
type
of integer, date is a
specific format of string that allows for meaningful comparison of
dates, and that varchar, char, tinytext
and
text
are all types of strings so that one can use the SQL operator
"LIKE". Note that attribute entry records
the date the technical report (TR) was entered in the database
and attribute date
records when the writing of the TR was completed.
Part 0 (warm-up, don't turn in) To get an understanding of
what
the entries in main and authors look like, go to the
SQL
Web interface and enter and submit these four SQL queries, one at a
time:
select *
from main M
where M.date='April 2006'
select M.entry, M.date
from main M
where M.entry < '1994-05-01'
select *
from authors A
where A.id='TR-595-99'
select *
from authors A
where A.author LIKE 'Walker%'
Part 1. Write and submit SQL queries to find the following.
Hand
in a print-out of the results of each query. Note that for long
queries, the print-out of the SQL Web interface page with
the result will cut off the query, so you need to type it on a
separate page.
Query a: Find the names of all authors of TR's dated
in the
summer
of 2002 (June, July and August).
Query b: Find the ids, titles, and dates of the TRs with the
word "security" in their abstracts.
Query c: Find the names of all co-authors of
Professor Appel
in
technical reports dated sometime in 2001.
Query d: For those authors who
have published more than 1 TR
in 2004, find their names and
the number of TRs they published in
2004.
Query
e: Find the entry
date(s) when the most TRs were entered and the number of TRs entered on that entry date.
"Entry date" refers to the entry attribute,
not the date attribute.
Part 2. Why do you think the database was organized the way
it
was: two tables with the data recorded as indicated in the table
definitions?
Would you organize it differently? Be sure to consider the use of this
database as represented by the technical report server.
Problem 5.
One database system to which you have
access
is the Princeton University Instructional Oracle Facility. Go to the Princeton
University Instructional Oracle Facility Web site and follow the
directions
to establish an account. Then go to the Instructional
Oracle SQL Editor. You will use the Web interface provided there to
define tables and insert and delete values.
Important: The local copies of Oracle 8
documentation linked at the bottom of the Princeton
University Instructional Oracle Facility Web site no lonter
exist. Furthermore, the current version being run by the facility
is Oracle 9.2 Documentation can be obtained from Oracle: Oracle9i
SQL Reference: Table of Contents. To turn in your work, use
the
browser print command to print your sequence of SQL commands and the
results.
Note that the full sequence of commands must be executed at one time
(one
transaction) in the editor window to appear as a sequence in the bottom
frame, where the results also appear. Also, you must have the bottom
frame
selected to print both the command sequence and the results. If
you wish to submit your work by email, cut and paste the contents of
the bottom frame to a text file and email that. Be
sure to save your tables in your Oracle database until your work
has been graded.
For this problem we will use the following database for a consulting
firm:
- relation consultant with
attributes (name,
street,
city)
- relation client with
attributes (c_name,
street, city)
- relation works with attributes (name, c_name, fee_rate)
- name is a foreign key referencing consultant
- c_name is a foreign key referencing client
- relation supervised_by with
attributes (name, c_name,
supervisor_name)
- (name, c_name) is a foreign key referencing works
- supervisor_name is a foreign key referencing consultant
Underlined attributes constitute the primary key for a relation -- our
usual convention.
Part 1 Define tables corresponding to the 4 relations in your
Oracle database. Include definitions of PRIMARY KEY constraints and
FOREIGN
KEY constraints.
Part 2 Add two consultants with at least two clients each and
two consultants with no clients; at least one consultant must have a
supervisor for each client with which he/she is paired. Of course, you
can use minimalist names
so that you don't need to do a lot of typing.
Part 3 Show that the Oracle facility enforces FOREIGN
KEY
constraints by executing a sequence of inserts and deletes for two of
the
relations (your choice of which) to illustrate enforcement. Show (i)
that
a tuple cannot be deleted in the referenced table if the primary key
value
of that tuple is present as the foreign key value of a tuple in the
referencing
table, (ii) that the primary key value of a tuple cannot be changed in
the referenced table if that primary key value is present as the
foreign key value of a tuple in the referencing table, and (iii) that a
tuple cannot be added in the referencing table if the foreign key value
in that tuple is not present as a primary key value in a tuple of the
referenced
table.
WARNING: You must follow each INSERT or DELETE or UPDATE
command
with a COMMIT command on a separate line for the ORACLE database system
to permanently store or delete the values. For example:
insert into works (name, c_name, fee_rate) values ('smith', 'jones', 100);
commit;
See Item 3 under Guidelines on Specifying SQL Commands To SQL Editor
in the SQL
Editor Tutorial.
NOTE: If you have a Computer Science Department account,
then you also have access to a MySQL database
server that can be used for course or research projects or
for further
exploration of database systems. See CS
Guide: Database. If you have a CS
account,
you can get a MySQL account by filling out the form Database
Setup on CS Guide
list of Request Forms.
WARNING: CS Guide pages are restricted to local (i.e.
cs.princeton.edu)
access. If you do not have a CS account but would like to
work
with the MySQL database server, please email Professor LaPaugh.