You are on page 1of 20

MySQL for Developers

Student Guide - Volume I

D61830GC20
Edition 2.0
August 2012
D78890

Authors

Copyright 2012, Oracle and/or its affiliates. All rights reserved.

Jeremy Smyth

Disclaimer

Pat Garcia

This document contains proprietary information and is protected by copyright and


other intellectual property laws. You may copy and print this document solely for your
own use in an Oracle training course. The document may not be modified or altered
in any way. Except where your use constitutes "fair use" under copyright law, you
may not use, share, download, upload, copy, print, display, perform, reproduce,
publish, license, post, transmit, or distribute this document in whole or in part without
the express authorization of Oracle.

Technical Contributors
and Reviewers
KimSeong Loh
Scott Noyes

Editors
Raj Kumar
Vijayalakshmi Narasimhan
Graphic Editor

The information contained in this document is subject to change without notice. If you
find any problems in the document, please report them in writing to: Oracle University,
500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not
warranted to be error-free.
Restricted Rights Notice
If this documentation is delivered to the United States Government or anyone using
the documentation on behalf of the United States Government, the following notice is
applicable:

Seema Bopaiah

Publishers
Nita Brozowski
Michael Sebastian Almeida

U.S. GOVERNMENT RIGHTS


The U.S. Governments rights to use, modify, reproduce, release, perform, display, or
disclose these training materials are restricted by the terms of the applicable Oracle
license agreement and/or the applicable U.S. Government contract.
Trademark Notice
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names
may be trademarks of their respective owners.

Contents

Introduction to MySQL
Course Goals 1-2
Course Lesson Map 1-5
Student Introductions 1-8
Classroom Environment 1-9
MySQL: Overview 1-10
Acquisitions of the MySQL Company 1-11
MySQL Is Powering the World! 1-12
MySQL Database Server Editions 1-13
MySQL Tools 1-14
MySQL Connectors and APIs 1-15
MySQL Services 1-17
MySQL Community Support 1-18
Oracle Lifetime Support for MySQL 1-19
MySQL-Supported Operating Systems 1-20
MySQL Websites 1-21
MySQL Curriculum Path 1-22
MySQL Certification 1-23
MySQL Online Documentation 1-24
Example Databases 1-25
Summary 1-26

MySQL Client and Server Concepts


Objectives 2-2
MySQL Architecture 2-3
Client Programs 2-4
MySQL Non-Client Utility Programs 2-5
MySQL Server 2-6
Server Process 2-7
Connection Layer 2-8
Communication Protocols 2-9
SQL Layer 2-10
SQL Statement Processing 2-11
Storage Layer 2-12
Storage Engine: Overview 2-13

iii

Features Dependent on Storage Engine 2-14


How MySQL Uses Disk Space 2-15
How MySQL Uses Memory 2-17
Downloading MySQL Products 2-18
Starting the MySQL Command-Line Client 2-19
Using Script Files with MySQL 2-20
Databases in MySQL 2-21
Databases in MySQL: Real-World 2-22
Databases in MySQL: Fragments 2-23
Summary 2-24
Practice 2-1 Overview: Quiz 2-25
Practice 2-2 Overview: Classroom Environment Review 2-26
Practice 2-3 Overview: Installing the world_innodb Database 2-27
3

MySQL Clients
Objectives 3-2
Client Programs 3-3
Client Programs: Options 3-4
Command-Line Options 3-5
Options with Parameters 3-6
Options for Connecting 3-7
The --protocol Option 3-8
--host and --port Options 3-9
--socket and --shared-memory-base-name Options 3-10
--compress Option 3-11
Options for User Identification 3-12
Using the --safe-updates Option 3-14
Option Files 3-15
Viewing Option File Locations 3-17
Standard Option File Locations 3-18
Specifying Additional Option Files 3-19
Overriding How a Program Reads Option Files 3-20
mysql Client Program 3-22
Using mysql Interactively 3-23
Statement Terminators 3-24
\G Statement Terminator 3-26
Statements on Multiple Lines 3-28
Prompts 3-29
Changing the Primary Prompt 3-30
Line Editing in mysql 3-31
Formatting Output 3-32

iv

mysql Commands 3-33


Sample STATUS Output 3-34
Logging mysql Sessions 3-35
Accessing Help 3-36
Quiz 3-38
MySQL Workbench 3-39
MySQL Workbench: Functional Modules 3-40
MySQL Workbench: Primary GUI Window 3-41
MySQL Workbench: SQL Development Window 3-42
MySQL Workbench: Data Modeling Module 3-43
MySQL Workbench: Data Modeling Features 3-44
MySQL Workbench: Data Modeling Window 3-45
MySQL Workbench: Data Modeling Panels 3-46
MySQL Workbench: Data Modeling EER Diagram 3-47
MySQL Workbench: Server Administration Window 3-48
MySQL Workbench: Server Administration Module Features 3-49
Quiz 3-50
MySQL Connectors 3-51
Connectors Developed by MySQL 3-52
MySQL Community Connectors 3-54
Connector Example: Java 3-55
Connector Example: PHP 3-57
Summary 3-59
Practice 3-1 Overview: Quiz MySQL Clients and Connectors 3-60
Practice 3-2 Overview: Using the mysql Command-Line Client with
Various Options 3-61
Practice 3-3 Overview: Using the mysql Command-Line Client with
Option Files 3-62
Practice 3-4 Overview: Practicing Use of Statement Terminators
and Editing Keys 3-63
Practice 3-5 Overview: Using the mysql Command-Line Client Interactively and in
Batch Mode 3-64
Practice 3-6 Overview: Using MySQL Workbench to Run Statements
and Explore Databases 3-65
Practice 3-7 Overview: Working with Connectors and MySQL APIs 3-66
4

Querying for Table Data


Objectives 4-2
SELECT Statement 4-3
SELECT: Basic Example 4-4
SELECT: Using Expressions 4-5

SELECT: Using Clauses 4-6


SELECT: Using FROM 4-7
SELECT: Using DISTINCT 4-8
SELECT: Using DISTINCT with NULL 4-10
SELECT: Using WHERE 4-11
SELECT: Using WHERE Operators 4-12
A Word About NULL 4-14
SELECT: Using WHERE with IN Option 4-15
SELECT: Using WHERE with AND/OR Operators 4-16
SELECT: Using ORDER BY 4-17
SELECT: Using ORDER BY with ASC or DESC 4-19
SELECT: Using ORDER BY with ASC and DESC 4-20
SELECT: Using LIMIT 4-21
SELECT: Using LIMIT and Skip Count 4-22
SELECT: Using LIMIT and ORDER BY 4-23
SELECT: Using LIMIT and ORDER BY with ASC
and DESC 4-24
Quiz 4-25
Aggregate Functions 4-26
Aggregate Functions: Most Common 4-27
Aggregate Functions: Using COUNT() 4-28
Aggregate Functions: GROUP BY Clause 4-29
Aggregate Functions: GROUP BY Clause and GROUP_CONCAT Function 4-30
Aggregate Functions: GROUP BY and HAVING Clauses 4-31
Aggregate Functions: GROUP BY Clause and WITH ROLLUP Modifier 4-32
Aggregate Functions: Super Aggregate Operation 4-33
Spaces in Function Names 4-34
Concatenating Multiple Query Results 4-35
Using UNION 4-36
Using UNION with ORDER BY and LIMIT 4-37
Exporting and Importing Data 4-38
Export Data by Using SELECT with INTO OUTFILE 4-39
SELECT with INTO OUTFILE: Output File Characteristics 4-40
Using Data File Format Specifiers 4-41
Escape Character Sequences 4-42
Importing Data by Using LOAD DATA INFILE 4-43
Skipping or Transforming Input Data 4-44
Duplicate Records 4-45
Summary 4-47
Practice 4-1 Overview: Using the SELECT Statement for Basic Queries 4-48
Practice 4-2 Overview: Using the SELECT Statement for Aggregate Queries 4-49

vi

Practice 4-3 Overview: Using the UNION Statement to Concatenate Queries 4-50
Practice 4-4 Overview: Exporting Database Data 4-51
Practice 4-5 Overview: Importing Database Data 4-52
Practice 4-6 Overview: Additional Practice 4-53
5

Handling Errors and Warnings


Objectives 5-2
SQL Modes 5-3
Setting SQL Modes 5-4
Important SQL Modes 5-5
Strict Mode 5-7
Interpreting Error Messages 5-8
Using SHOW WARNINGS 5-9
Using SHOW ERRORS 5-11
perror Utility 5-12
Declaring Error Handlers 5-13
Declaring Named Conditions 5-14
RESIGNAL 5-15
SIGNAL 5-16
Error Handling when Using Connectors 5-17
Java: The SQLException Class 5-18
Using SQLException 5-19
PHP: mysqli Error-Handling Functions 5-20
PHP: The mysqli_sql_exception Class 5-21
Using mysqli Error Handling 5-22
Using mysqli_sql_exception and Exception Handling 5-23
Quiz 5-24
Summary 5-25
Practice 5-1 Overview: Quiz Handling Errors 5-26
Practice 5-2 Overview: Identifying Errors and Warnings While Using
the mysql Client 5-27
Practice 5-3 Overview: Handling MySQL Errors when Coding with Connectors 5-28

Data Types
Objectives 6-2
Data Type: Overview 6-3
Creating Tables with Data Types 6-4
Numeric Data Types 6-5
Numeric: Integer Data Types 6-6
Numeric: Integer Data Types Comparison 6-7
Numeric: Floating-Point Data Types 6-8

vii

Numeric: MySQL-Specific Floating-Point Data Types 6-10


Numeric: Floating-Point Data Types Comparison 6-11
Numeric: Fixed-Point Data Types 6-12
Numeric: Bit Data Types 6-14
Temporal Data Types 6-15
Temporal Data Types: Comparison 6-17
Character String Data Types 6-18
Character String: Considerations 6-19
Character String: Text Class Data Types 6-20
Character String: Text Class Data Types Comparison 6-22
Character String: Integer Class Data Types 6-23
Quiz 6-24
Character Set and Collation Support 6-25
Available Character Sets 6-26
Available Collations 6-27
Binary String Data Types 6-28
Binary String: Data Types Comparison 6-29
Setting Data Types to NULL 6-30
When to Use NULL 6-31
When Not to Use NULL 6-32
Spatial Data Type Extensions 6-33
Spatial Data Type Extensions: Storage Engines 6-34
Spatial Data Type Extensions: Data Types 6-35
Spatial Data Type Extensions: Populating Spatial Columns 6-36
Summary 6-37
Practice 6-1 Overview: Quiz Data Types 6-38
Practice 6-2 Overview: Using Numeric Data Types 6-39
Practice 6-3 Overview: Using Temporal Data Types 6-40
Practice 6-4 Overview: Using String Data Types 6-41
Practice 6-5 Overview: Using Spatial Data Types 6-42
Practice 6-6 Overview: Reviewing Data Types in the sakila Database 6-43
7

SQL Expressions
Objectives 7-2
SQL Expressions 7-3
SQL Expressions: Examples 7-4
Numeric Expressions 7-5
Numeric Expressions: Results 7-6
String Expressions 7-7
String Expressions: Concatenation 7-8
Case-Sensitivity in String Comparisons 7-9

viii

Case-Sensitivity in String Comparisons: Changing Collation 7-11


String Pattern Matching 7-12
String Pattern Matching: LIKE Versus NOT LIKE 7-13
String Pattern Matching: Regular Expressions 7-14
String Pattern Matching: RLIKE Syntax 7-15
String Pattern Matching: RLIKE Character Class 7-16
String Pattern Matching: Regular Expression Examples 7-17
String Pattern Matching: Ranges of Characters 7-19
String Pattern Matching: Escaping Characters 7-20
Temporal Expressions 7-22
Temporal Expressions: Date Components 7-23
Temporal Expressions: Interval Arithmetic 7-24
Quiz 7-25
Function Expressions 7-26
Function Expressions: Spaces in Function Names 7-27
Comparison Functions: Using LEAST() and GREATEST() 7-28
Comparison Functions: Using INTERVAL() 7-29
Control Flow Functions 7-30
Control Flow Functions: IF() with ORDER BY Example 7-31
Control Flow Functions: SUM(IF())Example 7-32
Control Flow Functions: IF(NULL)Example 7-33
Control Flow Functions: CASE Functions 7-34
Control Flow Functions: CASE Function Syntax 7-35
Control Flow Functions: CASE Value Comparison 7-36
Control Flow Functions: Condition Evaluation 7-37
Numeric Functions 7-38
Numeric Functions: ABS() and SIGN() Examples 7-39
Numeric Functions: FLOOR() and CEILING() Examples 7-40
Numeric Functions: ROUND() Examples 7-41
Numeric Functions: Trigonometric Examples 7-42
String Functions 7-43
String Functions: Numeric Category Examples (INSTR, LOCATE, POSITION) 7-44
String Functions: Numeric Category Examples (LENGTH, CHAR_LENGTH,
CONVERT) 7-45
String Functions: Numeric Category Examples (STRCMP) 7-46
String Functions: String Category Examples (CONCAT, CONCAT_WS, LEFT,
RIGHT) 7-47
String Functions: String Category Examples
(SUBSTRING, SUBSTRING_INDEX) 7-48
String Functions: String Category Examples (LTRIM, RTRIM, TRIM) 7-49
String Functions: String Category Examples (INSERT, REPLACE) 7-50

ix

String Functions: Character Set and Collation 7-51


Quiz 7-52
Temporal Functions 7-53
Temporal Functions: Function Types 7-54
Temporal Functions: Extracting Temporal Data
(NOW, CURDATE, CURTIME, DAYNAME) 7-55
Temporal Functions: Extracting Temporal Data (GET_FORMAT, DATE) 7-56
Temporal Functions: Extracting Temporal Data (YEAR, MONTH, DAYOFMONTH,
DAYOFYEAR, HOUR, MINUTE, SECOND) 7-57
Temporal Functions: Manipulating Temporal Data
(NOW, DATE_FORMAT, INTERVAL) 7-58
Temporal Functions: Manipulating Temporal Data (MAKEDATE, MAKETIME) 7-59
NULL-Related Functions: Using ISNULL and IFNULL 7-60
NULL-Related Functions: Using CONCAT and CONCAT_WS 7-61
Comments in SQL Statements 7-62
Comments in SQL Statements: As Part of SQL 7-63
Comments on Database Objects 7-64
Summary 7-65
Practice 7-1 Overview: Using SQL Expressions 7-66
Practice 7-2 Overview: Using SQL Functions 7-67
Practice 7-3 Overview: Additional Practice 7-68
8

Obtaining Metadata
Objectives 8-2
Metadata Access Methods 8-3
INFORMATION_SCHEMA Database 8-4
Using INFORMATION_SCHEMA: Examples 8-6
Using SHOW 8-7
Using SHOW DATABASES 8-8
Using SHOW TABLES 8-9
Using SHOW COLUMNS 8-10
Using SHOW INDEX 8-11
SHOW CHARACTER SET and SHOW COLLATION 8-12
Using SHOW with LIKE and WHERE 8-13
Using DESCRIBE 8-14
mysqlshow Command 8-15
Quiz 8-17
Summary 8-18
Practice 8-1 Overview: Obtaining Metadata 8-19
Practice 8-2 Overview: Using the SHOW and DESCRIBE Syntax 8-20
Practice 8-3 Overview: Showing Tables, Databases, and Columns 8-21

Databases
Objectives 9-2
Database Properties 9-3
Database Data Directory Features 9-4
Data Directory 9-5
Good Design Practices 9-6
Database Modeling 9-7
Structure Diagram (ERD) 9-8
Cardinality Diagram (ERD) 9-9
Cardinality Diagram: Examples 9-10
Keys 9-11
Keys: Cardinality ERD Example 9-12
Keys: SQL Example 9-13
Quiz 9-14
Normalization 9-15
Why Normalize a Database? 9-16
Advantages of Normalization 9-18
Disadvantages of Normalization 9-19
Normal Forms 9-20
Normalization Process: Example 9-21
First Normal Form: 1NF 9-22
Second Normal Form: 2NF 9-23
Third Normal Form: 3NF 9-24
Normalized Furniture Store Database 9-27
Normalization: SQL Example Using world_innodb 9-28
Normalization: SQL Example Explanation 9-29
Identifiers 9-30
Identifier Rules: Unquoted and Quoted 9-31
Identifier Rules: Alias 9-32
Identifiers: Reserved Words 9-33
Identifiers: Avoid Reserved Word Errors 9-34
Using Qualified Names 9-35
Case-Sensitivity 9-36
Creating Databases 9-37
Using and Displaying Databases 9-38
Altering Databases 9-39
Dropping Databases 9-40
Summary 9-41
Practice 9-1 Overview: Quiz Normalization 9-42
Practice 9-2 Overview: Creating a Database 9-43
Practice 9-3 Overview: Changing Database Character Set and Collation 9-44

xi

Practice 9-4 Overview: Removing a Database 9-45


Practice 9-5 Overview: Reviewing the Configuration and Normalization
of the sakila Database 9-46
10 Tables
Objectives 10-2
Creating a Table 10-3
Table Properties 10-5
Column Options 10-7
Table Constraints 10-8
Using the SHOW CREATE TABLE Statement 10-9
Creating New Tables from Existing Tables 10-10
Creating a New Table Using an Existing Table 10-11
Creating a New Table Using an Existing Table: Results 10-12
Copying an Existing Table Structure 10-13
Comparing SELECT Versus LIKE Results 10-14
Creating a Temporary Table 10-16
Adding a Table Column 10-17
Removing a Table Column 10-19
Modifying a Table Column 10-20
Changing Columns 10-21
Renaming Tables 10-22
Removing Tables 10-23
Quiz 10-24
Foreign Keys 10-25
Foreign Key in the world_innodb Database 10-26
Foreign Keys in the world_innodb Database 10-28
Foreign Key world_innodb Examples 10-29
Referential Integrity 10-31
Foreign Key Constraints 10-33
Creating Foreign Key Constraints 10-34
Creating Foreign Key Constraints: Full Syntax 10-35
Creating Foreign Key Constraints: Optional Syntax Elements 10-36
Foreign Keys and Storage Engines 10-37
InnoDB Storage Engine Status 10-38
Implementation of Foreign Keys in InnoDB 10-39
Summary 10-40
Practice 10-1 Overview: Creating and Viewing New Tables 10-41
Practice 10-2 Overview: Creating New Tables from Existing Tables 10-42
Practice 10-3 Overview: Adding, Removing, and Modifying Tables 10-43
Practice 10-4 Overview: Removing Tables from a Database 10-44

xii

Practice 10-5 Overview: Setting Foreign Keys 10-45


Practice 10-6 Overview: Additional Practice 10-46
11 Manipulating Table Data
Objectives 11-2
Inserting Data into a Table 11-3
Inserting Data into a Table: INSERT with SET 11-4
Inserting Data into a Table: INSERT with SELECT 11-5
Inserting Data into a Table: INSERT with LAST_INSERT_ID 11-6
Retrieving Last Insert ID Using Java 11-7
DELETE Statement 11-9
DELETE with ORDER BY and LIMIT 11-10
DELETE with ORDER BY and LIMIT: Ascending 11-11
UPDATE Statement 11-12
UPDATE Statement: Column Assignment Effects 11-13
UPDATE Statement Ordering 11-14
UPDATE with LIMIT 11-15
REPLACE Statement 11-16
REPLACE Results 11-17
REPLACE Algorithm 11-18
Insertion of Duplicate Rows 11-19
Removing Table Rows by Truncating 11-20
Quiz 11-21
Summary 11-22
Practice 11-1 Overview: Adding New Data to Tables 11-23
Practice 11-2 Overview: Removing Data from Existing Tables 11-24
Practice 11-3 Overview: Modifying Existing Table Row Data 11-25
Practice 11-4 Overview: Replacing Table Data 11-26
Practice 11-5 Overview: Avoiding Data Insertion Errors 11-27
Practice 11-6 Overview: Emptying Tables Using the
TRUNCATE TABLE Statement 11-28
Practice 11-7 Overview: Additional Practice 11-29
12 Transactions
Objectives 12-2
What Is a Transaction? 12-3
Handling Partial Operations 12-4
The ACID Properties of Transactions 12-5
Transaction Control Statements 12-6
The autocommit Mode 12-7
Controlling the autocommit Mode 12-9

xiii

Statements Causing an Implicit COMMIT 12-10


Consistency Issues 12-11
Isolation Levels 12-12
Resolving Consistency Issues with Isolation Levels 12-14
Setting Isolation Levels 12-15
Locking 12-16
Locking Reads 12-17
Quiz 12-19
Summary 12-20
Practice 12-1 Overview: Using Transaction Control Statements 12-21
Practice 12-2 Overview: QuizIsolation Levels and Locking 12-22
Practice 12-3 Overview: Working with Transactions 12-23
13 Joining Tables
Objectives 13-2
Joining Tables 13-3
Single Table Query Limitation 13-4
Combining Two Simple Tables 13-6
Combining Two Simple Tables: Glue Columns Together 13-7
Combining Two Simple Tables: Cartesian Product 13-8
Combining Two Simple Tables: Cartesian Product Dimensions 13-18
Filtering Out Undesired Rows and Columns 13-19
Final Result and Recapitulation 13-20
Joins and Foreign Keys 13-21
Joins and Denormalizing 13-22
Joins and Denormalizing: Tables 13-24
Quiz 13-25
Joining tables in SQL 13-26
Retaining Matching Rows 13-27
Join Condition 13-28
Ambiguous Column Names 13-29
Table Name Aliases 13-30
Reasons for Using Table Name Aliases 13-31
Common Alias Error 13-32
Basic Join Syntax 13-33
Inner Joins 13-35
Inner Joins: With ON Versus USING 13-36
Omitting the Join Condition 13-37
Outer Joins 13-38
LEFT JOIN: Syntax 13-40
LEFT JOIN: Using ON and WHERE 13-41

xiv

RIGHT JOIN: Syntax 13-42


RIGHT JOIN: Using ON and WHERE 13-43
Equivalent LEFT and RIGHT JOIN Syntax 13-44
Outer Joins: The Join Condition 13-45
Outer Joins: Using ON Versus WHERE 13-47
Outer Joins: Using ON Versus WHERE Comparison 13-48
Choosing Between Inner and Outer Joins 13-50
Quiz 13-51
Nullable Columns in the Join Condition 13-52
Joining to Another Outer Joined Table 13-53
Joining to Another Outer Joined Table: Example 13-55
When No Rows Meet the Join Condition 13-56
When No Rows Meet the Join Condition: Using LEFT JOIN with IS NULL 13-57
Aggregating Related Rows 13-58
Additional Types of Joins 13-59
NATURAL JOIN 13-60
NATURAL JOIN: Example 13-61
Equijoin and Non-Equijoin Joins 13-62
BETWEEN...AND Join 13-63
Autojoin Join 13-64
Joins in DELETE and UPDATE Statements 13-65
Multi-Table UPDATE Syntax 13-66
Multi-Table UPDATE: Example 13-67
Multi-Table DELETE Syntax 13-68
Multi-Table DELETE: Example 13-69
Multi-Table UPDATE and DELETE Join Advantages 13-70
Multi-Table UPDATE and DELETE Join Limitations 13-71
Summary 13-72
Practice 13-1 Overview: Examining the Cartesian Product and
Obtaining Table Data 13-73
Practice 13-2 Overview: Creating a Cartesian Product and Joining
Tables Using a WHERE Clause 13-74
Practice 13-3 Overview: Obtaining Information About Specific Columns 13-75
Practice 13-4 Overview: Using the JOIN Syntax 13-76
Practice 13-5 Overview: Obtaining Table Data Using the
INNER JOIN Keywords 13-77
Practice 13-6 Overview: Obtaining Table Data Using the LEFT OUTER JOIN and
RIGHT OUTER JOIN Keywords 13-78
Practice 13-7 Overview: Updating and Deleting Data in Multiple Tables 13-79
Practice 13-8 Overview: Executing Joins Across Multiple Tables 13-80

xv

14 Table Subqueries
Objectives 14-2
Subqueries 14-3
Subquery: Example 14-4
Subquery Categories 14-6
Scalar Subqueries 14-7
Scalar Subquery: Example 14-9
Row Subqueries 14-10
Row Subqueries: Example 14-11
Row Subqueries with Empty Sets 14-12
Table Subqueries 14-13
Subqueries in the FROM Clause 14-14
Calculating Aggregates of Aggregates 14-15
Table Subquery Operators 14-17
IN Operator 14-18
Pairwise IN Operation 14-19
Using NOT IN 14-20
IN Operator and NULL 14-21
EXISTS Operator 14-22
Using NOT EXISTS 14-23
Quiz 14-24
Quantifiers 14-25
ALL, ANY, and SOME 14-26
Alternatives to ANY and ALL 14-28
Correlated Subqueries 14-29
Correlated Subquery Scope 14-30
Modifying Data with Subqueries 14-31
Converting Subqueries to JOIN Statements 14-32
Rewriting IN and NOT IN 14-33
Rewriting NOT IN to an Outer Join 14-35
Limitations of Rewriting Subqueries to Joins 14-36
Summary 14-38
Practice 14-1 Overview: QuizSubqueries 14-39
Practice 14-2 Overview: Placing Subqueries in the SELECT and
FROM Clauses 14-40
Practice 14-3 Overview: Placing Subqueries in the WHERE Clause 14-41
Practice 14-4 Overview: Additional Practice 14-42
15 Views
Objectives 15-2
Views 15-3

xvi

Benefits of Views 15-4


Creating Views 15-5
View: Example 15-7
Naming Columns Explicitly 15-8
Updatable Views 15-10
Insertable Views 15-12
Updating Multitable Views 15-13
Using WITH CHECK OPTION 15-14
Checking Views 15-15
Altering Views 15-17
Dropping Views 15-18
Obtaining View Metadata with INFORMATION_SCHEMA 15-19
Obtaining View Metadata with SHOW CREATE VIEW 15-20
Other Metadata Statements 15-21
Quiz 15-22
Summary 15-23
Practice 15-1 Overview: Using the CREATE VIEW Statement 15-24
Practice 15-2 Overview: Using Updatable Views 15-25
Practice 15-3 Overview: Checking Views 15-26
Practice 15-4 Overview: Obtaining View Metadata 15-27
Practice 15-5 Overview: Additional Practice 15-28
16 Prepared Statements
Objectives 16-2
Why Use Prepared Statements? 16-3
User-Defined Variables 16-4
Preparing Statements 16-5
Executing a Prepared Statement 16-7
Deallocating a Prepared Statement 16-9
Limitations of Prepared Statements 16-10
Preparing Statements with Connectors 16-11
Preparing Statements with Java 16-12
Preparing Statements with Java: Example 16-13
Preparing Statements with PHP 16-14
Preparing Statements with PHP: Example 16-15
Quiz 16-16
Summary 16-17
Practice 16-1 Overview: Preparing Statements in MySQL 16-18
Practice 16-2 Overview: Programming with Prepared Statements 16-19

xvii

17 Stored Routines
Objectives 17-2
What Is a Stored Routine? 17-3
Uses of Stored Routines 17-4
Issues with Stored Routines 17-6
Creating Stored Procedures 17-7
Creating Stored Functions 17-8
Compound Statements 17-9
The DECLARE Statement 17-11
Using SELECT ... INTO 17-13
Using SET 17-14
Parameter Declarations 17-15
Executing Stored Routines 17-17
Stored Routines in Databases 17-18
Examining Stored Routines 17-19
Deleting Stored Routines 17-20
Quiz 17-21
Flow Control Statements 17-22
IF Statement 17-23
CASE Statement 17-24
REPEAT Statement 17-25
WHILE Statement 17-26
LOOP Statement 17-27
LEAVE and ITERATE 17-28
Cursors 17-29
Declaring Cursors 17-30
Working with Cursors 17-31
Quiz 17-32
Summary 17-33
Practice 17-1 Overview: Creating Two Stored Routines: A Stored Procedure
and a Stored Function 17-34
Practice 17-2 Overview: Creating Stored Routines with
Compound Statements 17-35
Practice 17-3 Overview: Creating Stored Routines with
Parameter Declarations 17-36
Practice 17-4 Overview: Examining Stored Routines 17-37
Practice 17-5 Overview: Deleting and Re-creating a Stored Routine 17-38
Practice 17-6 Overview: Creating Stored Routines by Using
Flow Control Statements 17-39
Practice 17-7 Overview: Working with the DECLARE CONDITION and DECLARE
HANDLER Statements 17-40

xviii

Practice 17-8 Overview: Creating a Stored Function to Demonstrate


Stored Routine Features 17-41
Practice 17-9 Overview: Additional Practice 17-42
18 Triggers
Objectives 18-2
What Are Triggers? 18-3
Trigger Use Cases 18-4
Creating Triggers 18-5
Trigger Events 18-6
Using BEFORE Triggers 18-7
Using AFTER Triggers 18-8
Trigger Metadata 18-9
Trigger Error Handling 18-10
Deleting Triggers 18-11
Trigger Restrictions 18-12
Quiz 18-13
Summary 18-14
Practice 18-1 Overview: Creating and Dropping Triggers 18-15
Practice 18-2 Overview: Additional Practice 18-16
19 Optimization and Monitoring
Objectives 19-2
Why Optimize? 19-3
Optimization Strategies 19-4
SQL Parser and Query Optimizer 19-5
Using Indexes for Optimization 19-7
Index Types 19-8
Creating Indexes 19-9
Primary Key Indexes 19-11
Naming Indexes 19-12
Adding Indexes to Existing Tables 19-13
Using Index Prefixes 19-14
Indexing Without Prefixes 19-15
Indexing with Prefixes 19-16
Evaluating Index Prefixes 19-17
Leftmost Index Prefixes 19-18
Dropping Indexes 19-20
Using EXPLAIN to Analyze Queries 19-21
How EXPLAIN Works 19-22
EXPLAIN Output Columns 19-24

xix

EXPLAIN for Joins 19-26


EXPLAIN Types 19-27
Extra Column: Signs of Efficient Queries 19-29
Extra Column: Signs of Inefficient Queries 19-30
Quiz 19-31
Query Rewriting Techniques 19-32
Avoiding Column Expressions in Comparisons 19-33
Creating Indexes to Support Joins 19-34
Comparing Similar Data Types 19-35
Avoiding Leading Wildcards in LIKE Patterns 19-36
Considering Computed Columns 19-37
Optimizing Queries by Limiting Output 19-38
Using Summary Tables 19-39
Optimizing Data Modification Statements 19-41
Performance Schema 19-42
Instruments, Instances, Events, and Consumers 19-43
Quiz 19-45
Summary 19-46
Practice 19-1 Overview: QuizOptimization 19-47
Practice 19-2 Overview: Creating a Table with Indexes 19-48
Practice 19-3 Overview: Altering the Indexes in a Pre-existing Table 19-49
Practice 19-4 Overview: Removing an Index from a Pre-existing Table 19-50
Practice 19-5 Overview: Using Optimization Techniques 19-51
20 Conclusion
Course Goals 20-2
MySQL Curriculum Path 20-5
MySQL Resources 20-6
We Need Your Evaluation! 20-7
Thank You 20-8
Q&A Session 20-9

xx

You might also like