Top Banner
CGS 2835 Interdisciplinary Web Development Web Database Useful Web Apps +
50

Database

May 19, 2015

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Database

CGS 2835 Interdisciplinary Web Development

WebDatabaseUseful Web Apps

WebDatabaseUseful Web Apps

++

Page 2: Database

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.

Page 3: Database

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

Today’s Dominant Web Architecture

Page 4: Database

CGS 2835 Interdisciplinary Web Development

AMP

• LAMP (Linux AMP)• SAMP (Solaris AMP)• WAMP (Windows AMP)• MAMP (Mac AMP)• XAMPP (Cross Platform AMP + Perl)

Today’s Dominant Web Architecture

Page 5: Database

CGS 2835 Interdisciplinary Web Development

DatabaseServer

WebServerHTML REQUEST w/DATAHTML REQUEST w/DATA

http://iSpace.ci.fsu.edu/~username/mysite/index.html?name=geo

SQL CommandSQL Command

PHPPHP

Page 6: Database

CGS 2835 Interdisciplinary Web Development

DatabaseServer

WebServer

PHPPHP

data

PHP-created HTML

Page 7: Database

CGS 2835 Interdisciplinary Web Development

MySQLDatabaseServer

PHPPHPdata

PHP-created HTML

SQLSQL

Our Focus

Page 8: Database

CGS 2835 Interdisciplinary Web Development

Databases

Page 9: Database

CGS 2835 Interdisciplinary Web Development

File or Table

Database• A collection of data organized to meet user’s needs.

Records (Entities)

Field(Attribute)

Page 10: Database

CGS 2835 Interdisciplinary Web Development

Database Fields

• Fields are set to hold specific types of data.

Page 11: Database

CGS 2835 Interdisciplinary Web Development

Database

A Database is a

collection of files/tables

Page 12: Database

CGS 2835 Interdisciplinary Web Development

Database Heirarchy

Table

Page 13: Database

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.

Page 14: Database

CGS 2835 Interdisciplinary Web Development

Primary Keys

Which field would act as the best primary key?Which field would act as the best primary key?

Page 15: Database

CGS 2835 Interdisciplinary Web Development

Primary Key

Page 16: Database

CGS 2835 Interdisciplinary Web Development

The Relational Model

• In a relational database, tables are linked (related) through common fields.

Page 17: Database

CGS 2835 Interdisciplinary Web Development

Relation Types• One-to-many

– Most typical– Makes use of primary key

• One-to-one• Many-to-many

Page 18: Database

CGS 2835 Interdisciplinary Web Development

MySQL

An open-source, relational database ideal for use with PHP

Page 19: Database

CGS 2835 Interdisciplinary Web Development

MySQL - Installation• MySQL is included with AMP• and installed on iSpace http://tools.ci.fsu.edu

http://localhost

Page 20: Database

CGS 2835 Interdisciplinary Web Development

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.

Page 21: Database

CGS 2835 Interdisciplinary Web Development

https://ispace-tools.cci.fsu.edu

MySQL – Creating a Database

Page 22: Database

CGS 2835 Interdisciplinary Web Development

Click Edit to manage the database with phpMyAdmin

Click Edit to manage the database with phpMyAdmin

You will need this information for use in your PHP

Code

You will need this information for use in your PHP

Code

Page 23: Database

CGS 2835 Interdisciplinary Web Development

phpMyAdminis 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).

Page 24: Database

CGS 2835 Interdisciplinary Web Development

Access MySQL Database

We will use phpMyAdmin to:•Create our databases•Create tables in the 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

Page 25: Database

CGS 2835 Interdisciplinary Web Development

When I click my database link in phpMyAdmin, I am prompted to create a table.

Page 26: Database

CGS 2835 Interdisciplinary Web Development

Then I create fields for my table.

Page 27: Database

CGS 2835 Interdisciplinary Web Development

I can then use the Insert tab to enter records into the table.

Page 28: Database

CGS 2835 Interdisciplinary Web Development

Page 29: Database

CGS 2835 Interdisciplinary Web Development

The browse buttons allows you to view the contents of the table.

Page 30: Database

CGS 2835 Interdisciplinary Web Development

Page 31: Database

CGS 2835 Interdisciplinary Web Development

You can also run SQL queries on your data from within phpMyAdmin.

Page 32: Database

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.

MySQLDatabaseServer

SQLSQLSQLSQLPHPPHP

Page 33: Database

CGS 2835 Interdisciplinary Web Development

MySQL & phpMyAdmin

• Either method of interacting with the database requires knowledge of SQL

MySQLDatabaseServer

SQLSQLSQLSQLPHPPHP

Page 34: Database

CGS 2835 Interdisciplinary Web Development

SQL – The Basics

Page 35: Database

CGS 2835 Interdisciplinary Web Development

Database Strengths

• Data can be sifted, sorted and queried through the use of data manipulation languages.

The power of a database and DBMS lies in the user’s ability to manipulate the data to turn up useful information.

Page 36: Database

CGS 2835 Interdisciplinary Web Development

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”

Page 37: Database

CGS 2835 Interdisciplinary Web Development

SQL CommandsSELECT - extracts data from a databaseUPDATE - updates data in a databaseDELETE - deletes data from a databaseINSERT INTO - inserts new data into a database

CREATE DATABASE - creates a new databaseALTER DATABASE - modifies a databaseCREATE TABLE - creates a new tableALTER TABLE - modifies a tableDROP TABLE - deletes a tableCREATE INDEX - creates an index (search key)DROP INDEX - deletes an index

From www.w3schools.com/sql

Page 38: Database

CGS 2835 Interdisciplinary Web Development

SELECT SELECT field_names(s)FROM table_nameExamples:

SELECT LastName,FirstName FROM Employees

SELECT * FROM Employees

Employee_Id LastName FirstName Address City

1 Baldauf Ola 120 Main St Chicago

2 Svendson Jon 3 Bogus Dr Tallahassee

3 Pettersen Kari 2413 Sayer Ave Tallahassee

4 Willis Carl 12 Bacon Cr Atlanta

5 Smith Jason    

LastName FirstName

Baldauf Ola

Svendson Jon

Pettersen Kari

Willis Carl

Smith Jason

Employee_Id LastName FirstName Address City

1 Baldauf Ola 120 Main St Chicago

2 Svendson Jon 3 Bogus Dr Tallahassee

3 Pettersen Kari 2413 Sayer Ave Tallahassee

4 Willis Carl 12 Bacon Cr Atlanta

5 Smith Jason    

Employees Table

Page 39: Database

CGS 2835 Interdisciplinary Web Development

SELECT SELECT column_name(s)FROM table_nameWHERE column_name operator valueExample

SELECT * FROM Employees WHERE LastName=’Willis'

Employee_Id LastName FirstName Address City

1 Baldauf Ola 120 Main St Chicago

2 Svendson Jon 3 Bogus Dr Tallahassee

3 Pettersen Kari 2413 Sayer Ave Tallahassee

4 Willis Carl 12 Bacon Cr Atlanta

5 Smith Jason    

Employee_Id LastName FirstName Address City

4 Willis Carl 12 Bacon Cr Atlanta

Employees Table

Page 40: Database

CGS 2835 Interdisciplinary Web Development

SELECT SELECT column_name(s)FROM table_nameWHERE column_name operator valueAND/OR column_name operator valueExample

SELECT * FROM Employees WHERE LastName=’Willis’ OR LastName=‘Pettersen’

Employee_Id LastName FirstName Address City

1 Baldauf Ola 120 Main St Chicago

2 Svendson Jon 3 Bogus Dr Tallahassee

3 Pettersen Kari 2413 Sayer Ave Tallahassee

4 Willis Carl 12 Bacon Cr Atlanta

5 Smith Jason    

Employee_Id LastName FirstName Address City

3 Pettersen Kari 2413 Sayer Ave Tallahassee

4 Willis Carl 12 Bacon Cr Atlanta

Employees Table

Page 41: Database

CGS 2835 Interdisciplinary Web Development

UPDATEUPDATE table_nameSET column1=value, column2=value2,...WHERE some_column=some_valueExample

UPDATE EmployeesSET Address=’2727 Monroe St', City=’Tallahassee'WHERE LastName=’Smith' AND FirstName=’Jason'

Employee_Id LastName FirstName Address City

1 Baldauf Ola 120 Main St Chicago

2 Svendson Jon 3 Bogus Dr Tallahassee

3 Pettersen Kari 2413 Sayer Ave Tallahassee

4 Willis Carl 12 Bacon Cr Atlanta

5 Smith Jason    

Employees Table

Employee_Id LastName FirstName Address City

1 Baldauf Ola 120 Main St Chicago

2 Svendson Jon 3 Bogus Dr Tallahassee

3 Pettersen Kari 2413 Sayer Ave Tallahassee

4 Willis Carl 12 Bacon Cr Atlanta

5 Smith Jason 2727 Monroe St Tallahassee

Page 42: Database

CGS 2835 Interdisciplinary Web Development

INSERT INTOINSERT 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 FirstName Address City

1 Baldauf Ola 120 Main St Chicago

2 Svendson Jon 3 Bogus Dr Tallahassee

3 Pettersen Kari 2413 Sayer Ave Tallahassee

4 Willis Carl 12 Bacon Cr Atlanta

5 Smith Jason    

Employees Table

Employee_Id LastName FirstName Address City

1 Baldauf Ola 120 Main St Chicago

2 Svendson Jon 3 Bogus Dr Tallahassee

3 Pettersen Kari 2413 Sayer Ave Tallahassee

4 Willis Carl 12 Bacon Cr Atlanta

5 Smith Jason    

6 Larkin Robert 34 W 7th Atlanta

Page 43: Database

CGS 2835 Interdisciplinary Web Development

DELETEDELETE FROM table_nameWHERE some_column=some_valueExample

DELETE FROM EmployeesWHERE LastName=’Willis' AND FirstName=’Carl'

Employee_Id LastName FirstName Address City

1 Baldauf Ola 120 Main St Chicago

2 Svendson Jon 3 Bogus Dr Tallahassee

3 Pettersen Kari 2413 Sayer Ave Tallahassee

4 Willis Carl 12 Bacon Cr Atlanta

5 Smith Jason    

Employees Table

Employee_Id LastName FirstName Address City

1 Baldauf Ola 120 Main St Chicago

2 Svendson Jon 3 Bogus Dr Tallahassee

3 Pettersen Kari 2413 Sayer Ave Tallahassee

5 Smith Jason 2727 Monroe St Tallahassee

Page 44: Database

CGS 2835 Interdisciplinary Web Development

PHP > MySQL

Page 45: Database

CGS 2835 Interdisciplinary Web Development

Accessing a MySQL Database from PHP

First create a database, table, and fields using phpMyAdmin

1.Establish a connection to mySQL server2.Select the database3.Use mysql_query to issue SQL commands

Page 46: Database

CGS 2835 Interdisciplinary Web Development

1. Establish a Connectionmysql_connect("2006.ispace.ci.fsu.edu", ”glm04", ”tm4bj9yc") or die(mysql_error());

Page 47: Database

CGS 2835 Interdisciplinary Web Development

2. Select the Databasemysql_connect("2006.ispace.ci.fsu.edu", ”glm04", ”tm4bj9yc") or die(mysql_error());

mysql_select_db(’glm04_webdev') or die(mysql_error());

Page 48: Database

CGS 2835 Interdisciplinary Web Development

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', '[email protected]' ) ")or die(mysql_error());

Page 49: Database

CGS 2835 Interdisciplinary Web Development

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>";}

Page 50: Database

CGS 2835 Interdisciplinary Web Development

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