Download Sample Relational Database

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Sample Relational Database
Modern Information Systems
WT 20xx/20xx
Name Matriculation#
audiobook-library (idea from audiobook-library Graz-Mariahilf)
Table of Contents:
Database Schema: ................................................................................................................................... 2
Practical implementation of the database with mySQL .......................................................................... 3
Inserting Test Data with PHP and HTML ................................................................................................. 4
Database Queries(PHP) ........................................................................................................................... 7
XSL Transformation with XSLT ............................................................................................................... 11
Page 1
Database Schema:
Domains:
ATitle string;
AAuthor string;
AYearPublication integer;
ASpeaker string;
AType string;
APublishing string;
AISBN integer;
BBegin date;
BEnd date;
Cid integer;
CFirstName string;
CLastName string;
CDateBirth date;
CCity string;
CPostcode integer;
CAddress string;
articles title
articles author
articles year of publication
articles speaker
articles type
articles publishing
articles international standard book number
borrowings begin date
borrowings end date
customers id
customers firstname
customers lastname
customers date of birth
customer city
customer postcode
customers address
Relations
Relation: article(aisbn, atitle, aauthor, ayearpublication, aspeaker, atype, apublishing)
Relation: borrowing(aisbn, cid, bbegin, bend)
Relation: customer(cid, cfirstname, clastname, cdatebirth, ccity, cpostcode, caddress)
Page 2
Practical implementation of the database with mySQL
Relation
CREATE TABLE Article(
AISBN
VARCHAR(17)
ATitle
VARCHAR(50)
AAuthor
VARCHAR(50)
AYearPublication INTEGER,
ASpeaker
VARCHAR(50),
AType
VARCHAR(20)
APublishing
VARCHAR(50),
PRIMARY KEY(AISBN)
);
CREATE TABLE Customer(
Cid
CFirstName
CLastName
CDateBirth
CCity
CPostcode
CAddress
PRIMARY KEY(Cid)
);
INTEGER
VARCHAR(50)
VARCHAR(50)
DATE,
VARCHAR(50)
INTEGER
VARCHAR(50)
NOT NULL ,
NOT NULL ,
NOT NULL ,
NOT NULL ,
NOT NULL AUTO_INCREMENT,
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL,
CREATE TABLE Borrowing(
AISBN
VARCHAR(17) NOT NULL,
CId
INTEGER
NOT NULL,
BBegin
INTEGER
NOT NULL,
BEnd
INTEGER,
PRIMARY KEY(AISBN, Cid, BBegin),
FOREIGN KEY(AISBN)REFERENCES Article(AISBN),
FOREIGN KEY(Cid) REFERENCES Customer(Cid)
);
Page 3
Inserting Test Data with PHP and HTML
#PHP script handling the values of article_form
<?
/* this script will handle the variables passed from the insert_article_form.php file */
/* declare some relevant variables */
PRINT "<html>";
PRINT "<head><title>Insert New Article</title></head>";
PRINT "<body>";
$atitle = $_POST["atitle"];
$aauthor = $_POST["aauthor"];
$ayearpublication = $_POST["ayearpublication"];
$aspeaker = $_POST["aspeaker"];
$atype = $_POST["atype"];
$apublishing = $_POST["apublishing"];
$aisbn = $_POST["aisbn"];
/* MySQL table created to store the data */
$tablename = "article";
// make connection to the databse
require("connect.php");
$today = date("Y-m-d");
PRINT "Today is: $today.<br>";
PRINT "<hr>";
PRINT "You issued the following INSERT query:<br>";
PRINT "<ul>";
PRINT "<li>Title of the Article: <em>$atitle</em>";
PRINT "<li>Author of the Article: <em>$aauthor</em>";
PRINT "<li>Year of Publication: <em>$ayearpublication</em>";
PRINT "<li>Speaker of the Article: <em>$aspeaker</em>";
PRINT "<li>Publishing: <em>$apublishing</em>";
PRINT "<li>ISBN: <em>$aisbn</em>";
PRINT "</ul>";
PRINT "<hr>";
/* Insert information into table */
$query = "INSERT INTO $tablename VALUES('$aisbn', '$atitle', '$aauthor', '$ayearpublication',
'$aspeaker', '$atype', '$apublishing')"; // Fehler
$result = MYSQL_QUERY($query);
Page 4
PRINT "Database server response:<br>";
if($result != 0){
$affected_rows = mysql_affected_rows();
PRINT "<strong>Query OK. Rows affected $affected_rows</strong>";
} else{
PRINT "<strong>Query FAILED. ".'Ungültige Abfrage: ' . mysql_error() . "</strong>";
}
PRINT "<hr>";
PRINT "<a href=\"insert_article.html\">new article</a><br>";
PRINT "<a href=\"main.html\">Back</a>";
PRINT "</body>";
PRINT "</html>";
// close connection to the database
require("disconnect.php");
?>
#inserting a new customer
<?
/* this script will handle the variables passed from the insert_customer.html file */
/* declare some relevant variables */
PRINT "<html>";
PRINT "<head><title>Insert New Customer</title></head>";
PRINT "<body>";
$cfirstname = $_POST["cfirstname"];
$clastname = $_POST["clastname"];
$cdatebirth = $_POST["cdatebirth"];
$ccity = $_POST["ccity"];
$cpostcode = $_POST["cpostcode"];
$caddress = $_POST["caddress"];
/* MySQL table created to store the data */
$tablename = "customer";
// make connection to the databse
require("connect.php");
$today = date("Y-m-d");
PRINT "Today is: $today.<br>";
PRINT "<hr>";
PRINT "You issued the following INSERT query:<br>";
PRINT "<ul>";
PRINT "<li>Customer first name: <em>$cfirstname</em>";
Page 5
PRINT "<li>Customer last name: <em>$clastname</em>";
PRINT "<li>Customer date of birth (YYYYMMDD): <em>$cdatebirth</em>";
PRINT "<li>Customer city: <em>$ccity</em>";
PRINT "<li>Customer postcode: <em>$cpostcode</em>";
PRINT "<li>Customer address: <em>$caddress</em>";
PRINT "</ul>";
PRINT "<hr>";
/* Insert information into table */
$query = "INSERT INTO $tablename VALUES(NULL, '$cfirstname', '$clastname', '$cdatebirth', '$ccity',
'$cpostcode', '$caddress')";
$result = MYSQL_QUERY($query);
PRINT "Database server response:<br>";
if($result != 0){
$affected_rows = mysql_affected_rows();
PRINT "<strong>Query OK. Rows affected $affected_rows</strong>";
} else{
PRINT "<strong>Query FAILED. ".'Ungültige Abfrage: ' . mysql_error() . "</strong>";
}
PRINT "<hr>";
PRINT "<a href=\"insert_customer.html\">new customer</a><br>";
PRINT "<a href=\"main.html\">Back</a>";
PRINT "</body>";
PRINT "</html>";
// close connection to the database
require("disconnect.php");
?>
Page 6
Database Queries(PHP)
Find all articles borrowed by a specific customer...
<?
/* this script find the data of the chosen article of find_article_form */
PRINT "<html>";
PRINT "<head><title>Find Article by chosen Customer</title></head>";
PRINT "<body>";
/* declare some relevant variables */
$cid = $_POST["cid"];
// make connection to the databse
require("connect.php");
$query = "SELECT article.AId, article.Atitle, article.AAuthor FROM article, borrowing WHERE
article.AId = borrowing.AId and borrowing.CId='$cid'";
$result = MYSQL_QUERY($query);
/* How many rows in the result? */
$number = MYSQL_NUMROWS($result);
$i = 0;
if($number == 0){
PRINT "No Articles borrowed by this customer";
} elseif ($number > 0){
PRINT "<table border = 1>";
PRINT "<tr>";
PRINT "<td>AId</td>";
PRINT "<td>ATitle</td>";
PRINT "<td>AAuthor</td>";
PRINT "</tr>";
while($i < $number)
{
$aid = mysql_result($result, $i, "AId");
$atitle = mysql_result($result, $i, "ATitle");
$aauthor = mysql_result($result, $i, "AAuthor");
PRINT "<tr>";
PRINT "<td>$aid</td>";
PRINT "<td>$atitle</td>";
PRINT "<td>$aauthor</td>";
PRINT "</tr>";
$i++;
Page 7
}
PRINT "</table>";
}
PRINT "<p>";
PRINT "<a href=\"main.html\">BACK</a>";
PRINT "</body>";
PRINT "</html>";
// close connection to the database
require("disconnect.php");
?>
Find all customers by a specific borrowed article...
<?
/* this script find the data of the chosen article of find_customer_form */
PRINT "<html>";
PRINT "<head><title>Find Customer by chosen article</title></head>";
PRINT "<body>";
/* declare some relevant variables */
$aid = $_POST["aid"];
// make connection to the databse
require("connect.php");
$query = "SELECT customer.CId, customer.CFirstName, customer.CLastName FROM customer,
borrowing WHERE customer.CId = borrowing.CId and borrowing.AId='$aid'";
$result = MYSQL_QUERY($query);
/* How many rows in the result? */
$number = MYSQL_NUMROWS($result);
$i = 0;
if($number == 0){
PRINT "No Customer borrowed this book";
} elseif ($number > 0){
PRINT "<table border = 1>";
PRINT "<tr>";
PRINT "<td>CId</td>";
PRINT "<td>CFirstName</td>";
PRINT "<td>CLastName</td>";
PRINT "</tr>";
while($i < $number)
{
Page 8
$cid = mysql_result($result, $i, "CId");
$cfirstname = mysql_result($result, $i, "CFirstName");
$clastname = mysql_result($result, $i, "CLastName");
PRINT "<tr>";
PRINT "<td>$cid</td>";
PRINT "<td>$cfirstname</td>";
PRINT "<td>$clastname</td>";
PRINT "</tr>";
$i++;
}
PRINT "</table>";
}
PRINT "<p>";
PRINT "<a href=\"main.html\">BACK</a>";
PRINT "</body>";
PRINT "</html>";
// close connection to the database
require("disconnect.php");
?>
Get a list of most borrowed articles.
<?
/* this script makes a list with the most borrowed articles */
PRINT "<html>";
PRINT "<head><title>Find most borrowed articles</title></head>";
PRINT "<body>";
// make connection to the databse
require("connect.php");
$query = "SELECT article.AId, article.ATitle, article.AAuthor , article.ASpeaker FROM article,
borrowing WHERE article.AId = borrowing.AId Group by article.AId Having COUNT(*) > 2";
$result = MYSQL_QUERY($query);
/* How many rows in the result? */
$number = MYSQL_NUMROWS($result);
$i = 0;
if ($number == 0){
PRINT "<CENTER>";
PRINT "<P>";
Page 9
PRINT "No borrowed articles in the database";
PRINT "</CENTER>";
} ELSEIF ($number == 1){
PRINT "<CENTER>";
PRINT "<P>";
PRINT "Only too low frequently borrowed articles in the database";
PRINT "</CENTER>";
} ELSEIF ($number > 1){
PRINT "<CENTER>";
PRINT "<P>";
PRINT "Articles: $number";
PRINT "<BR>";
PRINT "<table border = 1>";
PRINT "<tr>";
PRINT "<td>AId</td>";
PRINT "<td>ATitle</td>";
PRINT "<td>AAuthor</td>";
PRINT "<td>ASpeaker</td>";
PRINT "</tr>";
while ($i < $number)
{
$aid = mysql_result($result, $i, "AId");
$atitle = mysql_result($result, $i, "ATitle");
$aauthor = mysql_result($result, $i, "AAuthor");
$aspeaker = mysql_result($result, $i, "ASpeaker");
PRINT "<tr>";
PRINT "<td>$aid</td>";
PRINT "<td>$atitle</td>";
PRINT "<td>$aauthor</td>";
PRINT "<td>$aspeaker</td>";
PRINT "</tr>";
$i++;
}
PRINT "</table>";
PRINT "</CENTER>";
}
PRINT "<hr>";
PRINT "<a href = \"main.html\">Back</a>";
PRINT "</body>";
PRINT "</html>";
// close connection to the database
require("disconnect.php");
?>
Page 10
XSL Transformation with XSLT
Clientside
<?php
header("Content-Type: text/xml");
/*this script will create a list of borrowings */
/* declare some relevant variables */
// make connection to the databse
require("connect.php");
/* Select the data from the database */
$query = "select customer.cfirstname, customer.clastname, article.atitle, article.atype,
borrowing.bbegin, borrowing.bend from customer, article, borrowing where customer.cid =
borrowing.cid and article.aisbn = borrowing.aisbn";
$result = MYSQL_QUERY($query);
/* How many rows in the result? */
$number = MYSQL_NUMROWS($result);
/* Print these results into an XML file*/
$i = 0;
$content .= "<?xml-stylesheet type=\"text/xsl\" href=\"kidsstyle.xsl"?>\n";
$content .= "<!-- all borrowings -->\n";
$content .= "<borrowings>\n";
if ($number > 0){
while ($i < $number){
$cfirstname = mysql_result($result, $i, "cfirstname");
$clastname = mysql_result($result, $i, "clastname");
$atitle = mysql_result($result, $i, "atitle");
$bbegin = mysql_result($result, $i, "bbegin");
$bend = mysql_result($result, $i, "bend");
$content .= "<!-- borrowing -->\n";
$content .= "<borrowing bbegin = \"$bbegin\" bend = \"$bend\">\n";
$content .= "<customer>$cfirstname</customer>\n";
$content .= "<customer>$clastname</customer>\n";
$content .= "<article>$atitle</article>\n";
Page 11
$content .= "\n";
$content .= "</borrowing>\n";
$i++;
}
}
$content .= "</borrowings>\n";
echo $content;
// close connection to the database
require("disconnect.php");
?>
Serverside
<?php
/* this script will creating the list of borrowings in correct style */
/* declare some relevant variables */
// make connection to the databse
require("connect.php");
/* Select the data from the database */
$query = "select customer.cfirstname, customer.clastname, article.atitle, article.atype,
borrowing.bbegin, borrowing.bend from customer, article, borrowing where customer.cid =
borrowing.cid and article.aisbn = borrowing.aisbn";
$result = MYSQL_QUERY($query);
/* How many rows in the result? */
$number = MYSQL_NUMROWS($result);
/* Print these results into an XML file*/
$i = 0;
$content = "<";
$content .= "?xml version=\"1.0\" encoding=\"UTF-8\"?";
$content .= ">\n";
$content .= "<!-- all borrowings -->\n";
$content .= "<borrowings>\n";
if ($number > 0){
while ($i < $number){
$cfirstname = mysql_result($result, $i, "cfirstname");
Page 12
$clastname = mysql_result($result, $i, "clastname");
$atitle = mysql_result($result, $i, "atitle");
$bbegin = mysql_result($result, $i, "bbegin");
$bend = mysql_result($result, $i, "bend");
$content .= "<!-- borrowing -->\n";
$content .= "<borrowing bbegin = \"$bbegin\" bend = \"$bend\">\n";
$content .= "<customer>$cfirstname</customer>\n";
$content .= "<customer>$clastname</customer>\n";
$content .= "<article>$atitle</article>\n";
$content .= "</borrowing>\n";
$i++;
}
}
$content .= "</borrowings>\n";
$file = fopen("tmp.xml", "w");
fwrite ($file, $content);
fclose($file);
// close connection to the database
require("disconnect.php");
$xml = new DomDocument;
$xml->load('tmp.xml');
$xsl = new DomDocument;
$xsl->load('kidsstyle.xsl');
/* Create an XSLT processor */
$proc = new XsltProcessor;
$proc->importStyleSheet($xsl);
/* Perform the transformation */
$html = $proc->transformToXML($xml);
/* Detect errors */
if (!$html) die('XSLT processing error: '.libxml_get_last_error());
/* Output the resulting HTML */
echo $html;
?>
#created XML file with exported data
<?xml version="1.0" encoding="UTF-8"?>
<!-- all borrowings -->
Page 13
<borrowings>
<!-- borrowing -->
<borrowing bbegin = "2012-01-05" bend = "2012-01-13">
<customer>Ingrid</customer>
<customer>Reip</customer>
<article>Die Nadel</article>
</borrowing>
<!-- borrowing -->
<borrowing bbegin = "2012-01-03" bend = "2012-01-05">
<customer>Ingrid</customer>
<customer>Reip</customer>
<article>Mathematik erleben</article>
</borrowing>
<!-- borrowing -->
<borrowing bbegin = "2012-01-03" bend = "2012-01-05">
<customer>Ingrid</customer>
<customer>Reip</customer>
<article>Wie immer Chefsache</article>
</borrowing>
<!-- borrowing -->
<borrowing bbegin = "2011-12-08" bend = "2011-12-26">
<customer>Michael</customer>
<customer>Reip</customer>
<article>Die Nadel</article>
</borrowing>
<!-- borrowing -->
<borrowing bbegin = "2012-01-04" bend = "2012-01-11">
<customer>Michael</customer>
<customer>Reip</customer>
<article>Ich hab die Unschuld kotzen sehen</article>
</borrowing>
<!-- borrowing -->
<borrowing bbegin = "2011-01-01" bend = "2011-01-15">
<customer>Michael</customer>
<customer>Reip</customer>
<article>Der Schatten des Windes</article>
</borrowing>
<!-- borrowing -->
<borrowing bbegin = "2011-11-16" bend = "2011-11-30">
<customer>Markus</customer>
<customer>Muster</customer>
<article>Die Nadel</article>
</borrowing>
<!-- borrowing -->
<borrowing bbegin = "2011-01-01" bend = "2011-01-15">
<customer>Markus</customer>
Page 14
<customer>Muster</customer>
<article>Wie immer Chefsache</article>
</borrowing>
<!-- borrowing -->
<borrowing bbegin = "2012-01-23" bend = "2012-01-30">
<customer>Robert</customer>
<customer>Flocker</customer>
<article>Die Nadel</article>
</borrowing>
<!-- borrowing -->
<borrowing bbegin = "2011-09-20" bend = "2011-10-03">
<customer>Robert</customer>
<customer>Flocker</customer>
<article>Wie immer Chefsache</article>
</borrowing>
</borrowings>
#stylesheet printstyle – similar kidsstyle
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<html>
<body>
<table>
<tr>
<td><strong>First Name</strong></td>
<td><strong>Last Name</strong></td>
<td><strong>Article</strong></td>
<td><strong>Borrowed on</strong></td>
</tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="borrowing">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="borrowing">
<tr>
<xsl:apply-templates/>
<td><xsl:value-of select="@bbegin"/></td>
Page 15
<td><xsl:value-of select="@bend"/></td>
</tr>
</xsl:template>
<xsl:template match="customer">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
<xsl:template match="article">
<td><xsl:value-of select="."/></td>
</xsl:template>
</xsl:stylesheet>
Page 16
Related documents