SlideShare une entreprise Scribd logo
1  sur  66
Chapter 7
Working with Databases
and MySQL
PHP Programming with MySQL
2nd
Edition
2PHP Programming with MySQL, 2nd Edition
Objectives
In this chapter, you will:
• Study the basics of databases and MySQL
• Work with MySQL databases
• Define database tables
• Modify user privileges
• Work with database records
• Work with phpMyAdmin
3PHP Programming with MySQL, 2nd Edition
Introduction to Databases
• A database is an ordered collection of
information from which a computer program
can quickly access information
• Each row in a database table is called a record
• A record in a database is a single complete set
of related information
• Each column in a database table is called a field
• Fields are the individual categories of
information stored in a record
4PHP Programming with MySQL, 2nd Edition
Introduction to Databases
(continued)
Figure 7-1 Employee directory database
5PHP Programming with MySQL, 2nd Edition
Introduction to Databases
(continued)
• A flat-file database stores information in a
single table
• A relational database stores information
across multiple related tables
6PHP Programming with MySQL, 2nd Edition
Understanding Relational
Databases
• Relational databases consist of one or more
related tables
• A primary table is the main table in a
relationship that is referenced by another table
• A related table (or “child table”) references a
primary table in a relational database
• A primary key is a field that contains a unique
identifier for each record in a primary table
7PHP Programming with MySQL, 2nd Edition
Understanding Relational
Databases (continued)
• A primary key is a type of index, which
identifies records in a database to make
retrievals and sorting faster
• A foreign key is a field in a related table that
refers to the primary key in a primary table
• Primary and foreign keys link records across
multiple tables in a relational database
8PHP Programming with MySQL, 2nd Edition
One-to-One Relationships
• A one-to-one relationship exists between two
tables when a related table contains exactly one
record for each record in the primary table
• Create one-to-one relationships to break
information into multiple, logical sets
• Information in the tables in a one-to-one
relationship can be placed within a single table
• Make the information in one of the tables
confidential and accessible only by certain
individuals
9PHP Programming with MySQL, 2nd Edition
One-to-One Relationships
(continued)
Figure 7-2 One-to-one relationship
10PHP Programming with MySQL, 2nd Edition
One-to-Many Relationship
• A one-to-many relationship exists in a
relational database when one record in a
primary table has many related records in a
related table
• Breaking tables into multiple related tables to
reduce redundant and duplicate information is
called normalization
• Provides a more efficient and less redundant
method of storing this information in a database
11PHP Programming with MySQL, 2nd Edition
One-to-Many Relationship
(continued)
Figure 7-3 Table with redundant information
12PHP Programming with MySQL, 2nd Edition
One-to-Many Relationship
(continued)
Figure 7-4 One-to-many relationship
13PHP Programming with MySQL, 2nd Edition
Many-to-Many Relationship
• A many-to-many relationship exists in a
relational database when many records in one
table are related to many records in another
table
• A junction table creates a one-to-many
relationship for each of the two tables in a
many-to-many relationship
• A junction table contains foreign keys from the
two tables
14PHP Programming with MySQL, 2nd Edition
Working with Database
Management Systems
• A database management system (or DBMS) is
an application or collection of applications used
to access and manage a database
• A schema is the structure of a database
including its tables, fields, and relationships
• A flat-file database management system is a
system that stores data in a flat-file format
• A relational database management system
(or RDBMS) is a system that stores data in a
relational format
15PHP Programming with MySQL, 2nd Edition
Working with Database
Management Systems (continued)
Figure 7-5 Many-to-many relationship
16PHP Programming with MySQL, 2nd Edition
Working with Database
Management Systems (continued)
• Important aspects of database management
systems:
– The structuring and preservation of the
database file
– Ensuring that data is stored correctly in a
database’s tables, regardless of the database
format
– Querying capability
17PHP Programming with MySQL, 2nd Edition
Working with Database
Management Systems (continued)
• A query is a structured set of instructions and
criteria for retrieving, adding, modifying, and
deleting database information
• Structured query language (or SQL) is a
standard data manipulation language used
among many database management systems
• Open database connectivity (or ODBC) allows
ODBC-compliant applications to access any
data source for which there is an ODBC driver
Getting Started with
MySQL
18PHP Programming with MySQL, 2nd Edition
• The MySQL Monitor is a command-line
program for manipulating MySQL databases
• Connect to the MySQL server using a
command-line connect
• Commands are entered at the mysql->
command prompt in the console window
19PHP Programming with MySQL, 2nd Edition
Logging in to MySQL
• Enter the following command:
mysql –h host –u user –p
• Two accounts are created:
– Anonymous user account allows login without
specifying a username or password
– root account (the primary administrative account
for MySQL) is created without a password
mysql –u root
• Log out with the exit or quit commands
20PHP Programming with MySQL, 2nd Edition
Logging in to MySQL (continued)
$ mysql –h php_db -u dongosselin -p[ENTER]
Enter password: **********[ENTER]
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 6611 to server version: 4.1.9-
nt
Type 'help;' or 'h' for help. Type 'c' to clear the
buffer.
mysql>
• Use the up and down arrow keys on the keyboard
to scroll through previously entered commands
21PHP Programming with MySQL, 2nd Edition
Logging in to MySQL (continued)
Figure 7-6 MySQL Monitor on a Windows platform
23PHP Programming with MySQL, 2nd Edition
Understanding MySQL Identifiers
• An alias is an alternate name used to refer to a
table or field in SQL statements
• The case sensitivity of database and table
identifiers depends on the operating system
– Not case sensitive on Windows platforms
– Case sensitive on UNIX/Linux systems
• MySQL stores each database in a directory of
the same name as the database identifier
• Field and index identifiers are case insensitive
on all platforms
24PHP Programming with MySQL, 2nd Edition
Understanding MySQL Identifiers
(continued)
• Identifiers that must be quoted using the
backtick, or single quote, character (`)are
– An identifier that includes any character except
standard alphanumeric characters, underscores
(_) or dollar signs ($)
– Any identifier that contains one or more space
characters
– An identifier that is a reserved word in MySQL
– An identifier made entirely of numeric digits
– An identifier that contains a backtick character
25PHP Programming with MySQL, 2nd Edition
Getting Help with MySQL
Commands
26PHP Programming with MySQL, 2nd Edition
Creating Databases
• Use the CREATE DATABASE statement to create
a new database:
mysql> CREATE DATABASE vehicle_fleet;[ENTER]
• To use a new database, select it by executing
the USE DATABASE statement
27PHP Programming with MySQL, 2nd Edition
Selecting a Database
• Use the DATABASE() function to return the
name of the currently active database
mysql> SELECT DATABASE();[ENTER]
• View the available databases using the SHOW
DATABASES statement
mysql> SHOW databases;[ENTER]
• Use the DROP DATABASE statement to remove
all tables and delete a database
mysql> DROP DATABASE database;
28PHP Programming with MySQL, 2nd Edition
Defining Database Tables
• Data types that are assigned to fields determine
how much storage space the computer allocates
for the data in the database
• Choose the smallest data type possible for each
field
29PHP Programming with MySQL, 2nd Edition
Defining Database Tables
(continued)
Creating Tables
• Use the CREATE TABLE statement to create a
new table and define the column names and
data types for each column
mysql> CREATE TABLE vehicles
(license VARCHAR(10), make VARCHAR(25),
model VARCHAR(50), miles FLOAT,
assigned_to VARCHAR(40));[ENTER]
30PHP Programming with MySQL, 2nd Edition
Viewing Table Structure
• Use the DESCRIBE table_name statement to
view the structure of the table
31PHP Programming with MySQL, 2nd Edition
Changing Table Field Names
• Use the ALTER TABLE to change the name of
an existing field in a table using the following
syntax
ALTER TABLE table_name ADD [COLUMN]
(column_name column_type [, column_name
column_type ...]);
• In MySQL Monitor, enter the following:
mysql> ALTER TABLE vehicles ADD COLUMN
(model_year INT);[ENTER]
32PHP Programming with MySQL, 2nd Edition
Modifying Column Types
• Use the ALTER TABLE to rename columns of
an existing field in a table using the following
syntax
ALTER TABLE table_name CHANGE [COLUMN]
column_name new_name column_type;
• In MySQL Monitor, enter the following:
mysql> ALTER TABLE vehicles CHANGE COLUMN
miles mileage FLOAT;[ENTER]
33PHP Programming with MySQL, 2nd Edition
Renaming Columns
• Use the ALTER TABLE to rename columns
using the following syntax
ALTER TABLE table_name MODIFY [COLUMN]
column_name column_type;
In MySQL Monitor, enter the following:
mysql> ALTER TABLE vehicles MODIFY COLUMN
model_year SMALLINT;[ENTER]
34PHP Programming with MySQL, 2nd Edition
Renaming Tables
• Use the ALTER TABLE to change the name of
an existing table using the following syntax
ALTER TABLE table_name RENAME [TO] new_name;
mysql> ALTER TABLE vehicles RENAME TO
company_cars;[ENTER]
35PHP Programming with MySQL, 2nd Edition
Removing Columns
• Use the ALTER TABLE to remove an existing
field from a table using the following syntax
ALTER TABLE table_name DROP [COLUMN]
column_name;
mysql> ALTER TABLE company_cars DROP COLUMN
assigned_to;[ENTER]
36PHP Programming with MySQL, 2nd Edition
Deleting Tables
• Execute the DROP TABLE statement to remove
all data and the table definition from a database
DROP TABLE table;
• In MySQL Monitor, enter the following at the
mysql> prompt:
mysql> DROP TABLE company_cars;[ENTER]
• You must be logged in as the root user or have
DROP privileges to delete a table.
37PHP Programming with MySQL, 2nd Edition
Modifying User Privileges
• Privileges are actions and operations a user
can perform with a table or a database
• For security purposes, user accounts should
only be assigned the minimum necessary
privileges to perform given tasks
38PHP Programming with MySQL, 2nd Edition
Modifying User Privileges
(continued)
39PHP Programming with MySQL, 2nd Edition
40PHP Programming with MySQL, 2nd Edition
Granting Privileges
• The syntax for the GRANT statement is:
GRANT privilege [(column)] [, privilege [(columns)]] ...
ON {table | * | *.* | database.*}
TO user [IDENTIFIED BY 'password'];
• The GRANT statement creates the user account
if it does not exist and assigns the specified
privileges
• If the user account already exists, the GRANT
statement just updates the privileges
41PHP Programming with MySQL, 2nd Edition
Revoking Privileges
• The syntax for the REVOKE statement is:
REVOKE privilege [(column)] [, privilege [(columns)]] ...
ON {table | * | *.* | database.*}
FROM user;
• The REVOKE ALL PRIVILEGES statement
removes all privileges from a user account for a
specified table or database
• You must be logged in with the root account or
have sufficient privileges to revoke privileges
from another user account
42PHP Programming with MySQL, 2nd Edition
Adding Records
• Use the INSERT statement to add individual
records to a table
• The syntax for the INSERT statement is:
INSERT INTO table_name (column1, column2, …)
VALUES(value1, value2, ...);
• The values entered in the VALUES list must
be in the same order in which you defined the
table fields
• Specify NULL in any fields for which you do not
have a value
43PHP Programming with MySQL, 2nd Edition
Adding Records (continued)
• In MySQL Monitor, enter the following code at
the mysql> prompt:
mysql> INSERT INTO company_cars(license,
model_year, make, model, mileage)
VALUES('CK-2987', 2009, 'Toyota',
'Corolla', 3508.4);[ENTER]
Adding Records (continued)
• The LOAD DATA statement, with the full path
and name of a local text file, is used to add
multiple records to a table
LOAD DATA INFILE 'file_path' INTO TABLE
table_name (column1, column2, …);
• Each record in the text file must be placed on a
separate line with a tab delimiter between each
field
44PHP Programming with MySQL, 2nd Edition
Adding Records (continued)
• If the column list is omitted, the values on each
line must be in the same order you defined the
table fields
• Use consecutive tabs with nothing between
them to designate a column with no value
• In MySQL Monitor, enter the following code at
the mysql> prompt:
mysql> LOAD DATA INFILE 'company_cars.txt'
INTO TABLE company_cars;[ENTER]
45PHP Programming with MySQL, 2nd Edition
Adding Records (continued)
• The optional FIELDS TERMINATED BY clause
of the LOAD DATA statement allows you to
change the field separator to a character such
as (~ or ,) instead of the default tab
character
• In MySQL Monitor, enter the following code at
the mysql> prompt:
mysql> LOAD DATA INFILE 'company_cars.txt‘
INTO TABLE company_cars;[ENTER]
46PHP Programming with MySQL, 2nd Edition
47PHP Programming with MySQL, 2nd Edition
Retrieving Records
• Use the SELECT statement to retrieve records
from a table:
SELECT criteria FROM table_name;
• Use the asterisk (*) wildcard with the SELECT
statement to retrieve all fields from a table
• To return multiple fields, separate field names
with a comma
48PHP Programming with MySQL, 2nd Edition
Retrieving Records (continued)
• In MySQL Monitor, enter the following code at
the mysql> prompt:
mysql> SELECT model, mileage FROM
company_cars;[ENTER]
Using Aggregate Functions
• Aggregate functions summarize data in record
sets rather than display the individual records
• The COUNT() function is unique in that
– The wildcard (*) can be used as a function
argument instead of a field name
– The keyword DISTINCT can be used after the
opening parentheses
• The DISTINCT keyword can also be used with
the SELECT statement to retrieve records with a
unique value in the WHERE clause
49PHP Programming with MySQL, 2nd Edition
Using Aggregate Functions
(continued)
• To retrieve aggregate values for groups of
records, use the GROUP BY clause and include
the fields that you use to group the records as
part of the query
• In MySQL Monitor, enter the following code at
the mysql> prompt:
mysql> SELECT model_year, AVG(mileage)
FROM company_cars GROUP BY
model_year;[ENTER]
50PHP Programming with MySQL, 2nd Edition
51PHP Programming with MySQL, 2nd Edition
Sorting Query Results
• Use the ORDER BY keyword with the SELECT
statement to perform an alphanumeric sort of the
results returned from a query
• In MySQL Monitor, enter the following code at
the mysql> prompt:
mysql> SELECT make, model FROM inventory
ORDER BY make, model;[ENTER]
52PHP Programming with MySQL, 2nd Edition
Sorting Query Results (continued)
• To perform a reverse sort, add the DESC
keyword after the name of the field by which
you want to perform the sort
• In MySQL Monitor, enter the following code at
the mysql> prompt:
mysql> SELECT make, model FROM
company_cars ORDER BY make DESC,
model;[ENTER]
53PHP Programming with MySQL, 2nd Edition
Filtering Query Results
• The criteria portion of the SELECT statement
determines which fields to retrieve from a table
• You can also specify which records to return by
using the WHERE keyword
• In MySQL Monitor, enter the following code at
the mysql> prompt:
mysql> SELECT * FROM inventory WHERE
make='Martin‘;[ENTER]
54PHP Programming with MySQL, 2nd Edition
Filtering Query Results (continued)
• Use the keywords AND and OR to specify more
detailed conditions about the records you want
to return
• In MySQL Monitor, enter the following code
using the AND keyword at the mysql> prompt:
mysql> SELECT * FROM company_cars
WHERE model_year=2007 AND
mileage<60000;[ENTER]
55PHP Programming with MySQL, 2nd Edition
Filtering Query Results (continued)
• In MySQL Monitor, enter the following code
using the OR keyword at the mysql> prompt:
mysql> SELECT * FROM company_cars
WHERE make='Toyota’ OR
make='Honda‘ ORDER BY mileage ;[ENTER]
56PHP Programming with MySQL, 2nd Edition
Updating Records
• To update records in a table, use the UPDATE
statement
• The syntax for the UPDATE statement is:
UPDATE table_name
SET column_name=value
WHERE condition;
– The UPDATE keyword specifies the name of the
table to update
– The SET keyword specifies the value to assign to
the fields in the records that match the condition
in the WHERE keyword
57PHP Programming with MySQL, 2nd Edition
Updating Records (continued)
• In MySQL Monitor, enter the following code
using the OR keyword at the mysql> prompt:
mysql> UPDATE company_cars SET mileage=368.2
WHERE make='Ford’ AND model='Fusion';
[ENTER]
58PHP Programming with MySQL, 2nd Edition
Deleting Records
• Use the DELETE statement to delete records in a
table
• The syntax for the DELETE statement is:
DELETE FROM table_name
WHERE condition;
• The DELETE statement deletes all records that
match the condition
• To delete all the records in a table, leave off the
WHERE keyword
59PHP Programming with MySQL, 2nd Edition
Deleting Records (continued)
• In MySQL Monitor, enter the following code at
the mysql> prompt:
mysql> DELETE FROM company_cars WHERE
model_year=2006 AND make='Honda'
AND model='Accord';[ENTER]
• To delete all records from a table, omit the
WHERE clause
60PHP Programming with MySQL, 2nd Edition
Summary
• A database is an ordered collection of
information from which a computer program can
quickly access information
• A record in a database is a single, complete set
of related information
• Fields are the individual categories of
information stored in a record
• A flat-file database stores information in a
single table
61PHP Programming with MySQL, 2nd Edition
Summary (continued)
• A relational database stores information across
multiple related tables
• A query is a structured set of instructions and
criteria for retrieving, adding, modifying, and
deleting database information
• Structured query language, or SQL
(pronounced sequel), is a standard data
manipulation language among many database
management systems
62PHP Programming with MySQL, 2nd Edition
Summary (continued)
• MySQL Monitor is a command-line program
that you use to manipulate MySQL databases
• To work with a database, you must first select it
by executing the USE DATEBASE statement
• You use the CREATE DATABASE statement to
create a new database
• To delete a database, you execute the DROP
DATABASE statement, which removes all tables
from the database and deletes the database
itself
63PHP Programming with MySQL, 2nd Edition
Summary (continued)
• The fields in a table also store data according to
type
• To keep your database from growing too large,
you should choose the smallest data type
possible for each field
• To create a table, you use the CREATE TABLE
statement, which specifies the table and column
names and the data type for each column
64PHP Programming with MySQL, 2nd Edition
Summary (continued)
• To modify a table, you use the ALTER TABLE
statement, which specifies the table being
changed and the change to make
• To delete a table, you execute the DROP TABLE
statement, which removes all data and the table
definition
• You use a GRANT statement to create user
accounts and assign privileges, which refer to
the operations that a user can perform with a
database
65PHP Programming with MySQL, 2nd Edition
Summary (continued)
• You use the REVOKE statement to take away
privileges from an existing user account for a
specified table or database
• You add individual records to a table with the
INSERT statement
• To add multiple records to a database, you use
the LOAD DATA statement with a local text file
that contains the records you want to add
66PHP Programming with MySQL, 2nd Edition
Summary (continued)
• You use the SELECT statement to retrieve
records from a table
• You use the ORDER BY keyword with the
SELECT statement to perform an alphanumeric
sort of the results returned from a query
• To perform a reverse sort, add the DESC
keyword after the name of the field by which you
want to perform the sort
67PHP Programming with MySQL, 2nd Edition
Summary (continued)
• You can specify which records to return from a
database by using the WHERE keyword
• You use the UPDATE statement to update
records in a table
• You use the DELETE statement to delete records
from a table
• The phpMyAdmin graphical tool simplifies the
tasks associated with creating and maintaining
databases and tables

Contenu connexe

Tendances (19)

Advanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsAdvanced MySQL Query Optimizations
Advanced MySQL Query Optimizations
 
DataBase Management System Lab File
DataBase Management System Lab FileDataBase Management System Lab File
DataBase Management System Lab File
 
Db2 Important questions to read
Db2 Important questions to readDb2 Important questions to read
Db2 Important questions to read
 
MySQL Replication Evolution -- Confoo Montreal 2017
MySQL Replication Evolution -- Confoo Montreal 2017MySQL Replication Evolution -- Confoo Montreal 2017
MySQL Replication Evolution -- Confoo Montreal 2017
 
Unit 5-lecture-3
Unit 5-lecture-3Unit 5-lecture-3
Unit 5-lecture-3
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 
Sqlmap
SqlmapSqlmap
Sqlmap
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
MySQL and its basic commands
MySQL and its basic commandsMySQL and its basic commands
MySQL and its basic commands
 
MYSQL-Database
MYSQL-DatabaseMYSQL-Database
MYSQL-Database
 
Introduction to php database connectivity
Introduction to php  database connectivityIntroduction to php  database connectivity
Introduction to php database connectivity
 
Impala SQL Support
Impala SQL SupportImpala SQL Support
Impala SQL Support
 
Hive
HiveHive
Hive
 
Pig
PigPig
Pig
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
Db2 performance tuning for dummies
Db2 performance tuning for dummiesDb2 performance tuning for dummies
Db2 performance tuning for dummies
 
PL/SQL New and Advanced Features for Extreme Performance
PL/SQL New and Advanced Features for Extreme PerformancePL/SQL New and Advanced Features for Extreme Performance
PL/SQL New and Advanced Features for Extreme Performance
 
Ibm db2
Ibm db2Ibm db2
Ibm db2
 
8. column oriented databases
8. column oriented databases8. column oriented databases
8. column oriented databases
 

Similaire à 9780538745840 ppt ch07

Active/Active Database Solutions with Log Based Replication in xDB 6.0
Active/Active Database Solutions with Log Based Replication in xDB 6.0Active/Active Database Solutions with Log Based Replication in xDB 6.0
Active/Active Database Solutions with Log Based Replication in xDB 6.0EDB
 
Overview of oracle database
Overview of oracle databaseOverview of oracle database
Overview of oracle databaseSamar Prasad
 
Overview of oracle database
Overview of oracle databaseOverview of oracle database
Overview of oracle databaseSamar Prasad
 
SQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredSQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredDanish Mehraj
 
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfytxjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfytWrushabhShirsat3
 
Oracle Database Overview
Oracle Database OverviewOracle Database Overview
Oracle Database Overviewhonglee71
 
Lecture 4-RDBMS.pptx
Lecture 4-RDBMS.pptxLecture 4-RDBMS.pptx
Lecture 4-RDBMS.pptxRUBAB79
 
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019Dave Stokes
 
Introduction To Maxtable
Introduction To MaxtableIntroduction To Maxtable
Introduction To Maxtablemaxtable
 
Access Data from XPages with the Relational Controls
Access Data from XPages with the Relational ControlsAccess Data from XPages with the Relational Controls
Access Data from XPages with the Relational ControlsTeamstudio
 
DEE 431 Introduction to Mysql Slide 3
DEE 431 Introduction to Mysql Slide 3DEE 431 Introduction to Mysql Slide 3
DEE 431 Introduction to Mysql Slide 3YOGESH SINGH
 
Database Systems Design, Implementation, and Management
Database Systems Design, Implementation, and ManagementDatabase Systems Design, Implementation, and Management
Database Systems Design, Implementation, and ManagementOllieShoresna
 
Apache Hadoop 1.1
Apache Hadoop 1.1Apache Hadoop 1.1
Apache Hadoop 1.1Sperasoft
 

Similaire à 9780538745840 ppt ch07 (20)

Active/Active Database Solutions with Log Based Replication in xDB 6.0
Active/Active Database Solutions with Log Based Replication in xDB 6.0Active/Active Database Solutions with Log Based Replication in xDB 6.0
Active/Active Database Solutions with Log Based Replication in xDB 6.0
 
Overview of oracle database
Overview of oracle databaseOverview of oracle database
Overview of oracle database
 
Overview of oracle database
Overview of oracle databaseOverview of oracle database
Overview of oracle database
 
SQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredSQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics Covered
 
unit-ii.pptx
unit-ii.pptxunit-ii.pptx
unit-ii.pptx
 
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfytxjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
 
Oracle Database Overview
Oracle Database OverviewOracle Database Overview
Oracle Database Overview
 
Lecture 4-RDBMS.pptx
Lecture 4-RDBMS.pptxLecture 4-RDBMS.pptx
Lecture 4-RDBMS.pptx
 
Fudcon talk.ppt
Fudcon talk.pptFudcon talk.ppt
Fudcon talk.ppt
 
Nosql data models
Nosql data modelsNosql data models
Nosql data models
 
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
 
Mysql tutorial 5257
Mysql tutorial 5257Mysql tutorial 5257
Mysql tutorial 5257
 
MySQL.pptx
MySQL.pptxMySQL.pptx
MySQL.pptx
 
Mysql tutorial
Mysql tutorialMysql tutorial
Mysql tutorial
 
Introduction To Maxtable
Introduction To MaxtableIntroduction To Maxtable
Introduction To Maxtable
 
Access Data from XPages with the Relational Controls
Access Data from XPages with the Relational ControlsAccess Data from XPages with the Relational Controls
Access Data from XPages with the Relational Controls
 
DEE 431 Introduction to Mysql Slide 3
DEE 431 Introduction to Mysql Slide 3DEE 431 Introduction to Mysql Slide 3
DEE 431 Introduction to Mysql Slide 3
 
Database Systems Design, Implementation, and Management
Database Systems Design, Implementation, and ManagementDatabase Systems Design, Implementation, and Management
Database Systems Design, Implementation, and Management
 
Apache Hadoop 1.1
Apache Hadoop 1.1Apache Hadoop 1.1
Apache Hadoop 1.1
 
MS ACCESS.pptx
MS ACCESS.pptxMS ACCESS.pptx
MS ACCESS.pptx
 

Plus de Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 

Plus de Terry Yoast (20)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 

Dernier

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 

Dernier (20)

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 

9780538745840 ppt ch07

  • 1. Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2nd Edition
  • 2. 2PHP Programming with MySQL, 2nd Edition Objectives In this chapter, you will: • Study the basics of databases and MySQL • Work with MySQL databases • Define database tables • Modify user privileges • Work with database records • Work with phpMyAdmin
  • 3. 3PHP Programming with MySQL, 2nd Edition Introduction to Databases • A database is an ordered collection of information from which a computer program can quickly access information • Each row in a database table is called a record • A record in a database is a single complete set of related information • Each column in a database table is called a field • Fields are the individual categories of information stored in a record
  • 4. 4PHP Programming with MySQL, 2nd Edition Introduction to Databases (continued) Figure 7-1 Employee directory database
  • 5. 5PHP Programming with MySQL, 2nd Edition Introduction to Databases (continued) • A flat-file database stores information in a single table • A relational database stores information across multiple related tables
  • 6. 6PHP Programming with MySQL, 2nd Edition Understanding Relational Databases • Relational databases consist of one or more related tables • A primary table is the main table in a relationship that is referenced by another table • A related table (or “child table”) references a primary table in a relational database • A primary key is a field that contains a unique identifier for each record in a primary table
  • 7. 7PHP Programming with MySQL, 2nd Edition Understanding Relational Databases (continued) • A primary key is a type of index, which identifies records in a database to make retrievals and sorting faster • A foreign key is a field in a related table that refers to the primary key in a primary table • Primary and foreign keys link records across multiple tables in a relational database
  • 8. 8PHP Programming with MySQL, 2nd Edition One-to-One Relationships • A one-to-one relationship exists between two tables when a related table contains exactly one record for each record in the primary table • Create one-to-one relationships to break information into multiple, logical sets • Information in the tables in a one-to-one relationship can be placed within a single table • Make the information in one of the tables confidential and accessible only by certain individuals
  • 9. 9PHP Programming with MySQL, 2nd Edition One-to-One Relationships (continued) Figure 7-2 One-to-one relationship
  • 10. 10PHP Programming with MySQL, 2nd Edition One-to-Many Relationship • A one-to-many relationship exists in a relational database when one record in a primary table has many related records in a related table • Breaking tables into multiple related tables to reduce redundant and duplicate information is called normalization • Provides a more efficient and less redundant method of storing this information in a database
  • 11. 11PHP Programming with MySQL, 2nd Edition One-to-Many Relationship (continued) Figure 7-3 Table with redundant information
  • 12. 12PHP Programming with MySQL, 2nd Edition One-to-Many Relationship (continued) Figure 7-4 One-to-many relationship
  • 13. 13PHP Programming with MySQL, 2nd Edition Many-to-Many Relationship • A many-to-many relationship exists in a relational database when many records in one table are related to many records in another table • A junction table creates a one-to-many relationship for each of the two tables in a many-to-many relationship • A junction table contains foreign keys from the two tables
  • 14. 14PHP Programming with MySQL, 2nd Edition Working with Database Management Systems • A database management system (or DBMS) is an application or collection of applications used to access and manage a database • A schema is the structure of a database including its tables, fields, and relationships • A flat-file database management system is a system that stores data in a flat-file format • A relational database management system (or RDBMS) is a system that stores data in a relational format
  • 15. 15PHP Programming with MySQL, 2nd Edition Working with Database Management Systems (continued) Figure 7-5 Many-to-many relationship
  • 16. 16PHP Programming with MySQL, 2nd Edition Working with Database Management Systems (continued) • Important aspects of database management systems: – The structuring and preservation of the database file – Ensuring that data is stored correctly in a database’s tables, regardless of the database format – Querying capability
  • 17. 17PHP Programming with MySQL, 2nd Edition Working with Database Management Systems (continued) • A query is a structured set of instructions and criteria for retrieving, adding, modifying, and deleting database information • Structured query language (or SQL) is a standard data manipulation language used among many database management systems • Open database connectivity (or ODBC) allows ODBC-compliant applications to access any data source for which there is an ODBC driver
  • 18. Getting Started with MySQL 18PHP Programming with MySQL, 2nd Edition • The MySQL Monitor is a command-line program for manipulating MySQL databases • Connect to the MySQL server using a command-line connect • Commands are entered at the mysql-> command prompt in the console window
  • 19. 19PHP Programming with MySQL, 2nd Edition Logging in to MySQL • Enter the following command: mysql –h host –u user –p • Two accounts are created: – Anonymous user account allows login without specifying a username or password – root account (the primary administrative account for MySQL) is created without a password mysql –u root • Log out with the exit or quit commands
  • 20. 20PHP Programming with MySQL, 2nd Edition Logging in to MySQL (continued) $ mysql –h php_db -u dongosselin -p[ENTER] Enter password: **********[ENTER] Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 6611 to server version: 4.1.9- nt Type 'help;' or 'h' for help. Type 'c' to clear the buffer. mysql> • Use the up and down arrow keys on the keyboard to scroll through previously entered commands
  • 21. 21PHP Programming with MySQL, 2nd Edition Logging in to MySQL (continued) Figure 7-6 MySQL Monitor on a Windows platform
  • 22. 23PHP Programming with MySQL, 2nd Edition Understanding MySQL Identifiers • An alias is an alternate name used to refer to a table or field in SQL statements • The case sensitivity of database and table identifiers depends on the operating system – Not case sensitive on Windows platforms – Case sensitive on UNIX/Linux systems • MySQL stores each database in a directory of the same name as the database identifier • Field and index identifiers are case insensitive on all platforms
  • 23. 24PHP Programming with MySQL, 2nd Edition Understanding MySQL Identifiers (continued) • Identifiers that must be quoted using the backtick, or single quote, character (`)are – An identifier that includes any character except standard alphanumeric characters, underscores (_) or dollar signs ($) – Any identifier that contains one or more space characters – An identifier that is a reserved word in MySQL – An identifier made entirely of numeric digits – An identifier that contains a backtick character
  • 24. 25PHP Programming with MySQL, 2nd Edition Getting Help with MySQL Commands
  • 25. 26PHP Programming with MySQL, 2nd Edition Creating Databases • Use the CREATE DATABASE statement to create a new database: mysql> CREATE DATABASE vehicle_fleet;[ENTER] • To use a new database, select it by executing the USE DATABASE statement
  • 26. 27PHP Programming with MySQL, 2nd Edition Selecting a Database • Use the DATABASE() function to return the name of the currently active database mysql> SELECT DATABASE();[ENTER] • View the available databases using the SHOW DATABASES statement mysql> SHOW databases;[ENTER] • Use the DROP DATABASE statement to remove all tables and delete a database mysql> DROP DATABASE database;
  • 27. 28PHP Programming with MySQL, 2nd Edition Defining Database Tables • Data types that are assigned to fields determine how much storage space the computer allocates for the data in the database • Choose the smallest data type possible for each field
  • 28. 29PHP Programming with MySQL, 2nd Edition Defining Database Tables (continued)
  • 29. Creating Tables • Use the CREATE TABLE statement to create a new table and define the column names and data types for each column mysql> CREATE TABLE vehicles (license VARCHAR(10), make VARCHAR(25), model VARCHAR(50), miles FLOAT, assigned_to VARCHAR(40));[ENTER] 30PHP Programming with MySQL, 2nd Edition
  • 30. Viewing Table Structure • Use the DESCRIBE table_name statement to view the structure of the table 31PHP Programming with MySQL, 2nd Edition
  • 31. Changing Table Field Names • Use the ALTER TABLE to change the name of an existing field in a table using the following syntax ALTER TABLE table_name ADD [COLUMN] (column_name column_type [, column_name column_type ...]); • In MySQL Monitor, enter the following: mysql> ALTER TABLE vehicles ADD COLUMN (model_year INT);[ENTER] 32PHP Programming with MySQL, 2nd Edition
  • 32. Modifying Column Types • Use the ALTER TABLE to rename columns of an existing field in a table using the following syntax ALTER TABLE table_name CHANGE [COLUMN] column_name new_name column_type; • In MySQL Monitor, enter the following: mysql> ALTER TABLE vehicles CHANGE COLUMN miles mileage FLOAT;[ENTER] 33PHP Programming with MySQL, 2nd Edition
  • 33. Renaming Columns • Use the ALTER TABLE to rename columns using the following syntax ALTER TABLE table_name MODIFY [COLUMN] column_name column_type; In MySQL Monitor, enter the following: mysql> ALTER TABLE vehicles MODIFY COLUMN model_year SMALLINT;[ENTER] 34PHP Programming with MySQL, 2nd Edition
  • 34. Renaming Tables • Use the ALTER TABLE to change the name of an existing table using the following syntax ALTER TABLE table_name RENAME [TO] new_name; mysql> ALTER TABLE vehicles RENAME TO company_cars;[ENTER] 35PHP Programming with MySQL, 2nd Edition
  • 35. Removing Columns • Use the ALTER TABLE to remove an existing field from a table using the following syntax ALTER TABLE table_name DROP [COLUMN] column_name; mysql> ALTER TABLE company_cars DROP COLUMN assigned_to;[ENTER] 36PHP Programming with MySQL, 2nd Edition
  • 36. Deleting Tables • Execute the DROP TABLE statement to remove all data and the table definition from a database DROP TABLE table; • In MySQL Monitor, enter the following at the mysql> prompt: mysql> DROP TABLE company_cars;[ENTER] • You must be logged in as the root user or have DROP privileges to delete a table. 37PHP Programming with MySQL, 2nd Edition
  • 37. Modifying User Privileges • Privileges are actions and operations a user can perform with a table or a database • For security purposes, user accounts should only be assigned the minimum necessary privileges to perform given tasks 38PHP Programming with MySQL, 2nd Edition
  • 38. Modifying User Privileges (continued) 39PHP Programming with MySQL, 2nd Edition
  • 39. 40PHP Programming with MySQL, 2nd Edition Granting Privileges • The syntax for the GRANT statement is: GRANT privilege [(column)] [, privilege [(columns)]] ... ON {table | * | *.* | database.*} TO user [IDENTIFIED BY 'password']; • The GRANT statement creates the user account if it does not exist and assigns the specified privileges • If the user account already exists, the GRANT statement just updates the privileges
  • 40. 41PHP Programming with MySQL, 2nd Edition Revoking Privileges • The syntax for the REVOKE statement is: REVOKE privilege [(column)] [, privilege [(columns)]] ... ON {table | * | *.* | database.*} FROM user; • The REVOKE ALL PRIVILEGES statement removes all privileges from a user account for a specified table or database • You must be logged in with the root account or have sufficient privileges to revoke privileges from another user account
  • 41. 42PHP Programming with MySQL, 2nd Edition Adding Records • Use the INSERT statement to add individual records to a table • The syntax for the INSERT statement is: INSERT INTO table_name (column1, column2, …) VALUES(value1, value2, ...); • The values entered in the VALUES list must be in the same order in which you defined the table fields • Specify NULL in any fields for which you do not have a value
  • 42. 43PHP Programming with MySQL, 2nd Edition Adding Records (continued) • In MySQL Monitor, enter the following code at the mysql> prompt: mysql> INSERT INTO company_cars(license, model_year, make, model, mileage) VALUES('CK-2987', 2009, 'Toyota', 'Corolla', 3508.4);[ENTER]
  • 43. Adding Records (continued) • The LOAD DATA statement, with the full path and name of a local text file, is used to add multiple records to a table LOAD DATA INFILE 'file_path' INTO TABLE table_name (column1, column2, …); • Each record in the text file must be placed on a separate line with a tab delimiter between each field 44PHP Programming with MySQL, 2nd Edition
  • 44. Adding Records (continued) • If the column list is omitted, the values on each line must be in the same order you defined the table fields • Use consecutive tabs with nothing between them to designate a column with no value • In MySQL Monitor, enter the following code at the mysql> prompt: mysql> LOAD DATA INFILE 'company_cars.txt' INTO TABLE company_cars;[ENTER] 45PHP Programming with MySQL, 2nd Edition
  • 45. Adding Records (continued) • The optional FIELDS TERMINATED BY clause of the LOAD DATA statement allows you to change the field separator to a character such as (~ or ,) instead of the default tab character • In MySQL Monitor, enter the following code at the mysql> prompt: mysql> LOAD DATA INFILE 'company_cars.txt‘ INTO TABLE company_cars;[ENTER] 46PHP Programming with MySQL, 2nd Edition
  • 46. 47PHP Programming with MySQL, 2nd Edition Retrieving Records • Use the SELECT statement to retrieve records from a table: SELECT criteria FROM table_name; • Use the asterisk (*) wildcard with the SELECT statement to retrieve all fields from a table • To return multiple fields, separate field names with a comma
  • 47. 48PHP Programming with MySQL, 2nd Edition Retrieving Records (continued) • In MySQL Monitor, enter the following code at the mysql> prompt: mysql> SELECT model, mileage FROM company_cars;[ENTER]
  • 48. Using Aggregate Functions • Aggregate functions summarize data in record sets rather than display the individual records • The COUNT() function is unique in that – The wildcard (*) can be used as a function argument instead of a field name – The keyword DISTINCT can be used after the opening parentheses • The DISTINCT keyword can also be used with the SELECT statement to retrieve records with a unique value in the WHERE clause 49PHP Programming with MySQL, 2nd Edition
  • 49. Using Aggregate Functions (continued) • To retrieve aggregate values for groups of records, use the GROUP BY clause and include the fields that you use to group the records as part of the query • In MySQL Monitor, enter the following code at the mysql> prompt: mysql> SELECT model_year, AVG(mileage) FROM company_cars GROUP BY model_year;[ENTER] 50PHP Programming with MySQL, 2nd Edition
  • 50. 51PHP Programming with MySQL, 2nd Edition Sorting Query Results • Use the ORDER BY keyword with the SELECT statement to perform an alphanumeric sort of the results returned from a query • In MySQL Monitor, enter the following code at the mysql> prompt: mysql> SELECT make, model FROM inventory ORDER BY make, model;[ENTER]
  • 51. 52PHP Programming with MySQL, 2nd Edition Sorting Query Results (continued) • To perform a reverse sort, add the DESC keyword after the name of the field by which you want to perform the sort • In MySQL Monitor, enter the following code at the mysql> prompt: mysql> SELECT make, model FROM company_cars ORDER BY make DESC, model;[ENTER]
  • 52. 53PHP Programming with MySQL, 2nd Edition Filtering Query Results • The criteria portion of the SELECT statement determines which fields to retrieve from a table • You can also specify which records to return by using the WHERE keyword • In MySQL Monitor, enter the following code at the mysql> prompt: mysql> SELECT * FROM inventory WHERE make='Martin‘;[ENTER]
  • 53. 54PHP Programming with MySQL, 2nd Edition Filtering Query Results (continued) • Use the keywords AND and OR to specify more detailed conditions about the records you want to return • In MySQL Monitor, enter the following code using the AND keyword at the mysql> prompt: mysql> SELECT * FROM company_cars WHERE model_year=2007 AND mileage<60000;[ENTER]
  • 54. 55PHP Programming with MySQL, 2nd Edition Filtering Query Results (continued) • In MySQL Monitor, enter the following code using the OR keyword at the mysql> prompt: mysql> SELECT * FROM company_cars WHERE make='Toyota’ OR make='Honda‘ ORDER BY mileage ;[ENTER]
  • 55. 56PHP Programming with MySQL, 2nd Edition Updating Records • To update records in a table, use the UPDATE statement • The syntax for the UPDATE statement is: UPDATE table_name SET column_name=value WHERE condition; – The UPDATE keyword specifies the name of the table to update – The SET keyword specifies the value to assign to the fields in the records that match the condition in the WHERE keyword
  • 56. 57PHP Programming with MySQL, 2nd Edition Updating Records (continued) • In MySQL Monitor, enter the following code using the OR keyword at the mysql> prompt: mysql> UPDATE company_cars SET mileage=368.2 WHERE make='Ford’ AND model='Fusion'; [ENTER]
  • 57. 58PHP Programming with MySQL, 2nd Edition Deleting Records • Use the DELETE statement to delete records in a table • The syntax for the DELETE statement is: DELETE FROM table_name WHERE condition; • The DELETE statement deletes all records that match the condition • To delete all the records in a table, leave off the WHERE keyword
  • 58. 59PHP Programming with MySQL, 2nd Edition Deleting Records (continued) • In MySQL Monitor, enter the following code at the mysql> prompt: mysql> DELETE FROM company_cars WHERE model_year=2006 AND make='Honda' AND model='Accord';[ENTER] • To delete all records from a table, omit the WHERE clause
  • 59. 60PHP Programming with MySQL, 2nd Edition Summary • A database is an ordered collection of information from which a computer program can quickly access information • A record in a database is a single, complete set of related information • Fields are the individual categories of information stored in a record • A flat-file database stores information in a single table
  • 60. 61PHP Programming with MySQL, 2nd Edition Summary (continued) • A relational database stores information across multiple related tables • A query is a structured set of instructions and criteria for retrieving, adding, modifying, and deleting database information • Structured query language, or SQL (pronounced sequel), is a standard data manipulation language among many database management systems
  • 61. 62PHP Programming with MySQL, 2nd Edition Summary (continued) • MySQL Monitor is a command-line program that you use to manipulate MySQL databases • To work with a database, you must first select it by executing the USE DATEBASE statement • You use the CREATE DATABASE statement to create a new database • To delete a database, you execute the DROP DATABASE statement, which removes all tables from the database and deletes the database itself
  • 62. 63PHP Programming with MySQL, 2nd Edition Summary (continued) • The fields in a table also store data according to type • To keep your database from growing too large, you should choose the smallest data type possible for each field • To create a table, you use the CREATE TABLE statement, which specifies the table and column names and the data type for each column
  • 63. 64PHP Programming with MySQL, 2nd Edition Summary (continued) • To modify a table, you use the ALTER TABLE statement, which specifies the table being changed and the change to make • To delete a table, you execute the DROP TABLE statement, which removes all data and the table definition • You use a GRANT statement to create user accounts and assign privileges, which refer to the operations that a user can perform with a database
  • 64. 65PHP Programming with MySQL, 2nd Edition Summary (continued) • You use the REVOKE statement to take away privileges from an existing user account for a specified table or database • You add individual records to a table with the INSERT statement • To add multiple records to a database, you use the LOAD DATA statement with a local text file that contains the records you want to add
  • 65. 66PHP Programming with MySQL, 2nd Edition Summary (continued) • You use the SELECT statement to retrieve records from a table • You use the ORDER BY keyword with the SELECT statement to perform an alphanumeric sort of the results returned from a query • To perform a reverse sort, add the DESC keyword after the name of the field by which you want to perform the sort
  • 66. 67PHP Programming with MySQL, 2nd Edition Summary (continued) • You can specify which records to return from a database by using the WHERE keyword • You use the UPDATE statement to update records in a table • You use the DELETE statement to delete records from a table • The phpMyAdmin graphical tool simplifies the tasks associated with creating and maintaining databases and tables