Lab assignment work must be submitted electronically by midnight at the end of the day that it is due. Programming work submitted after the deadline but before the "late deadline" -- midnight 48 hours after the deadline -- will be accepted but penalized 50%. No lab assignment work will be accepted after the late deadline.
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 -awhich will start a command line interface to PostgreSQL connected to the database with your user-name. (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.)
Description: Translate your E-R diagram to an appropriate relational schema in your PostgreSQL database. To do the translation, use the procedure outlined in Chapter 3 of the textbook. Use the CREATE TABLE command to specify each relation, its attributes, and its attribute types; see CREATE TABLE from the PostgreSQL interactive documentation. If you have an attribute that represents a date and/or time, you may want to look at the page on FAQ: Working with Dates and Times in PostgreSQL. In your table definitions, include the appropriate PRIMARY KEY, FOREIGN KEY, UNIQUE, and NOT NULL clauses to denote the constraints of your relational schema.
For each relation in your database, create an execution script file containing a COPY command and a few records of "realistic" data. More specifically, your test data should adhere to the following requirements:
Note that these are the minimal requirements for your database. If you wish, you can include more tuples or even write a program that creates synthetic data. In any case, pay attention to the following points:
Once you have created the script file, run it from the psql command line using the \i option. Please see Creating execution script files below for more details.
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. |
| create.script | The execution script file you used to create tables for the relational database. |
| create.log | A record of your session creating your tables. |
| data.script | The execution script file you used to populate your small database. |
| data.log | script log (i.e., a recording) of your session using data.script to populate your DB. |
To submit these files use the syntax:
submit cmps180-np.w04 proj2-part1 README create.script create.log data.script data.log
or proj2-late if your project part is late.
Description: You have a meeting with an executive from El Cheapo in order to give a demo of your relational database for MP3s'R'Us. More specifically, the executive asks you to answer the following queries:
What to turn in: Please see Recording Your Session below for a guide to preparing output to be submitted. For this part of the assignment, 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. |
| queries.script | The execution script file that contains the SQL queries, in the same order as the questions. |
| queries.log | A record of running queries.log and the results returned from the database. |
To submit these files use the syntax:
submit cmps180-np.w04 proj2-part2 README queries.script queries.log
or proj2-late if your project part is late.
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;
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.
exitFor more information about script, check out its man page.
| script data.log | to start saving the script log |
| psql -a | to run PostgreSQL's command line interface 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 file to Create a BEERS DB -- print out the current time SELECT timeofday(); -- Create Tables CREATE TABLE Beers ( name VARCHAR(30), manf VARCHAR(50) ); CREATE TABLE Bars ( name VARCHAR(30), addr VARCHAR(50), license VARCHAR(50) ); CREATE TABLE Sells ( bar VARCHAR(20), beer VARCHAR(30), price REAL ); CREATE TABLE Drinkers ( name VARCHAR(30), addr VARCHAR(50), phone CHAR(16) ); CREATE TABLE Likes ( drinker VARCHAR(30), beer VARCHAR(30) ); CREATE TABLE Frequents ( drinker VARCHAR(30), bar VARCHAR(30) ); -- print out the current time SELECT timeofday();
-- Sample Script file to Populate a BEERS DB -- print out the current time SELECT timeofday(); -- Populate the tables 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 \. COPY Bars FROM stdin USING DELIMITERS '|'; Joe's|123 Any Street|B7462A Sue's|456 My Way|C5473S \. 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 \. 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 \. COPY Likes FROM stdin USING DELIMITERS '|'; Bill Jones|Miller Bill Jones|Michelob Kelly Arthur|Anchor Steam Fred|MGD \. COPY Frequents FROM stdin USING DELIMITERS '|'; Bill Jones|Joe's Bill Jones|Sue's Kelly Arthur|Joe's \. -- print out the current time SELECT timeofday();
-- print out the current time SELECT timeofday(); -- Execute some SELECT queries-- SELECT * FROM Bars; SELECT * FROM Drinkers; -- print out the current time SELECT timeofday();
DROP TABLE Beers; DROP TABLE Bars; DROP TABLE Sells; DROP TABLE Likes; DROP TABLE Frequents; DROP TABLE Drinkers;