Chapter 3 Part2
Chapter 3 Part2
Content
3.1. How to design a database
3.2. How to using SQL to create a MySQL database
3.3. How to using SQL to work with a MySQL database
3.4. Professional PHP for working with MySQL
3.5. A database-driven website
C1, Slide 2
3.2. How to using SQL to create a MySQL
database
Objectives
Applied
1. Given the design for a database, create a SQL script that will
create the database, including all tables, primary keys, foreign key
constraints, and indexes.
2. Use SQL statements to create users and assign privileges to the
users.
3. Load data into a database table from a text file.
4. Dump a database to a SQL script.
C17, Slide 3
3.2. How to using SQL to create a MySQL
database
Objectives (continued)
Knowledge
1. Describe the use of the DDL statements for creating, altering, and
dropping databases, tables, and indexes.
2. Describe the column definitions for a table in terms of data types
and these attributes: unique, not null, default, primary key, auto-
increment, and references. Also, describe a table-level definition
for a primary key and a foreign key constraint.
3. Describe the use of the DDL statements for creating, renaming,
and dropping users and for assigning and revoking privileges.
4. Describe the process of loading data from a text file into a table,
and the process of dumping a database to a SQL script.
5. Describe the use of a script for creating a database.
C17, Slide 4
How to create a database
CREATE DATABASE my_guitar_shop2;
C17, Slide 5
Common numeric data types
INT[(size)]
TINYINT[(size)]
DECIMAL[(p[,s])]
C17, Slide 6
The syntax of the CREATE TABLE statement
CREATE TABLE [IF NOT EXISTS] tableName
(
columnName1 dataType [columnAttributes][,
columnName2 dataType [columnAttributes]][,
columnName3 dataType [columnAttributes]]...
)
C17, Slide 7
A table without column attributes
CREATE TABLE customers
(
customerID INT,
firstName VARCHAR(60),
lastName VARCHAR(60)
);
C17, Slide 8
Another table with column attributes
CREATE TABLE orders
(
orderID INT NOT NULL UNIQUE,
customerID INT NOT NULL,
orderNumber VARCHAR(50) NOT NULL,
orderDate DATE NOT NULL,
orderTotal DECIMAL(9,2) NOT NULL,
paymentTotal DECIMAL(9,2) DEFAULT 0
);
C17, Slide 9
Two column attributes for working
with a primary key
PRIMARY KEY
AUTO_INCREMENT
C17, Slide 10
A table with a two-column primary key
CREATE TABLE orderItems (
orderID INT NOT NULL,
productID INT NOT NULL,
itemPrice DECIMAL(10,2) NOT NULL,
discountAmount DECIMAL(10,2) NOT NULL,
quantity INT NOT NULL,
C17, Slide 11
Three attributes for working with a foreign key
CONSTRAINT
FOREIGN KEY
REFERENCES
C17, Slide 12
A table with a table-level foreign key
constraint
CREATE TABLE orders
(
orderID INT PRIMARY KEY,
customerID INT NOT NULL,
orderDate DATETIME NOT NULL,
CONSTRAINT ordersFkCustomers
FOREIGN KEY (customerID)
REFERENCES customers (customerID)
)
C17, Slide 13
An insert statement that fails
because a related row doesn’t exist
INSERT INTO orders
VALUES (1, 999, '2017-08-03')
C17, Slide 14
A statement that renames a table
ALTER TABLE products RENAME TO product;
C17, Slide 15
A statement that changes a column definition
ALTER TABLE customers MODIFY firstName VARCHAR(100) NOT NULL;
Warning
You should never alter a table or other database object in a
production database without first consulting the DBA.
C17, Slide 16
A statement that drops a table
DROP TABLE customers;
Warning
You should never drop a table in a production database without
first consulting the DBA, but you probably won’t have the
privileges for doing that.
C17, Slide 17
The syntax of the CREATE INDEX statement
CREATE [UNIQUE] INDEX|KEY indexName
ON tableName (columnName1 [ASC|DESC]
[, columnName2 [ASC|DESC]]...)
A statement that…
Creates an index based on a single column
CREATE INDEX customerID
ON orders (customerID);
Creates a unique index
CREATE UNIQUE INDEX emailAddress
ON customers (emailAddress);
Creates an index based on two columns
CREATE UNIQUE INDEX customerIDorderNumber
ON orders (customerID, orderNumber);
Creates an index that’s sorted in descending order
CREATE INDEX orderTotal
ON orders (orderTotal DESC);
C17, Slide 18
A CREATE TABLE statement that creates
indexes
CREATE TABLE customers (
customerID INT NOT NULL AUTO_INCREMENT,
emailAddress VARCHAR(255) NOT NULL,
firstName VARCHAR(60) NOT NULL,
C17, Slide 19
Privileges for working with data
SELECT
INSERT
UPDATE
DELETE
C17, Slide 20
Other privileges
CREATE USER
ALL [PRIVILEGES]
GRANT OPTION
USAGE
C17, Slide 21
The four privilege levels
Level Example
Global *.*
Database music_db.*
Table music_db.products
Column (listPrice) music_db.products
C17, Slide 22
How to create a user from a specific host
CREATE USER joel@localhost IDENTIFIED BY 'sesame';
C17, Slide 23
The syntax of the GRANT statement
GRANT privilegeList
ON [dbName.]table
TO userName1 [IDENTIFIED BY 'password1']
[, userName2 [IDENTIFIED BY 'password2'] ...]
[WITH GRANT OPTION]
C17, Slide 24
A statement that creates a user with no
privileges
GRANT USAGE
ON *.*
TO joel@localhost IDENTIFIED BY 'sesame';
C17, Slide 25
A statement that grants table privileges to a
user
GRANT SELECT, INSERT, UPDATE
ON my_guitar_shop2.products TO joel@localhost;
C17, Slide 26
The syntax of the REVOKE statement
for all privileges
REVOKE ALL[ PRIVILEGES], GRANT OPTION
FROM user [, user]
C17, Slide 27
The syntax of the REVOKE statement
for specific privileges
REVOKE privilegeList
ON [dbName.]table
FROM user [, user]
C17, Slide 28
A statement that lists all users
SELECT User, Host from mysql.user;
C17, Slide 29
The syntax of the SHOW GRANTS statement
SHOW GRANTS [FOR user]
C17, Slide 30
A statement that shows the privileges for a user
from a specific host
SHOW GRANTS FOR mgs_user@localhost;
C17, Slide 31
The Import tab for a table named products
C17, Slide 32
A tab-delimited text file that’s stored
in users.txt
1 John Smith [email protected]
2 Andrea Steelman [email protected]
3 Joel Murach [email protected]
C17, Slide 33
How to use the Windows command prompt
to load data from a text file
cd \xampp\mysql\bin
mysql -u root -p
Enter password: ******
use my_guitar_shop2;
load data local infile "c:/murach/products.txt"
into table products;
exit;
C17, Slide 34
The Export tab for my_guitar_shop2
C17, Slide 35
How to use phpMyAdmin to dump a database
1. Start phpMyAdmin, select the database, and click on the Export tab.
2. Set the options for the SQL script file.
3. Click on the Go button and save the file.
C17, Slide 36
The SQL script that creates
the my_guitar_shop2 database
-- create and select the database
DROP DATABASE IF EXISTS my_guitar_shop2;
CREATE DATABASE my_guitar_shop2;
USE my_guitar_shop2;
C17, Slide 37
The SQL script that creates the database
(cont.)
CREATE TABLE addresses (
addressID INT NOT NULL AUTO_INCREMENT,
customerID INT NOT NULL,
line1 VARCHAR(60) NOT NULL,
line2 VARCHAR(60) DEFAULT NULL,
city VARCHAR(40) NOT NULL,
state VARCHAR(2) NOT NULL,
zipCode VARCHAR(10) NOT NULL,
phone VARCHAR(12) NOT NULL,
disabled TINYINT(1) NOT NULL DEFAULT 0,
PRIMARY KEY (addressID),
INDEX customerID (customerID)
);
C17, Slide 38
The SQL script that creates the database
(cont.)
CREATE TABLE orders (
orderID INT NOT NULL AUTO_INCREMENT,
customerID INT NOT NULL,
orderDate DATETIME NOT NULL,
shipAmount DECIMAL(10,2) NOT NULL,
taxAmount DECIMAL(10,2) NOT NULL,
shipDate DATETIME DEFAULT NULL,
shipAddressID INT NOT NULL,
cardType INT NOT NULL,
cardNumber CHAR(16) NOT NULL,
cardExpires CHAR(7) NOT NULL,
billingAddressID INT NOT NULL,
PRIMARY KEY (orderID),
INDEX customerID (customerID)
);
C17, Slide 39
The SQL script that creates the database
(cont.)
CREATE TABLE orderItems (
itemID INT NOT NULL AUTO_INCREMENT,
orderID INT NOT NULL,
productID INT NOT NULL,
itemPrice DECIMAL(10,2) NOT NULL,
discountAmount DECIMAL(10,2) NOT NULL,
quantity INT NOT NULL,
PRIMARY KEY (itemID),
INDEX orderID (orderID),
INDEX productID (productID)
);
C17, Slide 40
The SQL script that creates the database
(cont.)
CREATE TABLE products (
productID INT NOT NULL AUTO_INCREMENT,
categoryID INT NOT NULL,
productCode VARCHAR(10) NOT NULL,
productName VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
listPrice DECIMAL(10,2) NOT NULL,
discountPercent DECIMAL(10,2) NOT NULL DEFAULT 0.00,
dateAdded DATETIME NOT NULL,
PRIMARY KEY (productID),
INDEX categoryID (categoryID),
UNIQUE INDEX productCode (productCode)
);
C17, Slide 41
The SQL script that creates the database
(cont.)
CREATE TABLE administrators (
adminID INT NOT NULL AUTO_INCREMENT,
emailAddress VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
firstName VARCHAR(255) NOT NULL,
lastName VARCHAR(255) NOT NULL,
PRIMARY KEY (adminID)
);
C17, Slide 42