CGS 2835 Interdisciplinary Web Development
Web
Database
Useful
Web Apps
+
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.
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
CGS 2835 Interdisciplinary Web Development
Database
Server
Web
ServerHTML REQUEST w/DATAHTML REQUEST w/DATA
SQL CommandSQL Command
PHP
CGS 2835 Interdisciplinary Web Development
Database
Server
Web
Server
PHP
data
PHP-created HTML
CGS 2835 Interdisciplinary Web Development
MySQL
Database
Server
PHPdata
PHP-created HTML
SQLSQL
Our Focus
CGS 2835 Interdisciplinary Web Development
Databases
CGS 2835 Interdisciplinary Web Development
File or Table
Database
• A collection of data organized to meet user’s
needs.
Records
(Entities)
Field
(Attribute)
CGS 2835 Interdisciplinary Web Development
Database Fields
• Fields are set to hold
specific types of data.
CGS 2835 Interdisciplinary Web Development
Database
A Database
is a
collection of
files/tables
CGS 2835 Interdisciplinary Web Development
Database Heirarchy
Table
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.
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
CGS 2835 Interdisciplinary Web Development
Primary Key
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
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
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!
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
CGS 2835 Interdisciplinary Web Development
MySQL
An open-source, relational database
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
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”
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).
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
CGS 2835 Interdisciplinary Web Development
When I click my database link in phpMyAdmin, I am prompted to create a table.
CGS 2835 Interdisciplinary Web Development
Then I create fields for my table.
CGS 2835 Interdisciplinary Web Development
I can then use the
Insert tab to enter
records into the table.
CGS 2835 Interdisciplinary Web Development
CGS 2835 Interdisciplinary Web Development
The browse buttons allows you to view the contents of the table.
CGS 2835 Interdisciplinary Web Development
CGS 2835 Interdisciplinary Web Development
You can also run SQL
queries on your data from
within phpMyAdmin.
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
CGS 2835 Interdisciplinary Web Development
MySQL & phpMyAdmin
• Either method of interacting with the database
requires knowledge of SQL
MySQL
Database
Server
SQLSQLSQLSQL
PHP

Database basics

  • 1.
    CGS 2835 InterdisciplinaryWeb Development Web Database Useful Web Apps +
  • 2.
    CGS 2835 InterdisciplinaryWeb 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 InterdisciplinaryWeb 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 InterdisciplinaryWeb Development Database Server Web ServerHTML REQUEST w/DATAHTML REQUEST w/DATA SQL CommandSQL Command PHP
  • 5.
    CGS 2835 InterdisciplinaryWeb Development Database Server Web Server PHP data PHP-created HTML
  • 6.
    CGS 2835 InterdisciplinaryWeb Development MySQL Database Server PHPdata PHP-created HTML SQLSQL Our Focus
  • 7.
    CGS 2835 InterdisciplinaryWeb Development Databases
  • 8.
    CGS 2835 InterdisciplinaryWeb Development File or Table Database • A collection of data organized to meet user’s needs. Records (Entities) Field (Attribute)
  • 9.
    CGS 2835 InterdisciplinaryWeb Development Database Fields • Fields are set to hold specific types of data.
  • 10.
    CGS 2835 InterdisciplinaryWeb Development Database A Database is a collection of files/tables
  • 11.
    CGS 2835 InterdisciplinaryWeb Development Database Heirarchy Table
  • 12.
    CGS 2835 InterdisciplinaryWeb 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 InterdisciplinaryWeb 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
  • 14.
    CGS 2835 InterdisciplinaryWeb Development Primary Key
  • 15.
    CGS 2835 InterdisciplinaryWeb 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 InterdisciplinaryWeb 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 InterdisciplinaryWeb 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 InterdisciplinaryWeb 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
  • 19.
    CGS 2835 InterdisciplinaryWeb Development MySQL An open-source, relational database
  • 20.
    CGS 2835 InterdisciplinaryWeb 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 InterdisciplinaryWeb 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 InterdisciplinaryWeb 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 InterdisciplinaryWeb 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 InterdisciplinaryWeb Development When I click my database link in phpMyAdmin, I am prompted to create a table.
  • 25.
    CGS 2835 InterdisciplinaryWeb Development Then I create fields for my table.
  • 26.
    CGS 2835 InterdisciplinaryWeb Development I can then use the Insert tab to enter records into the table.
  • 27.
  • 28.
    CGS 2835 InterdisciplinaryWeb Development The browse buttons allows you to view the contents of the table.
  • 29.
  • 30.
    CGS 2835 InterdisciplinaryWeb Development You can also run SQL queries on your data from within phpMyAdmin.
  • 31.
    CGS 2835 InterdisciplinaryWeb 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 InterdisciplinaryWeb Development MySQL & phpMyAdmin • Either method of interacting with the database requires knowledge of SQL MySQL Database Server SQLSQLSQLSQL PHP