0% found this document useful (0 votes)
37 views

Chapter 03

This document provides an overview of relational databases and MySQL. It discusses using phpMyAdmin to view database structure, import SQL scripts, and run queries. Key topics covered include database tables, relationships between tables using primary and foreign keys, SELECT, INSERT, UPDATE and DELETE statements, and assigning user privileges in MySQL. The objectives are to use phpMyAdmin to manage databases and run basic SQL statements.

Uploaded by

Thanh Van Dao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Chapter 03

This document provides an overview of relational databases and MySQL. It discusses using phpMyAdmin to view database structure, import SQL scripts, and run queries. Key topics covered include database tables, relationships between tables using primary and foreign keys, SELECT, INSERT, UPDATE and DELETE statements, and assigning user privileges in MySQL. The objectives are to use phpMyAdmin to manage databases and run basic SQL statements.

Uploaded by

Thanh Van Dao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Chapter 3

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

• INT - You can specify a width of up to 11 digits.

• 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.

• DOUBLE(M,D) - A double precision 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 16,4, where 4 is the number of decimals. Decimal precision can go to 53
places for a DOUBLE. REAL is a synonym for DOUBLE.

• DECIMAL(M,D) - An unpacked floating-point number that cannot be unsigned. In


unpacked decimals, each decimal corresponds to one byte. Defining the display length (M)
and the number of decimals (D) is required. NUMERIC is a synonym for DECIMAL.

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.

• VARCHAR(M) - A variable-length string between 1 and 255 characters in length; for


example VARCHAR(25). You must define a length when creating a VARCHAR field.

• 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

• DATE - A date in YYYY-MM-DD format, between 1000-01-01 and 9999-12-31. For


example, December 30th, 1973 would be stored as 1973-12-30.

• DATETIME - A date and time combination in YYYY-MM-DD HH:MM:SS format, between


1000-01-01 00:00:00 and 9999-12-31 23:59:59. For example, 3:30 in the afternoon on
December 30th, 1973 would be stored as 1973-12-30 15:30:00.

• TIMESTAMP - A timestamp between midnight, January 1, 1970 and sometime in 2037.


This looks like the previous DATETIME format, only without the hyphens between
numbers; 3:30 in the afternoon on December 30th, 1973 would be stored as
19731230153000 ( YYYYMMDDHHMMSS ).

• TIME - Stores the time in HH:MM:SS format.

• 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] ...] ]

A SELECT statement that gets all columns


SELECT * FROM products
WHERE categoryID = 2

The result table

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] ...] ]

A statement that gets selected columns and rows


SELECT productName, listPrice
FROM products
WHERE listPrice < 500
ORDER BY listPrice ASC

The result table

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

The result table

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)

A statement that adds one row to a table


INSERT INTO products
(categoryID, productCode, productName, listPrice)
VALUES
(1, 'tele', 'Fender Telecaster', 599.00)

A statement that uses the MySQL NOW function


to get the current date
INSERT INTO orders (customerID, orderDate)
VALUES (1, NOW())

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

A statement that updates a column in one row


UPDATE products
SET productName =
'Ludwig 5-Piece Kit with Zildjian Cymbals'
WHERE productCode = 'ludwig'

A statement that updates multiple rows


UPDATE products
SET listPrice = 299
WHERE categoryID = 1

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

A statement that deletes one row from a table


DELETE FROM products
WHERE productID = 1

A statement that deletes multiple rows


DELETE FROM products
WHERE listPrice > 200

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

You might also like