Professional Documents
Culture Documents
D61830GC20
Edition 2.0
August 2012
D78890
Authors
Jeremy Smyth
Disclaimer
Pat Garcia
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
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
iii
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
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
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
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
ix
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
xii
xiii
xiv
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
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
xix
xx