RDBMS
RDBMS
Request
Client’s browser Server
Response
Query Pictures
Architecture of a web service
Server
Request
Client’s browser Data processor
Response
Data
LAMP stack
Apache server running on Linux
● Listens for connections on ports you specify
– Where do we specify?
– In apache2.conf file
● Makes publicly viewable content you put in the
var/www/ folder of your Linux box
– Be sure permissions are set correctly (755/644)
● We’ve already seen how to set this up, even
inside a virtual container
What’s the point of a database?
● Why can’t we just serve data from a csv file?
● Because then we’d have to load the whole file
into memory for every operation
– doesn’t scale well with data volume
● Relational database management systems
(RDBMS) are a popular variant
– Relational := related records
– Related via keys
RDBMS = data modeling
● Learning to work with
RDBMS
– Connection etc. (5%)
– CRUD operations
(10% effort)
– Data modeling (85%
effort)
● What makes for an
effective data model?
Data normalization
● How would you make queries on this
database?
First normal form
● Every cell should have one value only
● Every record (row) should be unique
Second normal form
● Primary key
– Must be unique and non-null
– Must auto-increment with records
– Cannot be changed
● Second normal form desiderata
– Be in 1st NF
– Single column primary key
Second normal form
Second normal form
Third normal form
● Be in 2NF
● Remove transitive functional dependencies
● Transitive functional dependencies
Third normal form
Database desiderata
● Databases must pass the ACID test
● Atomicity = in a transaction with two or more pieces
of information, either all are committed or none are
● Consistency = a transaction either creates a new
valid state, or changes nothing
● Isolation = an uncommitted transaction must stay in
isolation from other transactions
● Durability = committed data is saved by the system
so its available even in the event of a failure
An employee management system
● An employee
● Belongs to one of several departments
Common constraints: not null, unique, primary key, foreign key, check, default,
index
SQL with PHP
● A PHP script can be included anywhere within
an HTML document within the tags <?php ?>
● Have to use a .php extension for that file
● Syntax of PHP very much like C
● But it has a bunch of functions and (super)
global variables that simplify server-side
programming
● Most common server-side scripting language
Web form data entry with PHP
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
PHP+SQL
● Can use PHP to
– Connect to specific databases on your machine
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$db = “emp_db”
// Create connection
// Check connection
if ($conn->connect_error) {
?>
PHP+SQL
● Can use PHP to
– Execute SQL queries on the database tables
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .
$row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
Displaying results to the web
● PHP variables can be concatenated easily with
HTML strings to generate content dynamically
<?php
$txt1 = "SQL access ";
$txt2 = <data from SQL database>;
Server
Request
Client’s browser Data processor
Response
PHP+
mySQL
Data
In lab next week
● Learn how to connect to dbs on mysql shell and perform CRUD operations on
them
● Learn how to do the same thing using PHP
● I've posted a link to a nice tutorial for both on the website
● Build a web interface for the employees database such that
– I can query for employees by ID, by last name or by department
– I can identify which departments are the largest, by employee count
– I can display people within departments ordered by tenure with the company
– I can see the gender ratio of employees in any department
– I can see the gender pay ratio in any department
● Gender pay ratio = the ratio of female to male salary for the same job title
● Think about a different data set you could model for developing your own web
app for the coming week