CMPS180 - Winter 04

Project Assignment 3

Due midnight at the end of Monday, Feb 23rd

Logistics and General Information


PostgreSQL

Familiarize yourself with the PostgreSQL relational DBMS by reading the document Managing a Database in the Interactive PostgreSQL documentation, logging into PostgreSQL, trying some of the examples in the document, and experimenting with the various commands. You don't need to turn anything in for this part. The PostgreSQL web page has links to documentation and other information. See PostgreSQL: Introduction and Concepts, by Bruce Momjian, which is available in printed form at the bookstore and on reserve at the library. There's also a PostgreSQL At A Glance document describing PostgreSQL's features.

Note: For system maintenance reasons, you are not allowed to create a new database by using the createdb command. A database has already been created for you with the same name as your user-name.

You can access your database with the command:

psql -a
which will start a command line interface to PostgreSQL connected to the database with your username. (The -a option will cause the input to be echoed to the console for saving a script log. If you don't want your input echoed, you can omit the -a, but please do include it when you are running an execution script to hand in.)


The assignment

For this assignment, you will refine your relational schema, insert integrity checks in your database, and create a logical schema for the users of the system.

Part A

Description: This part comprises of the following questions:

  1. Write down all non-trivial Functional Dependencies (FDs) that hold on your schema.
  2. Pick one FD that holds on a specific table, say R. Provide two instances of R, one that violates the FD, and one that satisfies it. Then, write an SQL query which allows you to detect whether the FD holds or not, and run in over the two instances.
    Notes:
  3. Given the FDs of question 1, check whether you schema is in BCNF. If not, then provide a BCNF decomposition of your tables.
  4. Is your new schema in 3NF?

What to turn in: Please see Recording Your Session below for a guide to preparing output to be submitted for this and subsequent project parts. In this and all subsequent project parts, the material you turn in should be clearly formatted and delineated, and should include comments for any aspects that are not crystal clear. Poorly assembled or documented material will not receive full credit, even if it is correct. You also will not receive full credit if you turn in your entire large data files (or large query results in later assignments) when we ask for small samples. Other than comments, truncation, and simple formatting, it is Academic Dishonesty to edit scripts before turning them in.

For this part of the assignment the following files should be turned in electronically using your cats account and the submit program:

README Please give your name, cats account, project part number, date, a list and description of the files you are submitting, and any other information that will be useful for the grader.
answers.txt A text file containing the answers to questions 1-4.
  • For question 1, you should list each functional dependency in a separate line, using the notation R: X-->Y, where R is the table name, and X and Y are attribute sets from R.
  • For question 2, indicate which functional dependency you will examine, then list the checking SQL query and explain how its output indicates whether the FD is violated or not.
  • For question 3, if your schema is not in BCNF, then list the violating functional dependencies.
  • For question 4, if your schema is not in 3NF, then list the violating functional dependencies.
fd_data_ok.script The COPY command which loads the instance of R that does not violate the FD.
fd_data_violate.script The COPY command which loads the instance of R that violates the FD.
fd_query.script The script file that contains the query to check for the FD of question 1. Make sure to explain how the query works in the answers.txt file.
fd_query_violate.log The log file showing the execution of the checking query on the instance that violates the FD.
fd_query_ok.log The log file showing the execution of the checking query on the instance that satisfies the FD.
bcnf_schema.script The script file with the CREATE TABLE commands for the BCNF schema. If your schema is already in BCNF, then just insert the commands to create the original schema.

To submit these files use the syntax:

submit cmps180-np.w04 proj3-part1 README answers.txt fd_data_ok.script fd_data_violate.script fd_query.script fd_query_violate.log fd_query_ok.log bcnf_schema.script

or proj3-part1-late if your project part is late.


Part B

Description: In this part, you will need to insert constraints in your schema that will check for the integrity of your data. Before tackling this part, it will be useful to look at the PostgreSQL documentation regarding constraints.

Read this before attempting to define any constraints in PostgreSQL.

What to turn in: Please see Recording Your Session below for a guide to preparing output to be submitted for this and subsequent project parts. In this and all subsequent project parts, the material you turn in should be clearly formatted and delineated, and should include comments for any aspects that are not crystal clear. Poorly assembled or documented material will not receive full credit, even if it is correct. You also will not receive full credit if you turn in your entire large data files (or large query results in later assignments) when we ask for small samples. Other than comments, truncation, and simple formatting, it is Academic Dishonesty to edit scripts before turning them in.

For this part of the assignment the following files should be turned in electronically using your cats account and the submit program:

README Please give your name, cats account, project part number, date, a list and description of the files you are submitting, and any other information that will be useful for the grader.
constraint_tables.script The script file with the updated CREATE TABLE commands for your schema. These commands should include the CHECK clauses that implement the constraints.
constraint_data.script The script file with the COPY commands to populate your database. You can include the same test data as in Project Assignment 2, as long as they do not violate the constraints.
constraint_tables.log An execution log showing that the execution of constraint_tables.script and constraint_data.script.
constraint_violates.script The script file with the INSERT/UPDATE statements that violate the constraints.
constraint_violates.log An execution log of the constraint_violates.script script.

To submit these files use the syntax:

submit cmps180-np.w04 proj3-part2 README constraint_tables.script constraint_tables.log constraint_data.script constraint_violates.script constraint_violates.log

or proj3-part2-late if your project part is late.


Part C

Description: For this part of the assignment, you will create a logical schema for a "special" group of users.

During your 120th meeting with El Cheapo executives, you are told that the system should support two different groups of users: on-line customers, who will browse the music store and buy songs, and market researchers, who will perform an analysis of the customers' buying trends. Since the second group of users are not SQL savvy, the executives would like to create a simplified logical schema for them, so that they can write simple SQL queries to get their answers. More specifically, an analyst needs to ask for the following information:

Your task is to define a logical schema (i.e., a set of views) which will make this information available to the analysts. You also need to show to the executives how the previous questions can be answered by SQL queries over the logical schema. Keep in mind that an analyst does not know what a join is, and the most complex SQL query that he/she can write can only involve selection predicates! In order to make the demonstration more believable, you should alter your test data so that each view contains at least five tuples in total.

What to turn in: Please see Recording Your Session below for a guide to preparing output to be submitted for this and subsequent project parts. In this and all subsequent project parts, the material you turn in should be clearly formatted and delineated, and should include comments for any aspects that are not crystal clear. Poorly assembled or documented material will not receive full credit, even if it is correct. You also will not receive full credit if you turn in your entire large data files (or large query results in later assignments) when we ask for small samples. Other than comments, truncation, and simple formatting, it is Academic Dishonesty to edit scripts before turning them in.

For this part of the assignment the following files should be turned in electronically using your cats account and the submit program:

README Please give your name, cats account, project part number, date, a list and description of the files you are submitting, and any other information that will be useful for the grader.
logical_data.script A script file with the necessary COPY commands, which will load the test data in the database.
logical_data.log The execution log for logical_data.script. Note that you should delete all existing tuples from your database prior to loading this new test data.
logical_schema.script The script file with the CREATE VIEW commands that create the logical schema for analysts.
logical_schema.log The execution log for logical_schema.script.
logical_queries.script The queries over the logical schema.
logical_queries.log An execution log showing the execution and results of the queries in logical_queries.script.

To submit these files use the syntax:

submit cmps180-np.w04 proj3-part3 README logical_data.script logical_data.log logical_schema.script logical_schema.log logical_queries.log

or proj3-part3-late if your project part is late.


Maintaining your databases

For the duration of the project, we suggest that you establish some kind of routine that includes reloading your database from the files created in this project part each time you want to get a "fresh" start with PostgreSQL. Remember to delete the contents of each relation (or destroy and recreate the relations) before reloading. Otherwise, unless there is a declared key (or you take APPEND out of your control file), PostgreSQL will happily append the new data to your old relation, causing your relation size to double, triple, quadruple, etc. To get rid of a table called T, issue the command:
drop table T;

If you want to get rid of all tuples in T without deleting the table itself, issue the command:

delete from T;

Recording Your Session

There are several methods for creating a typescript to turn in for your programming assignments. The most primitive way is to cut and paste your terminal output and save it in a file (if you have windowing capabilities). Another method is to use the Unix command script to record the terminal interaction. The script command records everything printed on your screen. The syntax for the command is:
    script [ -a ] [ filename ]
The record is written to filename. If no file name is given, the record is saved in the file typescript. The -a option allows you to append the session record to filename, rather than overwrite it. To end the recording, type.
    exit
For more information about script, check out its man page. 

Creating and running script files from psql

You will be using the psql command line interface to interact with your data base. See Managing a Database for information on starting using psql. Script files are text files of psql commands which can be executed like a batch file using the the \i command in psql. The syntax is \i filename entered following the psql prompt, where file name is the complete (case sensitive) name of the script file you desire to run. To run your execution script data.script and save the script log in the file data.log, do the following:
script data.log  to start saving the script log 
psql -a databasename  to run PostgreSQL's command line inteface using your database 
\i data.script  to import an execution script 
\q  to exit pqsl (PostgreSQL's command line interface) 
exit  to stop saving the script log 

The execution script file you create can consist of most any series of commands which you could enter following the psql prompt. This includes all of the SQL commands which you will be using to create, modify and test your data base. Examples include the CREATE TABLE and SELECT commands. Just like when using the psql command line interface you must terminate each SQL command in your script file with a semicolon. \i and \q do not need semicolons.

If you are recording your session using the script command described above into a script log then it is useful to start psql using the -a option so that all commands included in your execution script file will be echoed to the console and thus to your script log file. Here is an example script file that creates a table (relation) named products:

	CREATE TABLE products (
	  productID INT,
	  name VARCHAR(80)
          price NUMERIC(10,2),
          retailPrice NUMERIC(10,2)
        );
      
Here is an example script file that loads four tuples into the table (relation) named products created using the previous script file:
	COPY products FROM stdin USING DELIMITERS '|';
	1419|American Greetings CreataCard Gold V4.0|21.49|25.24
	1424|Barbie(R) Nail Designer(TM)|20.74|25.99
	1427|Panzer Commander|21.99|30.24
	1431|Riven: The Sequel to Myst|31.99|40.24
	\.
      
This is the format you will use for the files that load data into your tables. The USING DELIMITERS '|' and the use of '|' as a delimiter is optional. The default delimiter is the tab character. The delimited data on each line must match the attributes and their types in your table in a one to one manner and in the order they were defined in your CREATE TABLE commands. The COPY data must be terminated with '\.' For testing, some students have found it convenient to have a separate script file to populate each of the tables. I also find it convenient have a single script file to create all of my tables and another one to drop all of my tables. Look for examples at the end of this document.

Sample Script Files

createbeers.script
-- Sample Script file to Create and

-- Populate a BEERS DB

-- print out the current time

SELECT timeofday();

-- Create and Populate Tables

CREATE TABLE Beers (
  name VARCHAR(30),
  manf VARCHAR(50)
);

COPY Beers FROM stdin USING DELIMITERS '|';
Coors|Adolph Coors
Coors Lite|Adolph Coors
Miller|Miller Brewing
Miller Lite|Miller Brewing
MGD|Miller Brewing
Bud|Anheuser-Busch
Bud Lite|Anheuser-Busch
Michelob|Anheuser-Busch
Anchor Steam|Anchor Brewing
\.

CREATE TABLE Bars (
  name VARCHAR(30),
  addr VARCHAR(50),
  license VARCHAR(50)
);

COPY Bars FROM stdin USING DELIMITERS '|';
Joe's|123 Any Street|B7462A
Sue's|456 My Way|C5473S
\.

CREATE TABLE Sells (
  bar VARCHAR(20),
  beer VARCHAR(30),
  price REAL
);

COPY Sells FROM stdin USING DELIMITERS '|';
Joe's|Coors|2.50
Joe's|Bud|2.50
Joe's|Bud Lite|2.50
Joe's|Michelob|2.50
Joe's|Anchor Steam|3.50
Sue's|Coors|2.00
Sue's|Miller|2.00
\.

CREATE TABLE Drinkers (
  name VARCHAR(30),
  addr VARCHAR(50),
  phone CHAR(16)
);

COPY Drinkers FROM stdin USING DELIMITERS '|';
Bill Jones|180 Saint St.|831-459-1812
Kelly Arthur|180 Alto Pl.|650-856-2002
Fred|1234 Fifth St.|831-426-1956
\.

CREATE TABLE Likes (
  drinker VARCHAR(30),
  beer VARCHAR(30)
);

COPY Likes FROM stdin USING DELIMITERS '|';
Bill Jones|Miller
Bill Jones|Michelob
Kelly Arthur|Anchor Steam
Fred|MGD
\.

CREATE TABLE Frequents (
  drinker VARCHAR(30),
  bar VARCHAR(30)
);

COPY Frequents FROM stdin USING DELIMITERS '|';
Bill Jones|Joe's
Bill Jones|Sue's
Kelly Arthur|Joe's
\.

-- Execute some SELECT queries--

SELECT * FROM Bars;
SELECT * FROM Drinkers;

-- print out the current time
SELECT timeofday();

dropbeers.script
DROP TABLE Beers;
DROP TABLE Bars;
DROP TABLE Sells;
DROP TABLE Likes;
DROP TABLE Frequents;
DROP TABLE Drinkers;