PHP 08 MySQL
PHP 08 MySQL
http://en.wikipedia.org/wiki/Relational_database
Terminology
• Database - Contains many tables
• Relation (or table) - contains tuples and attributes
• Tuple (or row) - is a set of fields it generally represents
an “object” like a person or a music track
Rows /
Tuples
Tables / Relations
Two Roles in Large Projects
• Application Developer - Builds the logic for the
application, the look and feel of the application -
monitors the application for problems
SQL
Developer
Database
DBA Tools
Database Administrator
(dba)
A database administrator (DBA) is a person
responsible for the design, implementation,
maintenance and repair of an organization's
database. The role includes the development and
design of database strategies, monitoring and
improving database performance and capacity, and
planning for future expansion requirements. They
may also plan, co-ordinate and implement security
measures to safeguard the database.
http://en.wikipedia.org/wiki/Database_administrator
Database Model
A database model or database schema is
the structure or format of a database,
described in a formal language supported by
the database management system, In other
words, a "database model" is the application
of a data model when used in conjunctio
n with a database management system.
http://en.wikipedia.org/wiki/Database_model
SQL
• Structured Query Language is the language we use to
issue commands to the database
• Create a table
• Retrieve some data
• Insert data
• Delete data
http://en.wikipedia.org/wiki/SQL
Common Database Systems
• Three Major Database Management Systems in wide use
• MySQL - Simple fast and scalable - commercial open source
• Oracle - Large, commercial, enterprise-scale, very very
tweakable
• Command line
• Full Screen
Application Structure
SQL
Developer
Database
DBA Tools
Command Line (XAMPP)
• USE publications;
Application Structure
account / pw
End Application SQL
Database
User Software Data Model
account / pw SQL
Developer
Database
DBA Tools
Creating a Table
CREATE TABLE classics (
author VARCHAR(128),
title VARCHAR(128),
type VARCHAR(16), year CHAR(4))
ENGINE MyISAM;
DESCRIBE classics;
Data Types
http://www.youtube.com/watch?v=XhyRpvgm0
Numbers
32 bit
AUTO_INCREMENT
DROP TABLE classics;
• Often as we make
multiple tables and need CREATE TABLE classics (
to JOIN them together we id INT UNSIGNED NOT NULL
need an integer, primary AUTO_INCREMENT KEY,
key for each row so we author VARCHAR(128),
can efficiently add a title VARCHAR(128),
reference to a row in a type VARCHAR(16), year CHAR(4
table, in some other ENGINE MyISAM;
table as a foreign key
DESCRIBE classics;
Inserting...
INSERT INTO classics(author, title, type, year)
VALUES('Mark Twain','The Adventures of Tom Sawyer','Fiction','1876');INSERT INTO
classics(author, title, type, year)
VALUES('Jane Austen','Pride and Prejudice','Fiction','1811');INSERT INTO classics(author,
title, type, year)
VALUES('Charles Darwin','The Origin of Species','Non-Fiction','1856');INSERT INTO
classics(author, title, type, year)
VALUES('Charles Dickens','The Old Curiosity Shop','Fiction','1841');INSERT INTO
classics(author, title, type, year)
VALUES('William Shakespeare','Romeo and Juliet','Play','1594');
Inserted...
• Hashes or Trees
Hashes
A hash function is any algorithm or
subroutine that maps large data sets to
smaller data sets, called keys. For
example, a single integer can serve as an
index to an array (cf. associative array).
The values returned by a hash function
are called hash values, hash codes, hash
sums, checksums or simply hashes.
Hash functions are mostly used to
accelerate table lookup or data
comparison tasks such as finding items in
a database...
http://en.wikipedia.org/wiki/Hash_functio
B-Trees
In computer science, a B-tree is a tree data structure that keeps data sorted
and allows searches, sequential access, insertions, and deletions in
logarithmic amortized time. The B-tree is optimized for systems that read
and write large blocks of data. It is commonly used in databases and
filesystems.
http://en.wikipedia.org/wiki/B-tree
MySQL Index Types
• INDEX - Good for individual row lookup and sorting /
grouping results - works best with exact matches or
prefix lookups
mysql> SELECT title, author, year FROM classics WHERE year >
1850;+------------------------------+----------------+------+| title | author
| year |+------------------------------+----------------+------+| The Adventures of Tom
Sawyer | Mark Twain | 1876 || The Origin of Species | Charles Darwin |
1856 |+------------------------------+----------------+------+2 rows in set (0.00
sec)mysql>
The LIKE clause
• We can do wildcard matching in a WHERE clause using
the LIKE operator
mysql> SELECT title, author FROM classics WHERE title LIKE "%and%";
+---------------------+---------------------+| title | author |
+---------------------+---------------------+| Pride and Prejudice | Jane Austen ||
Romeo and Juliet | William Shakespeare |+---------------------+---------------------+2
rows in set (0.00 sec)mysql>
ORDER BY
mysql> SELECT title, author, year FROM classics ORDER BY year DESC;
+------------------------------+---------------------+------+| title | author
| year |+------------------------------+---------------------+------+| The Adventures of
Tom Sawyer | Mark Twain | 1876 || The Origin of Species | Charles
Darwin | 1856 || The Old Curiosity Shop | Charles Dickens | 1841 ||
Pride and Prejudice | Jane Austen | 1811 || Romeo and Juliet |
William Shakespeare | 1594 |+------------------------------+---------------------+------+5
rows in set (0.00 sec)mysql>
mysql> SELECT title, author, year FROM classics ORDER BY year DESC LIMIT
2;+------------------------------+----------------+------+| title | author | year |
+------------------------------+----------------+------+| The Adventures of Tom Sawyer | Mark Twain |
1876 || The Origin of Species | Charles Darwin | 1856 |+------------------------------
+----------------+------+2 rows in set (0.01 sec)
The LIMIT Clause
• The LIMIT clause can request the first "n" rows, or the first "n"
rows after some starting row. Note: the first row is zero, not one
mysql> SELECT title, author, year FROM classics ORDER BY year DESC LIMIT
1,2;+------------------------+-----------------+------+| title | author | year |
+------------------------+-----------------+------+| The Origin of Species | Charles Darwin | 1856 ||
The Old Curiosity Shop | Charles Dickens | 1841 |+------------------------+-----------------+------+2
rows in set (0.00 sec)
GROUP BY and COUNT
• We can get sub-counts of items where there are
duplicate values in a column
http://en.wikipedia.org/wiki/Join_(SQ
mysql> select * from tweet; mysql> select * from zips;+-------
+----------------+-------+| text | +-----------+-------+| zip | city |
zip |+----------------+-------+| Had state |+-------+-----------+-------+|
pizza | 48842 || Had Chi Dog | 48842 | Holt | MI || 48109 | Ann
48109 || Went to 572 | 48109 || Arbor | MI |+-------+-----------+-------
Coding at home | 48842 | +
+----------------+-------+
• http://dev.mysql.com/doc/refman/5.0/en/string-
functions.html
• http://dev.mysql.com/doc/refman/5.0/en/date-and-time-
functions.html
Summary