Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

PHP 13

This document discusses databases and how they are used with server-side programming like PHP. It explains that databases provide persistent storage of data that is available to all pages, unlike variables that are only available within a single page. It also describes some key concepts about relational databases including tables, rows, columns, primary keys, and how tables can be connected through foreign keys. Finally, it covers data types that can be used in databases and some storage details.

Uploaded by

Bigu Marius Alin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

PHP 13

This document discusses databases and how they are used with server-side programming like PHP. It explains that databases provide persistent storage of data that is available to all pages, unlike variables that are only available within a single page. It also describes some key concepts about relational databases including tables, rows, columns, primary keys, and how tables can be connected through foreign keys. Finally, it covers data types that can be used in databases and some storage details.

Uploaded by

Bigu Marius Alin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Databases

PHP-13
Sorting data with PHP

Data store Persistance

Variable

Only available to code in the same page


$customer_name

Form variable

Can be passed from page to page. But needs to be passed on to be


$_POST['cust_name']
remembered
$_GET['cust_name']

Session variable
Available to all pages during the session as long as the browser window is
$_SESSION['cust_name'] open. But forgotten after the session

Cookie
Available to all pages and client-side code, even after browser window is
$_COOKIE['cust_name'] closed. Expiry date set in the code

Data available to all pages and remains stored until deleted. Can be stored
Database data
infinitely.
Server Side Programming
HTTP Request PHP

Top 10 Offers HTML

HTML

HTTP Response

A server side program can be included with the HTML file

Server Side Programming


HTTP Request PHP

Top 10 Offers HTML

HTML

HTTP Response

A server side program can be included with the HTML file

The PHP engine may also get information from a database

MySQL
Database
HTTP Request PHP

Top 10 Offers HTML

HTML

HTTP Response

• The database must be launched in addition to the


web server
MySQL
Database
• Most programming languages provide some way of storing data (semi-
permanently) on the computer system, usually as files

• Managing files manually is clumsy and complex

• Databases abstract the data storage process and take care of the details

• Databases provide a service to the code

• We can access a database from PHP


Database Technologies

• There are different kinds of database

• But the classic database is a relational database

• Everything we will learn about relational databases is independent of PHP


and will apply equally when accessing databases using other langauges

• The basics haven't changed since the 1960s


Relational Database
• Data is organised into one or more tables of columns and rows

• A unique key identifies each row

• Rows are also called records or tuples

• Columns are also called attributes

• Each table/relation represents one "entity type" (such as customer or product)

• The rows represent instances of that type of entity


member_id fname lname tel email

1 John Smith (087)123-4567 js@gmail.com

2 Ann Jones (087)555-1234 ann.j@google.com

3 Mike McGrath (086)-123-4567 mike@mcgrath.com

4 Fionn Murphy (083)666-9999 murph96@hotmail.com


member_id fname lname tel email

1 John Smith (087)123-4567 js@gmail.com

2 Ann Jones (087)555-1234 ann.j@google.com

Primary Key
3 Mike McGrath (086)-123-4567 mike@mcgrath.com

4 Fionn Murphy (083)666-9999 murph96@hotmail.com


member_id fname lname tel email

1 John Smith (087)123-4567 js@gmail.com

2 Ann Jones (087)555-1234 ann.j@google.com

3 Mike a record
McGrath (086)-123-4567 mike@mcgrath.com

4 Fionn Murphy (083)666-9999 murph96@hotmail.com


A database may have many tables
member_id fname lname tel email video_id title

1 Titanic
1 John Smith (087)123-4567 js@gmail.com
2 Men in Black

2 Ann Jones (087)555-1234 ann.j@google.com 3 Star Wars

4 Central do Brazil
3 Mike McGrath (086)-123-4567 mike@mcgrath.com
5 Young Offenders

4 Fionn Murphy (083)666-9999 murph96@hotmail.com


Tables

• A database may comprise many tables

• Connections can be made between tables by including the key from one
table to a row

• This is known as a foreign key


video_id title

Connections between tables 1 Titanic

2 Men in Black
member_id fname lname tel email
3 Star Wars

1 John Smith (087)123-4567 js@gmail.com 4 Central do Brazil

5 Young Offenders
2 Ann Jones (087)555-1234 ann.j@google.com

3 Mike McGrath (086)-123-4567 mike@mcgrath.com


rental member_id video_id

4 Fionn Murphy (083)666-9999 murph96@hotmail.com


1 2 5

Member 3 has rented video 1 2 3 1


video_id title

Connections between tables 1 Titanic

2 Men in Black
member_id fname lname tel email
3 Star Wars

1 John Smith (087)123-4567 js@gmail.com 4 Central do Brazil

5 Young Offenders
Foreign Key
2 Ann Jones (087)555-1234 ann.j@google.com

3 Mike McGrath (086)-123-4567 mike@mcgrath.com


rental member_id video_id

4 Fionn Murphy (083)666-9999 murph96@hotmail.com


1 2 5

Member 3 has rented video 1 2 3 1


Data Types

• In order to store data efficiently and accurately the database needs to


know in advance precisely what type of data will be used for each column
(attribute)
Type Value

INT An integer -2, 147, 483, 648 -2147483648 to 2147483647

A floating point number. The number of decimal places can be specified.

DECIMAL
DECIMAL(5,2) permits valuse from -999.99 to 999.99

DOUBLE A long double-precision floating point number

DATE A date in the format YYYY-MM-DD format

TIME A time in the HH:MM:SS format


DATETIME Combined date and time YYYY-MM-DD HH:MM:SS

YEAR A year between 1901-2155 (notice there are 256 possible values 1 byte)

A string of defined fixed length up to 255 characters long.

CHAR()
CHAR(50) will be 50 characters long, padded if necessary.

A string of variable length up to 255 characters long.

VARCHAR()
VARCHAR(50) can hold strings of up to 50 characters or less (no padding)

TEXT A string of up to 65535 characters long

BLOB A blob in binary raw data


A single string from a defined list

ENUM ENUM ("Peperoni", "Hawaiian", "Margerita") allows entry of only one of


those values

A string or multiple strings from a defined set. SET ("Cheese", "Chilli",


SET
"Chorizo") allows entry of only one or more of these strings
More Types

• Different database flavours may allow for more types

• It's worth checking the documentation for full details

• But they are typically just variations on those listed


Storage Minimum Value Minimum Value Maximum Value Maximum Value
Type
(Bytes) Signed Unsigned Signed Unsigned

TINYINT 1 -128 0 127 255

SMALLINT 2 -32768 0 32767 65535

MEDIUMINT 3 -8388608 0 8388607 16777215

INT 4 -2147483648 0 2147483647 4294967295


Modifiers

• Further consraints can be placed on the data in tables

• The database management systems will enforce these rules


Modifiers
Modifier Purpose

Requires each record contain some data for this


NOT NULL
column
Insists that records cannot duplicate any entry in this
UNIQUE
column
Automically generate a number than is one more than
AUTO_INCREMENT
the previous value for that column
Specifies a value to be used in the event that a record
DEFAULT
in inserted without it
Specifies that column (or colums) to be used as the
PRIMARY KEY
primary key
S.Q.L.

• We can communicate with the database management system using


Structured Query Language (SQL).

• This language for building, managing, and modifying relational databases


has changed little since the 1960s

• Consequently any SQL skills you develop will be valuable for a long time

• The cool kids pronounce it "sequel"


S.Q.L.

HTML Data

SQL Commands / Queries

Tables
S.Q.L.
It is also possible to issue commands directly to the DBMS using the
command line and SQL

mysql> CREATE TABLE IF NOT EXISTS products


(
id INT UNIQUE AUTO_INCREMENT,
code INT NOT NULL
);
S.Q.L.
XAMPP also provides a web front end to the DBMS that allows for direct
manipulation of the database

http://localhost/phpmyadmin
RETRO-VISION
PARTY LIKE IT'S 1999
customers
id fname sname tel email pword
1 John Smith 0871234567 js@gmail.com pw1234
2 Ann Jones 0875551234 ann.j@google.com password
3 Mike McGrath 0861234567 mike@mcgrath.com secret
4 Fionn Murphy 0836669999 murph96@hotmail.com hotty96
CREATE TABLE IF NOT EXISTS customers
(
id INT AUTO_INCREMENT PRIMARY KEY,
fname VARCHAR(25) NOT NULL,
sname VARCHAR(30) NOT NULL,
tel VARCHAR(10),
email VARCHAR(50) UNIQUE NOT NULL,
pword VARCHAR(30) NOT NULL
)
INSERT INTO customers (fname, sname, tel, email, pword)

VALUES ("John", "Smith", "0871234567", "js@gmail.com", "pw1234"),

("Ann", "Jones", "0875551234", "ann.j@google.com", "password"),

("Mike", "McGrath", "0861234567", "mike@mcgrath.com", "secret"),

("Fionn", "Murphy", "0836669999", "murphy96@hotmail.com", "hotty96");


dvds
id title theyear rating
1 Titanic 1997 12A
2 Men in Black 1997 PG
3 Star Wars 1977 12A
4 Central do Brazil 1998 18

CREATE TABLE IF NOT EXISTS dvds


(
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(50) NOT NULL,
theyear YEAR,
rating ENUM ("G", "PG", "12A", "15A", "16", "18") DEFAULT "18"
)
INSERT INTO dvds (title, theyear, rating)

VALUES ("Titanic", 1997, "12A"),

("Men in Black", 1997, "PG"),

("Star Wars", 1977, "12A");

INSERT INTO dvds (title)

VALUES ("Central do Brazil");


dvds
dvds

allowed to be empty. no other field is


dvds

18 went in by default
Updating fields
UPDATE dvds

SET theyear = 1998 WHERE title = "Central do Brazil";

• This will update the theyear field in every row that has "Central do
Brazil" in the title field

• If there is more than one it will update them all

• In this case, that would make sense


SELECT

• The SELECT commands generates a table that is a subset of the table it


works on
SELECT

SELECT * FROM dvds


SELECT

SELECT title FROM dvds


SELECT

SELECT title FROM dvds WHERE year = 1997


SELECT
fname sname
John Smith
Ann Jones
Mike McGrath
Fionn Murphy

SELECT fname, sname FROM customers;


DROP

• DROP removes a table or database

• DROP TABLE customers;

• DROP DATABASE sock_shop;

• Obviously DROP should be used with caution


Rentals Table
CREATE TABLE IF NOT EXISTS rentals
(
id INT AUTO_INCREMENT PRIMARY KEY,
cust INT NOT NULL,
disc INT NOT NULL UNIQUE, id cust disc thedate
thedate DATETIME
)
Rentals Table
CREATE TABLE IF NOT EXISTS rentals
(
id INT AUTO_INCREMENT PRIMARY KEY,
cust INT NOT NULL,
disc INT NOT NULL UNIQUE, id cust disc thedate
thedate DATETIME
)
Rentals Table
CREATE TABLE IF NOT EXISTS rentals
(
disc INT NOT NULL PRIMARY KEY,
cust INT NOT NULL, disc cust thedate
thedate DATETIME
)
Rentals Table
CREATE TABLE IF NOT EXISTS rentals
(
disc INT NOT NULL PRIMARY KEY,
cust INT NOT NULL, disc cust thedate
thedate DATETIME

?
)
Rentals Table
• The right way to do the table depends on whether or not we can to keep
track of historic rentals or just the current rentals

Rental History
Current rentals

• Rental is primary key


• Disc is primary key

• Disc is not unique • No need for other key


Rentals Table
CREATE TABLE IF NOT EXISTS rentals
(
id INT AUTO_INCREMENT PRIMARY KEY,
cust INT NOT NULL,
disc INT NOT NULL UNIQUE, id cust disc thedate
thedate DATETIME 1 3 5 01/01/2018
)
2 2 4 01/01/2018

3 1 5 15/03/2018

4 4 12 22/05/2018
JOIN
id name id colour
1 Tom
1 red
2 Dick
2 black
5 Harry
3 blue
6 Mary

• JOIN generates the Cartesian product of the tables


id name id name id colour
1 Tom 1 Tom 1 red
2 Dick 2 Dick 2 red
5 Harry 5 Harry 3 red
6 Mary 6 Mary 1 red

JOIN
1 Tom 2 black

id colour 2 Dick 3 black


5 Harry 1 black
1 red
6 Mary 2 black
2 black
1 Tom 3 blue
3 blue
2 Dick 1 blue
• JOIN generates the Cartesian 5 Harry 2 blue
product of the tables
6 Mary 3 blue
JOIN

• The Cartesian porduct of two tables is not useful

• But we can make useful selections from it


SELECT * from customers, rentals;
SELECT fname, sname, disc FROM customers, rentals;
SELECT fname, sname, disc FROM customers,
rentals
WHERE customers.id = rentals.cust;
SELECT fname, sname, title FROM customers, rentals, dvds
WHERE customers.id = rentals.cust AND rentals.disc = dvds.id;

You might also like