Project Report

advertisement
Boswell, Daniel
CPSC 3375 – Database Concepts I
Final Project
May 5, 2010
1
Requirements




















Each movie has a title, release date, cast, genre(s), MPAA rating, plot outline, and
runtime (in minutes).
Store the character played by each actor/actress.
Some movies have a web site.
Each actor/actress has a name, date of birth, birthplace, hair color, eye color, and height.
A salary is paid to each actor/actress that is in a movie.
Each MPAA rating has an abbreviation and a description (e.g., the description for G is
General Audiences – All Ages Permitted).
A customer creates an account that is accessed via a username and password. The
account is also associated with a name and phone number.
Each account has a mailing address that is used as the destination for movie rentals.
To pay the monthly fees, each account is associated with a credit card number and
expiration date
Each customer is billed on the day of the month that he/she set up his/her account. For
example, if an account is set up on January 30, the account will be billed on the 30th of
each month.
Four subscription options are available
o 1 movie at a time ($9.49) – allows the customer to have 1 movie rental at a time and
no maximum rentals per month
o 2 movie at a time ($18.48) – allows the customer to have 2 movie rentals at a time
and no maximum rentals per month
o 3 movie at a time ($27.72) – allows the customer to have 3 movie rentals at a time
and no maximum rentals per month
o 4 movie at a time ($36.96) – allows the customer to have 4 movie rentals at a time
and no maximum rentals per month
Each account can have up to 10 profiles. Profiles are used to allow several people (e.g.,
family members) to share a single account. One profile is designated as the master
profile. This profile is used to maintain the account and is the recipient of important email communication from RipFlix.
Each profile has a name, e-mail address, username, password, movie queue (with up to
50 movies), number of movies (i.e., the number of movies from the account assigned to
the user).
Each profile is used to rate movies,1 (poor) through 5 (excellent). These ratings are used
to recommend additional movies to RipFlix customers.
Each profile has a maximum rating, which restricts the movies that can be added to the
queue by the profile (e.g., PG-13 indicates that the profile is allowed to rent G, PG, and
PG-13 movies, but not R, NC-17, or unrated).
RipFlix owns many copies of each movie. Thus, many users may rent the same movie
title, simultaneously.
When a movie is shipped to a customer, the shipping date is recorded. Similarly, the date
the movie is received by RipFlix is recorded.
Only one address per account.
I assumed that the subscriptions can be edited later. ie. a raise in prices.
Only one phone number per account
CPSC 3385
2-18
2
ER Diagram
3
Table Diagram
4
Index Design and Justification
CREATE INDEX movie ON Movies (title);
This will be the most commonly used search by users; therefore, I created this index.
CREATE INDEX mid ON Movies (mid);
I created this index because there are three table that pull the midoff of this table.
CREATE INDEX fname ON Actors (fname);
This is the second most commonly used search by users; therefore, I created this index.
CREATE INDEX username ON Profiles (username);
I created this index because there are four other table that pull the username off of this table.
I chose to create and index on the title of the movies, the first and last name of the actors, the mid
of the movie table, and the usernames of the profiles. I chose the movie title and the first and last
name of the actors because these would be the most searched fields. Then I chose the username
of the profiles because there are several other tables that use this field so speed is an issue here.
The cost of inserting into the index is outweighed by the speed up of the searches and lookups.
5
Database Creation Script
CREATE TABLE Mpaa (
rating VARCHAR NOT NULL ,
description VARCHAR
,
PRIMARY KEY(rating))
CREATE TABLE Subscription (
sid VARCHAR NOT NULL ,
price DOUBLE NOT NULL ,
descripton VARCHAR ,
num_of_movies INTEGER NOT NULL ,
PRIMARY KEY(sid));
CREATE TABLE top10 (
rank INTEGER NOT NULL ,
title VARCHAR ,
rating DOUBLE
,
PRIMARY KEY(rank));
CREATE TABLE Actors (
aid VARCHAR NOT NULL ,
fname VARCHAR ,
lname VARCHAR NOT NULL ,
dob CHAR(20) ,
pob VARCHAR ,
height VARCHAR ,
eyecolor VARCHAR ,
haircolor VARCHAR ,
website VARCHAR
,
PRIMARY KEY(aid));
CREATE TABLE Movies (
mid VARCHAR NOT NULL ,
title VARCHAR NOT NULL ,
Mpaa_rating VARCHAR NOT NULL ,
release_date CHAR(20) ,
runtime INTEGER ,
num_of_copies INTEGER ,
genre VARCHAR ,
website VARCHAR ,
plot TEXT ,
PRIMARY KEY(mid) ,
FOREIGN KEY(Mpaa_rating)
REFERENCES Mpaa(rating));
CPSC 3385
6-18
CREATE TABLE Customer (
account VARCHAR NOT NULL ,
Subscription_sid VARCHAR NOT NULL ,
address VARCHAR NOT NULL ,
city VARCHAR NOT NULL ,
state VARCHAR NOT NULL ,
zip VARCHAR NOT NULL ,
phone VARCHAR ,
ccnum VARCHAR NOT NULL ,
expiration CHAR(20) NOT NULL ,
creation CHAR(20) NOT NULL ,
main_username VARCHAR NOT NULL ,
PRIMARY KEY(account) ,
FOREIGN KEY(Subscription_sid)
REFERENCES Subscription(sid));
CREATE TABLE Cast (
cid VARCHAR NOT NULL ,
character_2 VARCHAR NOT NULL ,
Movies_mid VARCHAR NOT NULL ,
Actors_aid VARCHAR NOT NULL ,
salary DOUBLE
,
PRIMARY KEY(cid) ,
FOREIGN KEY(Actors_aid)
REFERENCES Actors(aid),
FOREIGN KEY(Movies_mid)
REFERENCES Movies(mid));
CREATE TABLE Profiles (
username VARCHAR NOT NULL ,
Customer_account VARCHAR NOT NULL ,
fname VARCHAR ,
middlei VARCHAR ,
lname VARCHAR ,
password_2 VARCHAR NOT NULL ,
emial VARCHAR ,
maxr VARCHAR ,
moviecount INTEGER
,
PRIMARY KEY(username) ,
FOREIGN KEY(Customer_account)
REFERENCES Customer(account));
CREATE TABLE Ratings (
ratingid VARCHAR NOT NULL ,
Profiles_username VARCHAR NOT NULL ,
CPSC 3385
7-18
rating DOUBLE
,
PRIMARY KEY(ratingid) ,
FOREIGN KEY(Profiles_username)
REFERENCES Profiles(username));
CREATE TABLE History (
historyid VARCHAR NOT NULL ,
Movies_mid VARCHAR NOT NULL ,
Profiles_username VARCHAR NOT NULL ,
shipped CHAR(20) ,
received CHAR(20)
,
PRIMARY KEY(historyid) ,
FOREIGN KEY(Profiles_username)
REFERENCES Profiles(username),
FOREIGN KEY(Movies_mid)
REFERENCES Movies(mid));
CREATE TABLE Queue (
queuid VARCHAR NOT NULL ,
Movies_mid VARCHAR NOT NULL ,
Profiles_username VARCHAR NOT NULL ,
queuenum INTEGER
,
PRIMARY KEY(queuid) ,
FOREIGN KEY(Profiles_username)
REFERENCES Profiles(username),
FOREIGN KEY(Movies_mid)
REFERENCES Movies(mid));
CREATE INDEX movie ON Movies (title);
CREATE INDEX mid ON Movies (mid);
CREATE INDEX fname ON Actors (fname);
CREATE INDEX username ON Profiles (username);
CPSC 3385
8-18
6
Database Population Script
LOAD DATA LOCAL INFILE 'Movies.csv'
INTO TABLE Movies
FIELDS TERMINATED BY ',
'(mid,title,Mpaa_rating,release_date,runtime,num_of_copies,genre,website,plot);
LOAD DATA LOCAL INFILE 'Ratings.csv'
INTO TABLE Ratings
FIELDS TERMINATED BY ', '(ratingid,Profiles_username,rating,title);
LOAD DATA LOCAL INFILE 'Actors.csv'
INTO TABLE Actors
FIELDS TERMINATED BY ',
'(aid,fname,lname,dob,pob,height,eyecolor,haircolor,website);
LOAD DATA LOCAL INFILE 'Cast.csv'
INTO TABLE Cast
FIELDS TERMINATED BY ', '(cid,character_2,Movies_mid,Actors_aid,salary);
LOAD DATA LOCAL INFILE 'Mpaa.csv'
INTO TABLE Mpaa
FIELDS LOCAL TERMINATED BY ', '(rating,description);
LOAD DATA LOCAL INFILE 'top10.csv'
INTO TABLE top10
FIELDS TERMINATED BY ', '(rank,title,rating);
LOAD DATA LOCAL INFILE ‘History.csv'
INTO TABLE History
FIELDS TERMINATED BY ', '(historyid,
Profiles_username,shipped,received,Movies_mid);
LOAD DATA LOCAL INFILE ‘Queue.csv'
INTO TABLE Queue
FIELDS TERMINATED BY ', '(queuid,Profiles_username,queuenum,Movies_mid);
LOAD DATA LOCAL INFILE 'Profiles.csv'
INTO TABLE Profiles
FIELDS TERMINATED BY ',
'(username,Customer_account,fname,middlei,lname,password_2,emial,maxr,moviecount);
LOAD DATA LOCAL INFILE 'Customers.csv'
INTO TABLE Customer
FIELDS TERMINATED BY ',
'(account,Subscription_sid,address,city,state,zip,phone,ccnum,main_username,expiration,creatio
n);
CPSC 3385
9-18
LOAD DATA LOCAL INFILE 'Subscription.csv'
INTO TABLE Subscription
FIELDS TERMINATED BY ', '(sid,price,descripton,num_of_movies);
CPSC 3385
10-18
7
Database Queries
SELECT DISTINCT title, Mpaa_rating, Mpaa.description
FROM Movies, Mpaa
Where Movies.genre LIKE ‘%Action%’ AND Movie.Mpaa_rating = Mpaa.rating
ORDER BY title;
SELECT rating, COUNT(Movies.mid) AS ‘Number of Movies’
FROM Mpaa, Movies
WHERE Mpaa.rating = Movies.MPaa_rating
GROUP BY rating;
CPSC 3385
11-18
SELECT title
FROM Movies
WHERE Mpaa_rating = (SELECT maxr FROM Profiles WHERE fname = ‘Samantha’ and
middle = ‘M’ and lname = ‘Shelnutt’);
SELECT DISTINCT genre, COUNT(Movies.mid) AS “Number of Movies”
FROM Movies
GROUP BY genre;
CPSC 3385
12-18
SELECT fname, middlei, lname, address, city, state, zip, ccnum, (price * 1.02) AS ‘fee’
FROM Profiles, Customer, Subscription
WHERE Customers.main_username = Profiles.username AND (Customers.creation LIKE
‘%/30/%’ || Customers.creation LIKE ‘%/31/%’) AND Customer.Subscription.sid =
Subscription.sid;
SELECT *
FROM top10
ORDERBY RANK;
CPSC 3385
13-18
SELECT fname, lname
FROM Actors
WHERE webite LIKE ‘%null%’;
SELECT fname, middlei, lname, emial
FROM Customer, Profiles
WHERE Customer.main_username = Profiles.username AND expiration < “12/11”;
CPSC 3385
14-18
SELECT fname, middlei, lname, emial, username, title
FROM Profiles, History, Movies
WHERE Profiles.username = History.Profiles_usermane and History.Movies_mid =
Movies.mid and History.recieved = ‘Null’;
SELECT username, emial, MAX(queuenum) AS “In queue”
FROM Profiles, Queue
HAVING MAX(queuENUM) < 4;
DELETE FROM Queue
WHERE Profiles_username = (SELECT username FROM Profiles WHERE Customer_account
= “2 1540 79940”);
DELETE FROM Ratings
WHERE Profiles_username = (SELECT username FROM Profiles WHERE
Customer_account = “2 1540 79940”);
DELETE FROM Profiles
WHERE Customer_account = “2 1540 79940”;
DELETE FROM Customers
WHERE account = “2 1540 79940”;
CPSC 3385
15-18
8
Web Page
Include the PHP code and a screen shot of your web page. Also, include the URL for your web
site.
http://sally.host.ualr.edu/~deboswell/index.php
index.php
<html>
<head>
<title>Actors Search</title>
</head>
<body>
<?php
if (isset($_GET[TargetLastName]))
{
print "<font size=+2><b><center>Actor List</center></b></font>";
$Success = mysql_connect("localhost", deboswell, "deboswell");
if (! $Success)
die ("ERROR: " . mysql_error());
$Success = mysql_select_db("deboswell");
if (! $Success)
die ("ERROR: " . mysql_error());
$query = "SELECT fname,lname,pob,dob FROM Actors WHERE lname =
'$_GET[TargetLastName]'";
$result = mysql_query($query);
if (! $result)
die("ERROR: " . mysql_error());
print "<table border = 1>";
print "<tr><td>First Name</td><td>Last Name</td><td>Place of Birth</td><td>Date of
Birth</td></tr>";
while ($row = mysql_fetch_array($result))
{
print "<tr>";
CPSC 3385
16-18
print "<td>" . $row["fname"] . "</td>";
print "<td>" . $row["lname"] . "</td>";
print "<td>" . $row["pob"] . "</td>";
print "<td>" . $row["dob"] . "</td>";
print "</tr>";
}
print "</table>";
$Success = mysql_close();
if (! $Success)
die ("ERROR: " . mysql_error());
}
else {
print "<font size=+2><b><center>Actor Search</center></b></font>";
print "<form name=Last Name method=get action=index.php>";
print "<label for=TargetLastName >Actor Last Name:</label><br>";
print "<input name=TargetLastName id=TargetLastName type=text size=32
maxlength=32>";
print "<input type=submit value=Search>";
print "</form>";
print "<form name=Year method=get action=index2.php>";
print "<label for=TargetDate ><br/><br/>Actor Birth Year:</label><br>";
print "<input name=TargetDate id=TargetDate type=text size=32 maxlength=32>";
print "<input type=submit value=Search>";
print "</form>";
}
?>
</body>
</html>
Index2.php
<html>
<head>
<title>Actors Search</title>
</head>
<body>
<?php
if (isset($_GET[TargetDate]))
CPSC 3385
17-18
{
print "<font size=+2><b><center>Actor List</center></b></font>";
$Success = mysql_connect("localhost", deboswell, "deboswell");
if (! $Success)
die ("ERROR: " . mysql_error());
$Success = mysql_select_db("deboswell");
if (! $Success)
die ("ERROR: " . mysql_error());
$query = "SELECT fname,lname,pob,dob FROM Actors WHERE dob LIKE
'%$_GET[TargetDate]%'";
$result = mysql_query($query);
if (! $result)
die("ERROR: " . mysql_error());
print "<table border = 1>";
print "<tr><td>First Name</td><td>Last Name</td><td>Place of Birth</td><td>Date of
Birth</td></tr>";
while ($row = mysql_fetch_array($result))
{
print "<tr>";
print "<td>" . $row["fname"] . "</td>";
print "<td>" . $row["lname"] . "</td>";
print "<td>" . $row["pob"] . "</td>";
print "<td>" . $row["dob"] . "</td>";
print "</tr>";
}
print "</table>";
$Success = mysql_close();
if (! $Success)
die ("ERROR: " . mysql_error());
}
?>
</body>
</html>
CPSC 3385
18-18
Download