Академический Документы
Профессиональный Документы
Культура Документы
mpp333
Database: Problem Set 3
(1)
php Sandwich shop
File:database.php
<?php
$hostname = "localhost";
$database = "sandwich-shop";
$username = "root";
$password = "";
$mysqli = new mysqli($hostname, $username, $password , $database);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>
File: index.php
<?php
// Start the session
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sandwich Shop</title>
<script type="text/javascript">
function validateForm(){
var val = phoneno.value
if (/^\d{10}$/.test(val)) {
// value is ok, use it
} else {
alert("Invalid Phone number; must be ten digits")
phoneno.focus()
return false
}
}
</script>
</head>
<body>
<h2 style="text-align:center;">The Sandwich Shop</h2>
<form id="form1" name="form1" onSubmit="return validateForm()" method="post" action="in.php">
<table width="400" style="margin:0px auto; border:1px solid black;">
<tr>
<td>Enter your Phone Number</td>
<td>
<input type="text" name="phoneno" id="phoneno" />
</tr>
<tr>
<td>Searching For</td>
<td><input type="text" name="searchkey" id="searchkey" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" id="submit" value="Enter" /></td>
</tr>
</table>
</form>
</body>
</html>
File: in.php
<?php
// Start the session
session_start();
$_SESSION['phoneno'] = $_POST['phoneno'];
?>
<?php
include("database.php");
if(isset($_POST["searchkey"]))
{
$orderQuery = "select m.sname, m.size, m.price, s.description
from menu m, sandwich s
where s.description like ?
and m.sname = s.sname GROUP BY m.sname";
$searching = "%{$_POST['searchkey']}%";
$orderStmt = $mysqli->prepare($orderQuery);
$orderStmt->bind_param("s", $searching);
}
else
{
$orderQuery = "select m.sname, m.size, m.price, s.description
from menu m, sandwich s
where m.sname = s.sname";
$orderStmt = $mysqli->prepare($orderQuery);
}
if($orderStmt == false)
{
name='rdSandwich' value='rd#".$myrow['sname']."#".$myrow1['size']."'
onclick='handleClick(this);'/></td>";
$rowCount++;
}
echo "</tr>";
}
$sizeStmt->close();
$orderStmt->close();
//echo "<tr><td><input type='submit' name='submit' id='submit' value='Place
Order'></td></tr>";
echo "</table><input type='submit' name='submit' id='submit' value='submit Order'
style=margin:2%;></center>";
echo "<div>";
?>
</form>
</body>
</html>
File: submission.php
<?php
// Start the session
session_start();
?>
<?php
include("database.php");
if(isset($_POST['submit']))
{
if(! get_magic_quotes_gpc() )
{
$o_size = addslashes ($_POST['o_size']);
$o_sname = addslashes ($_POST['o_sname']);
}
else
{
$o_size = $_POST['o_size'];
$o_sname = $_POST['o_sname'];
}
$o_phone = $_SESSION['phoneno'];
echo "<h1 style=text-align:center>Order Summary</h1>";
echo "\n";
echo "<div>";
echo "<center><table border=1 width=500>";
echo "<tr><td>Phone Number:</td><td style=text-align:center> ".$o_phone."</td></tr>";
echo "<tr><td>Sandwich Name:</td><td style=text-align:center> ".$o_sname."</td></tr>";
echo "<tr><td>Size order:</td><td style=text-align:center> ".$o_size."</td></tr>";
echo "</table></center>";
echo "</div>";
$submitQuery = "Select * from orders where phone = ? and sname = ? and size = ? and status
= 'pending'";
$submitStmt = $mysqli->prepare($submitQuery);
if($submitStmt == false)
{
echo "failed";
trigger_error('Wrong SQL: ' . $submitQuery . ' Error: ' . $mysqli->error,
E_USER_ERROR);
}
$submitStmt->bind_param("iss", $o_phone,$o_sname,$o_size);
if(!$submitStmt->execute())
{
echo "Execute failed: (" . $submitStmt->errno . ") " . $submitStmt->error;
}
if (!($submitResult = $submitStmt->get_result()))
{
echo "Getting result set failed: (" . $submitStmt->errno . ") " . $submitStmt->error;
}
if($submitResult->num_rows > 0)
{
$updateInsertQuery = "Update orders set quantity = quantity + 1, o_time = now()
where phone = ? and sname = ? and size = ? and status = 'pending'";
}
else
{
$updateInsertQuery = "Insert into orders values (?, ?, ?, now(), 1, 'pending')";
}
$submitStmt->close();
$updateInsertStmt = $mysqli->prepare($updateInsertQuery);
if($updateInsertStmt == false)
{
echo "failed";
trigger_error('Wrong SQL: ' . $updateInsertQuery . ' Error: ' . $mysqli->error,
E_USER_ERROR);
}
$updateInsertStmt->bind_param("iss", $o_phone,$o_sname,$o_size);
if(!$updateInsertStmt->execute())
{
echo "Execute failed: (" . $updateInsertStmt->errno . ") " . $updateInsertStmt->error;
}
/* if (!($updateInsertResult = $updateInsertStmt->get_result()))
{
echo "Getting result set failed: (" . $updateInsertStmt->errno . ") " .
$updateInsertStmt->error;
} */
echo "<h2 style=text-align:center>Your Order has been placed successfully!!!</h2></br>";
$updateInsertStmt->close();
echo "<center><a href=index.php>Click here to go main page</a></center>";
}
?>
File: sandwich-shop.sql
-- phpMyAdmin SQL Dump
-- version 4.2.7.1
-- http://www.phpmyadmin.net
--- Host: 127.0.0.1
-- Generation Time: Nov 15, 2014 at 02:00 PM
-- Server version: 5.5.39
-- PHP Version: 5.4.31
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
(2) Consider a relational schema R = (A, B, C, D, E, F) satisfying the following functional dependencies:
F = { C B, CB AC, CAE FB, D E, CA B }
a) Derive all candidate keys for this schema.
CB
CBAC
BA
CA
CAEFB
EF
DE
DF
CADEAD
CADFAD
CADFBD
CADFCD
CDB
CDA
CDED
CDFD
CDCD
CDABCDEF
So CD is the candidate key.
(b) Derive a canonical cover of the functional dependencies in F.
F = { C B, CB AC, CAE FB, D E, CA B }
(1.)Singleton RHS
C B, CB A, CBC ,CAE F, CAE B ,D E, CA B }
(2.)No extraneous LHS attributes.
CB A
C closure is CBA
So we can remove B
So, C A
CBC
C closure is CBA
So, we get CC..(we will not be including this in canonical cover)
CAE F
C closure is CBA
So we can remove A
So, we get CE F
So the canonical cover is: CA, CB, CEF, DE
(c) Is the above schema in BCNF? Prove or disprove. If it is not in BCNF, convert it into BCNF.
No the above schema is not in BCNF.
Our candidate key is CD.
And it is not included on the left hand side of the functional dependency.
Quest 3
VideoStore (cid, cname, ccity, bid, bname, bcity, mid, mtitle, mgenre, myear, copyid, rentdate,
returndate, cost)
(a)It is not a good relational schema.
Because there are too many attributes in one single table, this will cause lot of redundancies.
If we say that 2 customers want to buy the same movie from the same branch then it will cause lot of
redundancy which is not correct.
Here cname, ccity are dependent on cid and so they can be written in a separate relation . Same can
be said for bname, bcity as they are associated with bid.
This schema will result in update, insert and deletion anomalies.
Example : 1 customer cannot buy from more than one branches . Moreover the customer cannot buy
the movie again ( assuming cid as the key)
g(c) Identify the set F of non-trivial functional dependencies for this schema. (It is enough to
identify a subset E such that the closures of E and F are the same.)
Functional Dependencies are:
cid bid
cid cname, ccity
bid bname, bcity
cid, copyid, bid, mid, rentdate, returndate cost
mid mtitle, mgenre, myear
mtitle, myear mid
g(d) Derive a canonical cover of the functional dependencies in F using the steps mentioned in
one of the question above .
We get the following functional dependencies which are in the canonical form
cid bid
cid cname, ccity
bid bname, bcity
cid, copyid, bid, mid, rentdate, returndate cost
mid mtitle, mgenre, myear
mtitle, myear mid
g(e) Is the above schema in BCNF? Prove or disprove. If it is not in BCNF, convert it into BCNF.
No the schema is not in BCNF
So the relations that are in BCNF are as follows:
(cid , cname, ccity)
(bid , bname, bcity)
(cid, copyid, bid, mid, rentdate, returndate, cost)
(mid, mtitle, mgenre, myear)
(mid, cid, copyid, rentdate, returndate)
(cid,bid)
Here the BCNF is lossy BCNF as mtitle, myear mid is not satisfied.
g(f) Is the BCNF schema from d) dependency-preserving? Prove or disprove. If not, convert it into
3NF.
The above BCNF is dependency preserving .
(h)Now remove the assumption in g) again. Suppose we change the copyID in the schema so that it is
globally unique, meaning that no two copies can have the same copyID, even if they are copies of
different movies at different branches. How does this change your answers to parts b) to f)?
h(b) Derive all candidate keys for this schema.
Candidate keys:
(cid , bid, copyid, mid, rentdate, returndate)
(cid, bid , copyid , mtitle, myear , rentdate, returndate)
h(c) Identify the set F of non-trivial functional dependencies for this schema. (It is enough to
identify a subset E such that the closures of E and F are the same.)
Functional Dependencies are:
cid cname, ccity
bid bname, bcity
cid, copyid, rentdate, returndate cost
mid mtitle, mgenre, myear
mtitle, myear mid
h(e) Is the above schema in BCNF? Prove or disprove. If it is not in BCNF, convert it into
BCNF.
No the schema is not in BCNF as, if we consider the functional dependency, cid cname, ccity ; cid
is neither a candidate key nor a trivial dependency.
So the BCNF is
(cid , cname, ccity)
(bid , bname, bcity)
(cid, copyid, mid, rentdate, returndate, cost)
(mid, mtitle, mgenre, myear)
(mid, cid, bid ,copyid, returndate, rentdate )
h(f) Is the BCNF schema from d) dependency-preserving? Prove or disprove. If not, convert it
into 3NF.
The above BCNF is dependency preserving as the functional dependency gets preserved.
Quest 4
The following database is used:
CUSTOMER TABLE
MOVIE TABLE
BRANCH TABLE
COPY TABLE
RENTAL TABLE
(a)
create view AvailableMovies as
(b)
create view CustView as
( select cid , cname
from customer )
Output:
Output:
Original table which got updated because we inserted the tuple (111111 , Tom Cruize) in the view
custview:
(d) Add a new table OldAddresses that stores the old address of a customer when the customer does an
address
change. Then implement a trigger that adds the cid and old address to this table whenever the address
attribute in the Customer table is updated. (Check: does the table contain an address?)
insert into copy
(copyid,mid,bid)
(select R1.copyid+1, R1.mid, R1.bid from
(select cp.copyid, b.bid, m.mid, count(m.title) as Movie_count
from branch b, movie m, copy cp, rented r
where cp.bid = b.bid and cp.mid = m.mid and
r.copyid = cp.copyid
group by b.bname, m.title)R1,
(select b.bid, m.mid, count(m.title) as Total
from branch b, movie m, copy cp, rented r
where cp.bid = b.bid and cp.mid = m.mid and
r.copyid = cp.copyid
group by b.bname)R2
where R1.bid = R2.bid
and R1.Movie_count/R2.Total > 0.9);
Before Query:
(f) Write a query that lists all foreign keys in this database
SELECT
concat(table_name, '.', column_name) as 'foreign key',
concat(referenced_table_name, '.', referenced_column_name) as 'references'
FROM
information_schema.key_column_usage
WHERE
referenced_table_name is not null AND
table_schema = 'assignment3'