2. CGS 2835 Interdisciplinary Web Development
The Value of Databases
• Databases and Database Management Systems (DBMS)
transform large quantities of data into specific and
valuable information for accomplishing some goal.
3. CGS 2835 Interdisciplinary Web Development
AMP
•Apache – open source Web server
•MySQL – open source database
•PHP – programming language that
works in HTML to provide
interactivity with MySQL
4. CGS 2835 Interdisciplinary Web Development
Database
Server
Web
ServerHTML REQUEST w/DATAHTML REQUEST w/DATA
SQL CommandSQL Command
PHP
8. CGS 2835 Interdisciplinary Web Development
File or Table
Database
• A collection of data organized to meet user’s
needs.
Records
(Entities)
Field
(Attribute)
9. CGS 2835 Interdisciplinary Web Development
Database Fields
• Fields are set to hold
specific types of data.
12. CGS 2835 Interdisciplinary Web Development
Keys and Primary Key
• Key: A field in a record that is used to identify
the record
• Primary key: A field that uniquely identifies a
record
– A primary key field prevents duplicate records
from occurring in a table.
13. CGS 2835 Interdisciplinary Web Development
Primary Keys
• User identification number
• Sometimes email addresses are used as a unique
login, assuming two people cannot have the same
email
• A number is faster to look up, so a unique number
is often still generated
15. CGS 2835 Interdisciplinary Web Development
The Relational Model
• In a relational database, tables are linked (related) through
common fields.
• This can remove redundant data, and organize information
16. CGS 2835 Interdisciplinary Web Development
“Flat” database
userID First Name Last Name Email, …, … Message
1 Geo Miller …, … , … Hello!
2 Ken Baldauf …, … , … Greetings, how
are you?
1 Geo Miller …, … , … Doing well
2 Ken Baldauf ……, … , … Great!
The Relational Model
17. CGS 2835 Interdisciplinary Web Development
“Relational”
databaseuserID First
Name
Last
Name
Email, …,
…
1 Geo Miller …, … , …
2 Ken Baldauf …, … , …
The Relational Model
userID Message
1 Hello!
2 Greetings, how
are you?
1 Doing well
2 Great!
18. CGS 2835 Interdisciplinary Web Development
Relation Types
• One-to-many
– Makes use of primary key
– Example: one person makes
many posts
• One-to-one
– A piece of data only relating to one
record
• Many-to-many
– An item can have many sellers
and sellers can sell that item
many times
20. CGS 2835 Interdisciplinary Web Development
MySQL - Installing
• Some servers require an administrator to install
• On your own machine, there are installation packages for
AMP
• On Codio:
– parts install mysql
– parts start mysql
• On many linux distributions:
– apt-get install mysql-server
– service mysql start
21. CGS 2835 Interdisciplinary Web Development
MySQL – Creating a Database
Creating a database in MySQL is traditionally done with a terminal that has
administrator access to the MySQL server.
CREATE DATABASE IF NOT EXISTS “databasename”
A MySQL user must be created and given access to use the new database
CREATE USER IF NOT EXISTS “username”
SET PASSWORD FOR “username”=PASSWORD(“password”)
GRANT ALL PRIVILEGES ON “databasename” TO “username”
IDENTIFIED BY “password”
22. CGS 2835 Interdisciplinary Web Development
phpMyAdmin
is a popular application
for managing mySQL
databases.
Here you can click a
database name to access
its properties and tools.
Note that whenever
prompted for charset or
collation select UTF-8
Unicode (utf8).
23. CGS 2835 Interdisciplinary Web Development
Access MySQL Database
We will use phpMyAdmin
to:
•Create tables in databases
•Create fields in the tables
•Manage the database
•Examine table data
We will use PHP to:
• Enter records into tables
from HTML forms
• Read records from tables
and output to HTML pages
24. CGS 2835 Interdisciplinary Web Development
When I click my database link in phpMyAdmin, I am prompted to create a table.
30. CGS 2835 Interdisciplinary Web Development
You can also run SQL
queries on your data from
within phpMyAdmin.
31. CGS 2835 Interdisciplinary Web Development
MySQL & phpMyAdmin
• phpMyAdmin is a useful tool for manipulating mySQL
databases; however, in order to work with Web-generated
data, the mySQL database must be accessed from the HTML
code using PHP.
MySQL
Database
Server
SQLSQLSQLSQL
PHP
32. CGS 2835 Interdisciplinary Web Development
MySQL & phpMyAdmin
• Either method of interacting with the database
requires knowledge of SQL
MySQL
Database
Server
SQLSQLSQLSQL
PHP