(This document is adapted from the projects for CS180 Winter ’02 by Arthur Keller)
The schema has five relations as given below:
Assumptions:
Studio names are unique.
The producerCert is the certificate number of the movie executive who
directed the movie.
The length of the movie represents the length in minutes.
The networth of movie executives is assumed to be in millions.
This assignment is divided into two parts. Part A helps you gain familiarity
with PostgreSQL, creating tables and populating them with data. Part B
helps you gain experience with SQL programming by writing SQL queries for
retrieving answers to questions about a database that is provided.
Create your first database using the shell command:
createdb username
where "username" is your cats login username.
Then you can access your database with the command:
psql -a
To start a command line interface to PostgreSQL connecting to a different database, use the command:
psql -a databasename
where "databasename" is the name of your database (which should begin with your cats username).
(b) Create relations for your database based on the relational database schema given to you. Use the CREATE TABLE command to specify each relation its attributes and 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.
Turn in a script log showing an PostgreSQL session in which your relations are created successfully. Please see Recording Your Session below for details.
(c) For each relation in your database, create an execution script file containing a Copy Command and a few (approximately 5-10) records of "realistic" data. Then execute the script file from the psql command line using the \i option. Please see Creating execution script files below for more details.
Turn in a listing showing the contents of the files you created, the successful loading of the data into PostgreSQL, and the execution of "SELECT *" commands to show the contents of each relation.
Note:
1. Make sure not to generate
tuples that violate the key constraints.
2. The database almost certainly
includes relations that are expected to join with each other.
For example, the producerCert in the Movie relation should join with certnum of MovieExec relation. When generating data, be sure to generate values that actually do join. There are a couple of ways to properly generate joining values. One way is to generate records for multiple relations (e.g., Movie and MovieExec) at the same time. Another way is to generate the records for one relation first, and then use the joining values for the other relation. For example, you could generate records for relation Movie first, then use the Movie.producerCert values when creating values for MovieExec.certnum.
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, course 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 MOVIES 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 and your execution of SELECT queries on
each table.
|
To submit these files use the syntax:
submit cmps180-wt.w03 proj2-part1 README create.script create.log data.script data.log
or proj2-late if your project part is late.
drop table T;
delete from T;
script [ -a ] [ filename ]
exit
|
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)
);
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
\.
Important: Be sure to complete the PART A of the
assignment and save all the files you need to submit for that part before
dropping your tables.
Also, you need to create and populate the database with the data provided
(as stated below), else your queries will not produce the desired results.
Download the create-tables script and the populate-data script files. Once you have created the tables and populated them with data provided, you need to write queries to answer the following questions about the data.
1. Find all movies directed by Garry Marshall.
2. Find the title and genre of movies that start with 'The' and were
made after 1998.
3. Give the title, studio-name and studio# of those movies that star
Richard Gere but not Julia Roberts.
4. Find the title and genre of the longest movie.
5. Find name and address of those female movie stars who are also movie
executives with a net worth of over 10 million dollars.
6. Find the certificate number and average movie length for those executives
who have a net worth of atleast 10 million dollars and whose none of the
movies are smaller than 110 mins.
7. Find the name and net worth of those movie executives who have worked
with atleast one movie star born before 1962.
8. Find the studio that made the maximum number of movies.
For this part of the assignment you will submit a file which contains each of your queries in the order of the questions. Also, submit a log file that contains the results of running your queries against the database. Last, a separate README file should be included which indicates the names and descriptions of the files you have turned in. An example submit statement is:
submit cmps180-wt.w03 proj2-part2 README query.script query.log
Example Queries:
-- Sample Script file to Create and
-- Populate a BEERS DB
-- print out the current time
SELECT timeofday();
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();
DROP TABLE Beers;
DROP TABLE Bars;
DROP TABLE Sells;
DROP TABLE Likes;
DROP TABLE Frequents;
DROP TABLE Drinkers;