COS 597A, Fall 2011 - Problem Set 3

Due at 3:00pm, Wednesday  October 19, 2011

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



Problem 1.

Part A: In slide # 13 of  the SQL presentation (posted under 10/10/11), 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.  

We also consider query:
SELECT A.bname
FROM Acct A
WHERE A.acctn NOT 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_not_in_Rome.   It finds "the names of all branches with accts having no owner who lives in Rome."

Is Result_not_in_Rome πbname(Acct ) - Result_in_Rome ?   Justify your answer.

Part B:  Write an SQL query that does not use nested SELECT for Result_in_Rome.



Problem 2.

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 up to about Sept. 2008). 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='May 2008'

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 TRs 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 3.
This problem should be done on your account on the Department of Computer Science user/project MySQL database serverIf you have not yet made an account,  follow the instructions on the user/project database page of the CS Guide.
To get started, read the department's brief primer Getting Started with MySQL.   This will explain how to access your database. 

If you are running a MySQL server on your own machine, you may use that instead.

The CS department user/project server is running MySQL version 5.1.x.   The MySQL 5.1 Reference Manual gives details of this version of SQL.  For the most part you can just use the standard SQL commands discussed in class and in the textbook.   (See below for some exceptions when defining foreign keys.)  However, if you have not used MySQL before, I do recommend you read the introduction and Sections 3.1-3.4 of Chapter 3: Tutorial of the MySQL 5.1 Reference Manual.

This problem illustrates enforcement of foreign key constraints.   To use foreign key constraints in MySQL, we need to know a little about MySQL Storage Engines.   Quoting the Reference Manual " MySQL supports several storage engines that act as handlers for different table types."  Storage engines are defined per table and have different capabilities.  For our purposes, it suffices to know that the default storage engine,  MyISAM, does not support foreign key constraints, and the InnoDB engine does.   InnoDB also supports transactions.   Not every server need support every storage engine, but the CS user/project server does support InnoDB as well as MyISAM and a couple of others.   To use InnoDB, each table needs to be created with the ENGINE = INNODB option.  For example:

CREATE TABLE Owner
(name  CHAR(20),
acctn CHAR(20),
FOREIGN KEY (name) REFERENCES Cust(name),
FOREIGN KEY (acctn) REFERENCES Acct(acctn))
ENGINE = INNODB;

Note that commands in MySQL are followed by a ';' .  Also note that foreign key definitions in MySQL require specification of the referenced attribute as well as the referenced table even if the referenced attribute is the primary key.  See Section 13.6.4.4, “FOREIGN KEY Constraints”  of the MySQL 5.1 Reference Manual for more information.


For this problem we will use the following database for a consulting firm:

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.   At least one FOREIGN KEY constraint needs to be ON DELETE CASCADE and at least one FOREIGN KEY constraint needs to be the default (delete not allowed) for Part 3.
Use MySQL command "DESCRIBE table name" (e.g. DESCRIBE consultant) to document that each table exists.

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.  Use "SELECT * from table name"  to document the contents of each table.

Part 3  Show that the InnoDB engine of MySQL  enforces FOREIGN KEY constraints and that it supports ON DELETE CASCADE.  Execute 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 and ON DELETE CASCADE is not specified, (ii) that a tuple deleted in the referenced table cascades to a tuple deleted in the referencing table if the primary key value of the first tuple is present as the foreign key value of the second tuple and ON DELETE CASCADE is specified for the foreign key constraint, (iii) 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 (iv) 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.


To turn in your work, print a transcript of you MySQL session or cut and paste the important parts of the session into a text file.