PHP 09 MySQL
PHP 09 MySQL
and MySQL
Charles Severance
www.php-intro.com
Relational Databases
Relational databases model data by storing
rows and columns in tables. The power of the
relational database lies in its ability to
efficiently retrieve data from those tables and
in particular where there are multiple tables
and the relationships between those tables
involved in the query.
http://en.wikipedia.org/wiki/Relational_database
Terminology
• Database - Contains many tables
Rows /
Tuples
Tables / Relations
Application Structure
Application
End Database
Software (i.e. SQL
User Data Model
PHP)
SQL
Developer
Database
Tools (i.e.
DBA
phpMyAdmin)
SQL
• Structured Query Language is the language we use to issue commands
to the database
• Create a table
• Insert data
• Delete data
http://en.wikipedia.org/wiki/SQL
Common Database Systems
• Three Major Database Management Systems in wide use
• MySql - Simpler but very fast and scalable - commercial open source
• Macintosh
• Windows
• c:\xampp\mysql\bin\mysql.exe -u root -p
Your first MySQL Command
• Kind of like
• show databases;
If this does not work, stop and
figure out why.
DESCRIBE Users;
SQL Insert
• The limit can be a count or a starting row and count (starts from 0)
• You can request to receive the count of the rows that would be
retrieved instead of the rows
• Tables pretty much look like big fast programmable spreadsheet with
rows, columns, and commands
• The power comes when we have more than one table and we can
exploit the relationships between the tables
Looking at Data Types
• Numeric fields
• AUTO_INCREMENT fields
String Fields
• CHAR allocates entire space (faster for small strings where length is
known)
• TEXT up to 65K
• MEDIUMTEXT up to 16M
• LONGTEXT up to 4G
• Generally not used with indexing or sorting - and only then limited to a
prefix
Binary Types (rarely used)
• Character = 8 - 32 bits of information depending on character set
• TINYBLOB(n) - up to 255
• BLOB(n) - up to 65K
• MEDIUMBLOB(n) - up to 16M
• LONGBLOB(n) - up to 4G
http://www.youtube.com/watch?v=XhyRpvgm03g
Integer Numbers
• Numbers are very efficient, take little storage and are easy to process
because CPU's can compare them often with a single instruction
• Floating point numbers can represent a wide range of values but accuracy
is limited
• DATE - 'YYYY-MM-DD'
• TIME - 'HH:MM:SS'
DESCRIBE Users;
MySQL Functions
• Many operations in MySQL need to use the built-in functions (i.e. like
NOW() for dates)
• 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
Indexes
• As a table gets large (they always do) scanning all the data to find a
single row becomes very costly
• There are techniques to greatly shorten the scan as long as you create
data structures and maintain those structures - like shortcuts
• Hashes or Trees
MySQL Index Types
• PRIMARY KEY - Very little space, very very fast, exact match,
requires no duplicates, extremely fast for integer fields
• For large data sets, it is much faster to load your data into a table that
has no FULLTEXT index and then create the index than to load data
into a table that has an existing FULLTEXT index.
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_function
B-Trees
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 file systems.
http://en.wikipedia.org/wiki/B-tree
Specifying Indexes
ALTER TABLE Users ADD INDEX ( name ) USING BTREE
• SQL allows us to describe the shape of data to be stored and give many
hints to the database engine as to how we will be accessing or using the
data