2. 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
3. AMP
Todayâs Dominant Web Architecture
⢠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
9. Database
⢠A collection of data organized to meet userâs
needs.
File or Table
Field
(Attribute)
Records
(Entities)
CGS 2835
Interdisciplinary Web Development
10. Database Fields
⢠Fields are set to hold
specific types of data.
CGS 2835
Interdisciplinary Web Development
13. 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
14. Primary Keys
Which field would act as the best
primary key?
CGS 2835
Interdisciplinary Web Development
16. The Relational Model
⢠In a relational database, tables are linked (related) through
common fields.
CGS 2835
Interdisciplinary Web Development
17. Relation Types
⢠One-to-many
â Most typical
â Makes use of primary key
⢠One-to-one
⢠Many-to-many
CGS 2835
Interdisciplinary Web Development
19. MySQL - Installation
⢠MySQL is included with AMP
⢠and installed on iSpace
http://localhost
CGS 2835
Interdisciplinary Web Development
http://tools.ci.fsu.edu
20. MySQL â Creating a Database
⢠For security reasons MySQL accounts are
created through MySQL Administrative Tools
⢠Creating a MySQL account is the only
database command that cannot be issued
from PHP code.
CGS 2835
Interdisciplinary Web Development
21. MySQL â Creating a Database
https://ispace-tools.cci.fsu.edu
CGS 2835
Interdisciplinary Web Development
22. You will need this
You will need this
information for
information for
use in your PHP
use in your PHP
Code
Code
Click Edit to manage the database
Click Edit to manage the database
with phpMyAdmin
with phpMyAdmin
CGS 2835
Interdisciplinary Web Development
23. phpMyAdmin
is a popular application
for managing mySQL
databases. Itâs provided
at tools.ci.fsu.edu and
also often included with
AMP.
Here you can click a
database name to access
its properties and tools.
You can even create new
databases from here.
Note that whenever
prompted for charset or
collation select UTF-8
Unicode (utf8).
CGS 2835
Interdisciplinary Web Development
24. Access MySQL Database
We will use phpMyAdmin
to:
We will use PHP to:
â˘Create our databases
â˘Create tables in the databases
â˘Create fields in the tables
â˘Manage the database
â˘Examine table data
⢠Enter records into tables
from HTML forms
⢠Read records from tables
and output to HTML pages
CGS 2835
Interdisciplinary Web Development
25. When I click my database link in phpMyAdmin, I am prompted to create a table.
CGS 2835
Interdisciplinary Web Development
26. Then I create fields for my table.
CGS 2835
Interdisciplinary Web Development
27. I can then use the
Insert tab to enter
records into the table.
CGS 2835
Interdisciplinary Web Development
31. You can also run SQL
queries on your data from
within phpMyAdmin.
CGS 2835
Interdisciplinary Web Development
32. 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
SQL
SQL
CGS 2835
SQL
SQL
Interdisciplinary Web Development
PHP
33. MySQL & phpMyAdmin
⢠Either method of interacting with the database
requires knowledge of SQL
MySQL
Database
Server
SQL
SQL
CGS 2835
SQL
SQL
Interdisciplinary Web Development
PHP
34. SQL â The Basics
CGS 2835
Interdisciplinary Web Development
35. Database Strengths
ďŽ The power of a database and DBMS lies in the
userâs ability to manipulate the data to turn up
useful information.
⢠Data can be sifted, sorted
and queried through the
use of data manipulation
languages.
CGS 2835
Interdisciplinary Web Development
36. Data Manipulation Language
⢠A Data Manipulation Language (DML) is a specific
language provided with the DBMS that allows people
and other database users to access, modify, and
make queries about data contained in the database,
and to generate reports.
⢠Structured Query Language (SQL): The most popular
DML.
â SELECT * FROM EMPLOYEE WHERE JOB_CLASSIFICATION = âC2â
CGS 2835
Interdisciplinary Web Development
37. SQL Commands
SELECT - extracts data from a database
UPDATE - updates data in a database
DELETE - deletes data from a database
INSERT INTO - inserts new data into a database
CREATE DATABASE - creates a new database
ALTER DATABASE - modifies a database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index
From www.w3schools.com/sql
CGS 2835
Interdisciplinary Web Development
38. Employees Table
SELECT
SELECT field_names(s)
FROM table_name
Employee_Id LastName
1 Baldauf
2 Svendson
3 Pettersen
4 Willis
5 Smith
FirstName
Ola
Jon
Kari
Carl
Jason
Address
120 Main St
3 Bogus Dr
2413 Sayer Ave
12 Bacon Cr
Examples:
SELECT LastName,FirstName FROM Employees
LastName
Baldauf
Svendson
Pettersen
Willis
Smith
SELECT * FROM Employees
Employee_Id LastName
1 Baldauf
2 Svendson
3 Pettersen
4 Willis
5 Smith
CGS 2835
FirstName
Ola
Jon
Kari
Carl
Jason
FirstName
Ola
Jon
Kari
Address
120 Main St
3 Bogus Dr
2413 Sayer Ave
City
Chicago
Tallahassee
Tallahassee
Carl
Jason
12 Bacon Cr
Atlanta
Interdisciplinary Web Development
City
Chicago
Tallahassee
Tallahassee
Atlanta
39. Employees Table
SELECT
Employee_Id LastName
1 Baldauf
2 Svendson
3 Pettersen
4 Willis
5 Smith
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
FirstName
Ola
Jon
Kari
Carl
Jason
Address
120 Main St
3 Bogus Dr
2413 Sayer Ave
12 Bacon Cr
Example
SELECT * FROM Employees WHERE LastName=âWillis'
Employee_Id LastName
4 Willis
CGS 2835
FirstName
Carl
Address
12 Bacon Cr
City
Atlanta
Interdisciplinary Web Development
City
Chicago
Tallahassee
Tallahassee
Atlanta
40. SELECT
Employees Table
Employee_Id LastName
1 Baldauf
2 Svendson
3 Pettersen
4 Willis
5 Smith
FirstName
Ola
Jon
Kari
Carl
Jason
Address
120 Main St
3 Bogus Dr
2413 Sayer Ave
12 Bacon Cr
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
AND/OR column_name operator value
Example
SELECT * FROM Employees WHERE LastName=âWillisâ OR
LastName=âPettersenâ
Employee_Id LastName
3 Pettersen
4 Willis
CGS 2835
FirstName
Kari
Carl
Address
City
2413 Sayer Ave Tallahassee
12 Bacon Cr
Atlanta
Interdisciplinary Web Development
City
Chicago
Tallahassee
Tallahassee
Atlanta
41. UPDATE
Employees Table
Employee_Id LastName
1 Baldauf
2 Svendson
3 Pettersen
4 Willis
5 Smith
FirstName
Ola
Jon
Kari
Carl
Jason
Address
120 Main St
3 Bogus Dr
2413 Sayer Ave
12 Bacon Cr
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Example
UPDATE Employees
SET Address=â2727 Monroe St', City=âTallahassee'
WHERE LastName=âSmith' AND FirstName=âJason'
Employee_Id LastName
1 Baldauf
2 Svendson
3 Pettersen
4 Willis
5 Smith
CGS 2835
FirstName
Ola
Address
120 Main St
City
Chicago
Jon
Kari
Carl
Jason
3 Bogus Dr
2413 Sayer Ave
12 Bacon Cr
2727 Monroe St
Tallahassee
Tallahassee
Atlanta
Tallahassee
Interdisciplinary Web Development
City
Chicago
Tallahassee
Tallahassee
Atlanta
42. INSERT INTO
Employees Table
Employee_Id LastName
1 Baldauf
2 Svendson
3 Pettersen
4 Willis
5 Smith
FirstName
Ola
Jon
Kari
Carl
Jason
Address
120 Main St
3 Bogus Dr
2413 Sayer Ave
12 Bacon Cr
City
Chicago
Tallahassee
Tallahassee
Atlanta
INSERT INTO table_name
(ColumnName1, ⌠, ColumnNameN )
VALUES (âdata1â, ⌠, âdataNâ)
Example
INSERT INTO Employees (LastName, FirstName, Address, City)
VALUES (âLarkinâ, âRobertâ, â34 W 7thâ, âAtlantaâ)
Employee_Id LastName
1 Baldauf
2 Svendson
3 Pettersen
4 Willis
5 Smith
6 Larkin
CGS 2835
FirstName
Ola
Jon
Kari
Address
120 Main St
3 Bogus Dr
2413 Sayer Ave
City
Chicago
Tallahassee
Tallahassee
Carl
Jason
Robert
12 Bacon Cr
Atlanta
34 W 7th
Atlanta
Interdisciplinary Web Development
43. DELETE
Employees Table
Employee_Id LastName
1 Baldauf
2 Svendson
3 Pettersen
4 Willis
5 Smith
FirstName
Ola
Jon
Kari
Carl
Jason
DELETE FROM table_name
WHERE some_column=some_value
Example
DELETE FROM Employees
WHERE LastName=âWillis' AND FirstName=âCarl'
Employee_Id LastName
1 Baldauf
2 Svendson
3 Pettersen
5 Smith
CGS 2835
Address
120 Main St
3 Bogus Dr
2413 Sayer Ave
12 Bacon Cr
FirstName
Ola
Address
120 Main St
Jon
Kari
Jason
3 Bogus Dr
Tallahassee
2413 Sayer Ave Tallahassee
2727 Monroe St Tallahassee
Interdisciplinary Web Development
City
Chicago
City
Chicago
Tallahassee
Tallahassee
Atlanta
45. Accessing a MySQL Database from PHP
First create a database, table, and fields using phpMyAdmin
1.Establish a connection to mySQL server
2.Select the database
3.Use mysql_query to issue SQL commands
CGS 2835
Interdisciplinary Web Development
46. 1. Establish a Connection
mysql_connect("2006.ispace.ci.fsu.edu", âglm04", âtm4bj9yc") or die(mysql_error());
CGS 2835
Interdisciplinary Web Development
47. 2. Select the Database
mysql_connect("2006.ispace.ci.fsu.edu", âglm04", âtm4bj9yc") or die(mysql_error());
mysql_select_db(âglm04_webdev') or die(mysql_error());
CGS 2835
Interdisciplinary Web Development
48. 3. Use mysql_query
to Issue Commands
mysql_connect("2006.ispace.ci.fsu.edu", âglm04", âtm4bj9yc") or die(mysql_error());
mysql_select_db(âglm04_webdev') or die(mysql_error());
mysql_query("INSERT INTO visitors
(name, email) VALUES('Timmy Mellowman', 'mellowman@fsu.edu' ) ")
or die(mysql_error());
CGS 2835
Interdisciplinary Web Development
49. 3. Use mysql_query
to Issue Commands
mysql_connect("2006.ispace.ci.fsu.edu", âglm04", âtm4bj9yc") or die(mysql_error());
mysql_select_db(âglm04_webdev') or die(mysql_error());
$result = mysql_query("SELECT * FROM visitors")
or die(mysql_error());
while($row = mysql_fetch_array( $result )){
echo â<p> Name: ".$row['name'] ."<br />";
echo "Email: ".$row['email'] ."<br />";
echo " Date: ".$row['date'] .â</p>";
}
CGS 2835
Interdisciplinary Web Development
50. Useful Resources
⢠Tizag PHP/MySQL Tutorial
â http://www.tizag.com/mysqlTutorial
⢠W3Schools
â PHP MySQL: http://www.w3schools.com/php/php_mysql_intro.asp
â SQL: http://www.w3schools.com/sql/default.asp
⢠MySQL Manual:
â http://dev.mysql.com/doc/refman/5.0/en
⢠PHP MySQL functions:
â http://us3.php.net/manual/en/ref.mysql.php
CGS 2835
Interdisciplinary Web Development