Chapter 03
Chapter 03
Introduction
to relational databases
and MySQL
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 1
Objectives
Applied
1. Use phpMyAdmin to review the data and structure of the tables in a
database, to import and run SQL scripts that create databases, and
to create users with limited privileges.
2. Code simple SELECT, INSERT, UPDATE, and DELETE
statements, and use phpMyAdmin to run them.
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 2
Objectives (continued)
Knowledge
1. Describe the structure of a database table.
2. Describe how the tables in a relational database are related using
these terms: primary key and foreign key.
3. Identify the three types of relationships that can exist between two
tables.
4. Describe the way the columns in a table are defined using these
terms: data type, NULL value, default value, and auto-increment
column.
5. Describe the use of SELECT statements, including the use of inner
joins.
6. Describe the use of INSERT, UPDATE, and DELETE statements.
7. Describe the way the creation of users and the assignment of
privileges affect how a MySQL database can be used.
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 3
A products table
Primary key Columns
Rows
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 4
The relationship between two tables in a database
Primary key
Foreign key
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 5
Terms
foreign key
one-to-many relationship
one-to-one relationship
many-to-many relationship
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 6
The columns of the products table
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 7
MySQL Numeric Data Types
from http://www.tutorialspoint.com/mysql/mysql-data-types.htm
• FLOAT(M,D) - A floating-point number that cannot be unsigned. You can define the
display length (M) and the number of decimals (D). This is not required and will default to
10,2, where 2 is the number of decimals and 10 is the total number of digits (including
decimals). Decimal precision can go to 24 places for a FLOAT.
8
MySQL String Data Types
from http://www.tutorialspoint.com/mysql/mysql-data-types.htm
• CHAR(M) - A fixed-length string between 1 and 255 characters in length (for example
CHAR(5)), right-padded with spaces to the specified length when stored. Defining a
length is not required, but the default is 1.
• BLOB or TEXT - A field with a maximum length of 65535 characters. BLOBs are "Binary
Large Objects" and are used to store large amounts of binary data, such as images or
other types of files. Fields defined as TEXT also hold large amounts of data; the
difference between the two is that sorts and comparisons on stored data are case
sensitive on BLOBs and are not case sensitive in TEXT fields. You do not specify a length
with BLOB or TEXT.
• ENUM - An enumeration, which is a fancy term for list. When defining an ENUM, you are
creating a list of items from which the value must be selected (or it can be NULL). For
example, if you wanted your field to contain "A" or "B" or "C", you would define your
ENUM as ENUM ('A', 'B', 'C') and only those values (or NULL) could ever populate that
field.
9
MySQL Date & Time Data Types
from http://www.tutorialspoint.com/mysql/mysql-data-types.htm
• YEAR(M) - Stores a year in 2-digit or 4-digit format. If the length is specified as 2 (for
example YEAR(2)), YEAR can be 1970 to 2069 (70 to 69). If the length is specified as 4,
YEAR can be 1901 to 2155. The default length is 4.
10
The SELECT statement syntax for all columns
SELECT *
FROM table-1
[WHERE selection-criteria]
[ORDER BY column-1 [ASC|DESC]
[, column-2 [ASC|DESC] ...] ]
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 11
The syntax for selected columns
SELECT column-1 [, column-2] ...
FROM table-1
[WHERE selection-criteria]
[ORDER BY column-1 [ASC|DESC]
[, column-2 [ASC|DESC] ...] ]
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 12
A statement that gets data from two related tables
SELECT categoryName, productName, listPrice
FROM categories
INNER JOIN products
ON categories.categoryID = products.categoryID
WHERE listPrice > 800
ORDER BY listPrice ASC
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 13
The syntax for the INSERT statement
INSERT INTO table-name [(column-list)]
VALUES (value-list)
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 14
The syntax for the UPDATE statement
UPDATE table-name
SET expression-1 [, expression-2] ...
WHERE selection-criteria
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 15
The syntax for the DELETE statement
DELETE FROM table-name
WHERE selection-criteria
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 16
A command-line client
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 17
A web-based client
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 18
The Welcome page
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 19
The Home and Logout buttons at the top
of the sidebar on most pages
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 20
How to change your password
Click the Home button (the house icon). Then, click the Change
Password link.
On the Change Password page, enter and re-enter your new
password, and click the Go button.
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 21
Running a SQL script that creates a database
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 22
A success message after importing a SQL script
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 23
How to import and run a SQL script
1. Click the Import tab, go to the “File to Import” section, click the
Browse button, and select the file that contains the script.
2. Click the Go button. This runs the script that’s in the file.
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 24
The Structure tab for the my_guitar_shop 1
database
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 25
The Browse tab for the categories table
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 26
The SQL tab with a statement ready to run
Murach's PHP and MySQL, C3 © 2014, Mike Murach & Associates, Inc. Slide 27