CS 174: Server-Side Web Programming February 14 Class Meeting Department of Computer Science San Jose State University Spring 2018 Instructor: Ron Mak (substitute for Fabio Di Troia) www.cs.sjsu.edu/~mak
Entity Integrity Constraint No primary key column of a relational table can have null (empty) values. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Entity Integrity Constraint, cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Foreign Keys A foreign key is a column in a table that refers to a primary key column in another table. In a relational schema, draw an arrow from the foreign key to the corresponding primary key.
Mapping 1:M Relationships Mandatory participation on both sides. The foreign key belongs on the M side of the 1:M relationship. It corresponds to the primary key on the 1 side. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping 1:M Relationships, cont’d Optional participation on the 1 side. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping 1:M Relationships, cont’d Optional participation on the M side. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping 1:M Relationships, cont’d Rename a foreign key to better reflect the role of a relationship. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping M:N Relationships Use a bridge relation with two foreign keys. AKA: linking table or join table. Example: BELONGSTO Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping M:N Relationships, cont’d Optional participation on both sides. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping M:N Relationships, cont’d with an attribute. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping 1:1 Relationships Map 1:1 relationships similarly to 1:M relationships. One table will have a foreign key pointing to the primary key of the other table. It can be an arbitrary choice of which table has the foreign key. Make the choice that is most intuitive or efficient.
Mapping 1:1 Relationships, cont’d Table VEHICLE has the foreign key. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Referential Integrity Constraint The value of a foreign key must either: Match one of the values of the primary key in the referred table. Be null. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Example #1: ER Diagram Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Example #1: Relational Schema Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Example #1: Sample Data Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Candidate Keys Choose one of the candidate keys to be the primary key. Map the other as non-primary. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Candidate Keys, cont’d Generally choose a non-composite candidate key to be the primary key. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Multivalued Attributes Map the multivalued attribute as a new table. This becomes a 1:M relationship with the new table on the M side. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Derived Attributes Do not map derived attributes. Sample data records. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 As seen by the front-end application.
Mapping Example #2: Attributes Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Unary 1:M Relationships The table contains a foreign key that corresponds to its own primary key. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Unary M:N Relationships Use a bridge relation where both foreign keys refer to the primary key of the same table. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Unary 1:1 Relationships Map similarly to a unary 1:M relationship. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Multiple Relationships Map each relationship. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Weak Entities Map weak entities the same way you map regular entities. The resulting table has a composite primary key that is composed of: The partial identifier of the table. The foreign key corresponding to the primary key of the owner table.
Mapping Weak Entities, cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 The APARTMENT table’s composite primary key consists of AptNo (the partial key) and BuildingID (the foreign key corresponding the the primary key of the owner BUILDING table).
Mapping Example #3: ER Diagram Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Example #3: Relational Schema Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Example #3: Sample Data Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
Mapping Example #3: Sample Data, cont’d Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
ER Modeling and Relational Modeling Why not skip ER modeling and go directly to logical modeling (creating relational schemas)? ER modeling is better for visualizing requirements. Certain concepts can be visualized graphically only in an ER diagram. Every attribute appears only once in an ER diagram. A conceptual model (ER diagram) is better for communication and documentation.
Start the MariaDB Database Server
XAMPP Home Page phpMyAdmin is a PHP-based GUI interface to the MariaDB database server.
phpMyAdmin Home Page
Database Server Root Password By default, there is no root password. Set the root password using phpMyAdmin. Go to the Users accounts tab. Edit the root user at localhost to change its password.
Database Server Root Password, cont’d
Enable phpMyAdmin to Connect phpMyAdmin connects to the MariaDB database server as the root user. Now you must tell phpMyAdmin what the new root password is. Edit the configuration file XAMPP/xamppfiles/phpmyadmin/config.inc.php Edit the line $cfg['Servers'][$i]['password'] = 'root password'; For this class, use the database password sesame
Add a New Database User Name the user after yourself or your team. Click “Add user” on the Users tab. Use “localhost” for the host. Tick the checkbox to create a database with the same name.
Create a Table in the New Database
Insert Data into the Table
Browse the Table Contents
MySQL Command Line Interface ~: /Applications/XAMPP/xamppfiles/bin/mysql -u supercoders -p Enter password: mysql> show databases; +--------------------+ | Database | | information_schema | | supercoders | 2 rows in set (0.00 sec) mysql> use supercoders Database changed mysql> show tables; +-----------------------+ | Tables_in_supercoders | | people | 1 row in set (0.00 sec)
MySQL Command Line Interface, cont’d mysql> select * from people; +-----+--------+-------+--------+--------+ | id | first | last | gender | salary | | 101 | John | Adams | M | 120000 | | 102 | Mary | Smith | F | 155000 | | 105 | Helen | Troy | F | 75000 | | 110 | Albert | Jones | M | 160000 | 4 rows in set (0.00 sec) mysql> select * from people -> where first = 'Helen' -> and last = 'Troy'; +-----+-------+------+--------+--------+ | id | first | last | gender | salary | | 105 | Helen | Troy | F | 75000 | 1 row in set (0.00 sec) mysql>
Simple End-to-End Web Application! Client side A static HTML page the contains a form to submit a person’s first and last names. Server side PHP code that uses the first and last names to query a back end database. A dynamically generated web page that contains a table of the query results. Back end A database table containing people data. Demo
Default Web Page: index.html people/index.html <body> <form action="queryDB.php" method="get"> <fieldset> <legend>User input</legend> <p> <label>First name:</label> <input name="firstName" type="text" /> </p> <label>Last name:</label> <input name="lastName" type="text" /> <input type="submit" value="Submit" /> </fieldset> </form> </body>
PHP Page: queryDB.php queryDB.php Connect to the database and set $con <body> <h1>Query Results</h1> <p> <?php $first = filter_input(INPUT_GET, "firstName"); $last = filter_input(INPUT_GET, "lastName"); try { // Connect to the database. $con = new PDO("mysql:host=localhost;dbname=supercoders", "supercoders", "sesame"); $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); ... } catch(PDOException $ex) { echo 'ERROR: '.$ex->getMessage(); ?> </p> </body> queryDB.php Connect to the database and set $con to refer to the connection object. If an error occurs, throw an exception. PDO documentation: http://php.net/manual/en/book.pdo.php
PHP Page: queryDB.php, cont’d try { ... // Constrain the query if we got first and last names. if ((strlen($first) > 0) && (strlen($last) > 0)) { // Constrain the query if we got // first and last names. $query = "SELECT * FROM people ". "WHERE first = '$first' ". "AND last = '$last'"; } else { // Otherwise fetch all the rows. $query = "SELECT * FROM people"; // We're going to construct an HTML table. print "<table border='1'>\n"; }
PHP Page: queryDB.php, cont’d try { ... // Query the database. $data = $con->query($query); $data->setFetchMode(PDO::FETCH_ASSOC); } Query the database to get a result set $data. We will fetch rows from the result set as an associative array of name => value pairs.
PHP Page: queryDB.php, cont’d try { ... // Construct the HTML table row by row. // Start with a header row. $doHeader = true; foreach ($data as $row) { // The header row before the first data row. if ($doHeader) { print " <tr>\n"; foreach ($row as $name => $value) { print " <th>$name</th>\n"; } print " </tr>\n"; $doHeader = false; } } queryDB.php Loop over each $row of the result set $data. Loop over the name => value pairs of the first $row. Print the $name (the column name).
PHP Page: queryDB.php, cont’d try { ... // Data row. print " <tr>\n"; foreach ($row as $name => $value) { print " <td>$value</td>\n"; } print " </tr>\n"; } print " </table>\n"; } catch(PDOException $ex) { echo 'ERROR: '.$ex->getMessage(); } queryDB.php Loop over the name => value pairs of each $row and print the $value.