Buoi08.Master MySQL Programming (TT)
Buoi08.Master MySQL Programming (TT)
C17, Slide 1
3.2. How to use SQL to create a database
Objectives
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 2
3.2. How to use SQL to create a database
How to create a database
CREATE DATABASE my_guitar_shop2;
C17, Slide 3
3.2. How to use SQL to create a database
Common numeric data types
INT[(size)]
TINYINT[(size)]
DECIMAL[(p[,s])]
C17, Slide 4
3.2. How to use SQL to create a database
The syntax of the CREATE TABLE statement
CREATE TABLE [IF NOT EXISTS] tableName
(
columnName1 dataType [columnAttributes][,
columnName2 dataType [columnAttributes]][,
columnName3 dataType [columnAttributes]]...
)
C17, Slide 5
3.2. How to use SQL to create a database
A table without column attributes
CREATE TABLE customers
(
customerID INT,
firstName VARCHAR(60),
lastName VARCHAR(60)
);
C17, Slide 6
3.2. How to use SQL to create a database
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 7
3.2. How to use SQL to create a database
Two column attributes for working
with a primary key
PRIMARY KEY
AUTO_INCREMENT
C17, Slide 8
3.2. How to use SQL to create a database
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 9
3.2. How to use SQL to create a database
Three attributes for working with a foreign key
CONSTRAINT
FOREIGN KEY
REFERENCES
C17, Slide 10
3.2. How to use SQL to create a database
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 11
3.2. How to use SQL to create a database
An insert statement that fails
because a related row doesn’t exist
INSERT INTO orders
VALUES (1, 999, '2017-08-03')
C17, Slide 12
3.2. How to use SQL to create a database
A statement that renames a table
ALTER TABLE products RENAME TO product;
C17, Slide 13
3.2. How to use SQL to create a database
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 14
3.2. How to use SQL to create a database
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 15
3.2. How to use SQL to create a database
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 16
3.2. How to use SQL to create a database
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 17
3.2. How to use SQL to create a database
Privileges for working with data
SELECT
INSERT
UPDATE
DELETE
C17, Slide 18
3.2. How to use SQL to create a database
Other privileges
CREATE USER
ALL [PRIVILEGES]
GRANT OPTION
USAGE
C17, Slide 19
3.2. How to use SQL to create a database
The four privilege levels
Level Example
Global *.*
Database music_db.*
Table music_db.products
Column (listPrice) music_db.products
C17, Slide 20
3.2. How to use SQL to create a database
How to create a user from a specific host
CREATE USER joel@localhost IDENTIFIED BY 'sesame';
C17, Slide 21
3.2. How to use SQL to create a database
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 22
3.2. How to use SQL to create a database
A statement that creates a user with no privileges
GRANT USAGE
ON *.*
TO joel@localhost IDENTIFIED BY 'sesame';
C17, Slide 23
3.2. How to use SQL to create a database
A statement that grants table privileges to a user
GRANT SELECT, INSERT, UPDATE
ON my_guitar_shop2.products TO joel@localhost;
C17, Slide 24
3.2. How to use SQL to create a database
The syntax of the REVOKE statement
for all privileges
REVOKE ALL[ PRIVILEGES], GRANT OPTION
FROM user [, user]
C17, Slide 25
3.2. How to use SQL to create a database
C17, Slide 26
3.2. How to use SQL to create a database
A statement that lists all users
SELECT User, Host from mysql.user;
C17, Slide 27
3.2. How to use SQL to create a database
The syntax of the SHOW GRANTS statement
SHOW GRANTS [FOR user]
C17, Slide 28
3.2. How to use SQL to create a database
A statement that shows the privileges for a user
from a specific host
SHOW GRANTS FOR mgs_user@localhost;
C17, Slide 29
3.2. How to use SQL to create a database
The Import tab for a table named products
C17, Slide 30
3.2. How to use SQL to create a database
A tab-delimited text file that’s stored
in users.txt
1 John Smith jsmith@gmail.com
2 Andrea Steelman andi@murach.com
3 Joel Murach joelmurach@yahoo.com
C17, Slide 31
3.2. How to use SQL to create a database
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 32
3.2. How to use SQL to create a database
The Export tab for my_guitar_shop2
C17, Slide 33
3.2. How to use SQL to create a database
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 34
3.2. How to use SQL to create a database
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 35
3.2. How to use SQL to create a database
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 36
3.2. How to use SQL to create a database
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 37
3.2. How to use SQL to create a database
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 38
3.2. How to use SQL to create a database
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 39
3.2. How to use SQL to create a database
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 40
Q&A
C 16, Slide 41