CS145 - Autumn 2009
Introduction to Databases
Project Part 2: MySQL Features
Due Monday November 9

Important: Activate CGI Service for Part 3

Before you will be able to begin work on Project Part 3, you will need to activate a personal CGI account on the Stanford Unix machines. Activation can take up to 24 hours -- we strongly urge you to do it now, to avoid any delays when you begin Project Part 3. Visit Activate Personal CGI Service and follow the instructions. Once your CGI account is activated, you will have a new directory ~/cgi-bin/, which you will use for Part 3.

Another Important Reminder

As discussed for Project Part 1, it is critical that your MySQL tables use the "InnoDB" storage engine -- otherwise this part of your project will not work properly. InnoDB is the default storage engine for Windows installations of MySQL, but not for Linux, and therefore not for the Stanford machines. Make sure to include engine=innodb with every create table command.


Overview

This part of the project will introduce you to MySQL procedures, constraints, and triggers. You will also add a "current time" feature to your AuctionBase database.

Part A: Current Time

The original auction data that we provided for you in XML, which you translated into relations and loaded into your AuctionBase database in Project Part 1, represents a single point in time, specifically one second after midnight on December 20th, 2001 ("Dec-20-01 00:00:01").

To fully test your functionality, and to simulate the true operation of an online auction system in which auctions close as time passes, we suggest that you maintain a fictitious "current time" in your database. Add a new one-attribute table to your AuctionBase schema. This table should at all times contain a single row (i.e., a single value) representing the "current time," which can be updated to represent time passing. (It's up to you whether you also want to permit backward time-travel.) Initialize the table by inserting the current time for the initial state of your database: Dec-20-01 00:00:01.

Create a file named time.sql that contains your commands for creating and initializing your current time table and for selecting the current time. Your file should have the following form:

    drop table if exists Time;
    create table Time(currenttime datetime) engine=innodb;
    insert into Time values (str_to_date(...));
    select date_format(...) from Time;
    quit
Some important notes:

Part B: Procedures

Before getting started on this part, please read the help document MySQL Procedures.

For this part, you will be writing a procedure that computes the "bidding closure" for a specified seller. More precisely, given a particular seller as a parameter, you are to construct the set of all bidders who placed a bid on one of the seller's items, or (recursively) on an item being sold by any of the bidders in this set. For example, in computing the recursive bidding closure of seller Jennifer, if Karl placed a bid on one of Jennifer's items, and Haider placed a bid on one of Karl's items, then Karl and Haider would be in the recursive bidding closure of Jennifer. Note that Jennifer is not part of this closure.

Your procedure, called compute_closure, should take as an argument the user id of a seller, and it should insert into a relation called my_closure the user id's of the individuals in the seller's bidding closure.

Create a command file named closure.sql containing the command to create your closure relation, your MySQL procedure, and a command for invoking your procedure with parameter 'westbur'. To facilitate the grading process, your closure.sql file must have the following format:

     drop procedure if exists compute_closure;
     drop table if exists my_closure;
     create table my_closure (...);
     set @@max_sp_recursion_depth=255; # enables recursion
     delimiter | 
     create procedure compute_closure (...)
     begin
        ...
     end |
     delimiter ; 
     call compute_closure(...);
     select count(*) from my_closure;
     drop procedure compute_closure;
     drop table my_closure;
     quit
As in time.sql, there should be no semicolon after quit, but there should be a newline.The drop commands are for cleaning up before and after you run your procedure. You should fill in the details of creating your closure relation, the actual procedure, and calling your procedure. Be sure to test your procedure thoroughly.

Correctness check: When you run your procedure for seller 'westbur' using the large data set, your bidding closure should contain 87 rows.

Part C: Constraints and Triggers

Before getting started on this part, please read the help document MySQL Triggers, and look over the FOREIGN KEY Constraints page in the MySQL Manual; you may also want to refer to PRIMARY KEY and UNIQUE in the CREATE TABLE Syntax manual page. Be aware that MySQL constraints and triggers do not conform to the SQL-99 (SQL2) standard as covered in the textbook and in the more general material presented in class. For example, although MySQL accepts tuple-based CHECK constraints, they are not actually enforced (!). General assertions are not supported in MySQL at all, and trigger functionality is limited.

If the data in your AuctionBase system at a given point in time represents a correct state of the real world, a number of constraints are expected to hold. To get you started, here are a few possible examples, some of which depend on a particular schema:

Here is what you need to do:

Having trouble?

If you run into difficulties, before contacting the course staff please consult the Project Part 2 FAQ Page. We will keep it up-to-date with answers to common questions.

What to submit

Create a submission directory containing all and only the following files:

time.sql Command file for creating and initializing table Time as specified in Part A
closure.sql Command file for compute_closure procedure as specified in Part B
constraints.txt Constraints file as specified in Part C
constraints_add.sql Command file for adding your key and referential-integrity constraints
constraints_good.sql Command file with valid database modifications
constraints_bad.sql Command file with invalid database modifications
constraints_drop.sql Command file for dropping your key and referential-integrity constraints
trigger1_add.sql Command file for creating first constraint-checking triggers
trigger1_drop.sql Command file for dropping first constraint-checking triggers
trigger2_add.sql Command file for creating second constraint-checking triggers
trigger2_drop.sql Command file for dropping second constraint-checking triggers
trigger3_add.sql Command file for creating third constraint-checking triggers
trigger3_drop.sql Command file for dropping third constraint-checking triggers

Again, your submission directory should contain only these listed files. (One exception is that you may submit additional triggerN_add and triggerN_drop command files as described earlier.) As usual, from your submission directory execute the script:

  /usr/class/cs145/bin/submit-project
You may resubmit as many times as you like, however only the latest submission and timestamp are saved, and those are what we will use for grading your work and determining late penalties. Submissions via email will not be accepted.

If you changed Part 1: If you changed any of your tables from what you submitted in Project Part 1, then you will need to resubmit your files from Part 1, so that we can properly test and grade your Part 2. Please make a prominent note in constraints.txt, and include your files from Part 1 along with the files listed above for this part. Do not resubmit your Part 1 files unless the database itself has changed.

Final Note: Points will be deducted if you do not follow the submission procedures exactly as specified, including which files to include (and which not to include), file naming, and file contents. Remember to allow sufficient time to prepare your submission once your work is complete.