Sie sind auf Seite 1von 15

MySQL for Beginners

Student Guide
SQL-4401 Rev 2.1

D61918GC10
Edition 1.0
D63897

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


Disclaimer
This document contains proprietary information, is provided under a license agreement containing restrictions on use and
disclosure, 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 as expressly
permitted in your license agreement or allowed by 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.
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.
Sun Microsystems, Inc. Disclaimer
This training manual may include references to materials, offerings, or products that were previously offered by Sun
Microsystems, Inc. Certain materials, offerings, services, or products may no longer be offered or provided.Oracle and its
affiliates cannot be held responsible for any such references should they appear in the text provided.
Restricted Rights Notice
If this documentation is delivered to the U.S. Government or anyone using the documentation on behalf of the U.S.
Government, the following notice is applicable:
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.
AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro
Devices. Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used
under license and are trademarks or registered trademarks of SPARC International, Inc. UNIX is a registered trademark
licensed through X/Open Company, Ltd.

MySQL for Beginners

Preface

MySQL for Beginners Instructor Guide


This is the Instructor guide for the MySQL for Beginners training course. This guide was developed using the
MySQL 5.0 and/or MySQL 5.1 Community Edition - Generally Available (GA) Releases.

iii

MySQL for Beginners

Preface

Conventions used in this training guide


The following typographical conventions are used throughout this training guide:

Computer input and output is printed in this format: Computer input or output. This is also used
for the names of executable programs and file locations.

Keywords from the SQL language appear in this format: SQL KEYWORD. SQL keywords are not case
sensitive and may be written in any letter case, but the training guide uses uppercase.

Placeholders for user input inside appearing inside computer input appear in this format: <user input>

For emphasis, the folowing style is used: Emphasis

For extra emphasis, the following style is used: Extra Emphasis

When commands are shown that are meant to be executed from within a particular program, the prompt shown
preceding the command indicates which command to use. For example, shell> indicates a command that you
execute from your shell, and mysql> indicates a statement that you execute from the mysql client program:
shell> mysql -u root -h 127.0.0.1
mysql> SELECT * FROM world.City;
The shell is your command interpreter. On Linux, this is typically a program such as sh, csh, or bash. On
Windows, the equivalent program is command.com or cmd.exe, typically run in a console window. When you enter
a command or statement shown in an example, do not type the prompt shown in the example.
Database, table, and column names must often be substituted into statements. To indicate that such substitution is
necessary, this manual uses db_name, tbl_name, and col_name. For example, you might see a statement like
this:
mysql> SELECT col_name FROM db_name.tbl_name;
This means that if you were to enter a similar statement, you would supply your own database, table, and column
names for the placeholdes db_name, tbl_name, and col_name., perhaps like this:
mysql> SELECT author_name FROM biblio_db.author_list;
In syntax descriptions, square brackets ([ and ]) indicate optional words or clauses. For example, in the following
statement, IF EXISTS is optional:
DROP TABLE [IF EXISTS] tbl_name;
When a syntax element consists of a number of alternatives, the alternatives are separated by vertical bars (pipe, |).
When one member from a set of choices may be chosen, the alternatives are listed within square brackets ([ and ]):
TRIM([[BOTH | LEADING | TRAILING] [remstr] FROM] str)
When one member from a set of choices must be chosen, the alternatives are listed within braces ({ and }):
{DESCRIBE | DESC} tbl_name [col_name | wild]

iv

MySQL for Beginners

Preface

An ellipsis (...) indicates the omission of a section of a statement, typically to provide a shorter version of more
complex syntax. For example, INSERT ... SELECT is shorthand for the form of INSERT statement that is
followed by a SELECT statement.
An ellipsis can also indicate that the preceding syntax element of a statement may be repeated. In the following
example, multiple reset_option values may be given, with each of those after the first preceded by commas:
RESET reset_option[, reset_option] ...
Commands for setting shell variables are shown using Bourne shell syntax. For example, the sequence to set the CC
environment variable and run the configure command looks like this in Bourne shell syntax:
shell> CC=gcc ./configure
If you are using csh or tcsh, you must issue commands somewhat differently:
shell> setenv CC gcc
and
shell> ./configure

MySQL for Beginners

Preface

Supporting images used in this Training Guide


The following is a summary of the standard images used in this manual to support the instruction:
IMAGE

123

vi

NAME

DESCRIPTION

Preparation

This image is used to describe the steps required to be completed


prior to performing a hands-on exercise.

Written exam

This image is used to identify that the student is going to be tested


upon the material previously presented in the instructional material

Inline Lab

Throughout the course the instructor will conduct labs in line with
the instruction, which are designed to help you to understand the
nuts and bolts (inner-workings) of the topic.

Further Practice
Lab

This image is used to convey to the student that there is a final


exercise to complete prior to the completion of the chapter.

Student notes

This image identifies an area on a page designated for students to


write notes associated with the class.

Slide number box

Indicates the number of an existing slide that corresponds to the text.

MySQL for Beginners

Preface

Acknowledgments
Sun Microsystems would like to thank the many individuals that played a part in bringing this training material to
the numerous students who will benefit from the knowledge and effort that each of these contributors put into the
training. Even though there were a large number of contributions from many Sun Microsystems' employees, the
following list of contributors played a vital role in developing this material and ensuring that its contents were
accurate, timely and most of all presented in a way that would benefit those that are utilizing it for the benefit of
improving their skills with MySQL.
Patricia Garcia - Course Developer and Editor
Max Mether - Course Development Manager
Sarah Sproehnle, Subject Matter Expert and Lead Instructor
Steven Jones, Subject Matter Expert
Ian Wrigley, Subject Matter Expert
Tobias Asplund, Contributing Instructor
Kimseong Loh, Contributing Instructor

vii

MySQL for Beginners

Preface

2
Class Delivery Preparation
There are a few steps taken at the beginning of class to prepare the students and instructor for the class:

Getting to know the students and their expectations

Checklist of important logistical information

Explanation of the format and usage of the slides and student guide

ix

MySQL for Beginners

Preface

Course Objectives
This course introduces you to MySQL and is designed to give participants basic RDBMS and SQL knowledge and
experience in order to develop and use a database (at a fundamental level). Upon completion of this course you will
be able to:

List the primary features and benefits of MySQL products

Describe MySQL connectors, their major features and differences

Find information about products, certification, support and training

Describe the MySQL client/server architecture model

Install and start the MySQL server

Explain the basics of relational databases

Distinguish the SQL language from MySQL language extensions

Explain data and column types with regard to efficient database design

Inspect the design, structure and content of databases

Design and create an efficiently structured database

Create database tables

Retrieve data from an existing table

Troubleshoot using typical warnings and errors

Modify and delete a database

Modify and delete table row data

Apply indexing and constraints to tables

Use data aggregation in queries

Combine data from multiple tables

Write nested queries

List simple functions (String, Date, Numerical)

Understand the primary methods for exporting and importing data

Create view-only tables

Describe the basics of transactions in MySQL

Explain MySQL storage engine concepts

Retrieve database Metadata

xi

MySQL for Beginners

Preface

Table of Contents
1 Introduction.............................................................................................................................. 1-0
1.1 Learning Objectives............................................................................................................. 1
1.2 MySQL Overview................................................................................................................ 2
1.2.1 Sun Acquisition........................................................................................................... 2
1.2.2 MySQL Partners.......................................................................................................... 3
1.3 MySQL Products..................................................................................................................4
1.3.1 MySQL Database Server Products.............................................................................. 4
1.3.2 MySQL GUI Tools...................................................................................................... 4
1.3.3 MySQL Connectors.....................................................................................................5
1.3.4 Solutions for Embedding MySQL...............................................................................6
1.4 MySQL Services.................................................................................................................. 7
1.4.1 MySQL Training..........................................................................................................7
1.4.2 MySQL Certification................................................................................................... 7
1.4.3 MySQL Consulting......................................................................................................7
1.4.4 MySQL Support.......................................................................................................... 7
1.5 The MySQL Enterprise Subscription................................................................................... 9
1.5.1 MySQL Enterprise Server........................................................................................... 9
1.5.2 24x7 Production Support.............................................................................................9
1.5.3 MySQL Enterprise Monitor.......................................................................................10
1.5.4 Obtaining a MySQL Enterprise Subscription............................................................11
1.6 Supported Operating Systems............................................................................................ 12
1.7 MySQL Certification Program...........................................................................................13
1.8 Training Curriculum Paths................................................................................................. 14
1.9 MySQL Website................................................................................................................. 18
1.9.1 MySQL Community Web Page................................................................................. 19
1.9.2 MySQL Online Documentation.................................................................................21
1.10 Installing MySQL.............................................................................................................24
1.11 Installing the 'world' database.......................................................................................... 25
1.12 Chapter Summary.............................................................................................................27
2 MySQL Client/Server Model................................................................................................... 2-1
2.1 Learning Objectives............................................................................................................. 2
2.2 The Client/Sever Model Overview...................................................................................... 3
2.3 Communication Protocols.................................................................................................... 4
2.4 Connectors and MySQL.......................................................................................................5
2.5 The LAMP Stack..................................................................................................................6
2.6 Starting the MySQL Server and the MySQL Client............................................................ 7
2.6.1 MySQL Server and Command Line Client................................................................. 7
2.6.2 Starting the MySQL Client..........................................................................................7
2.6.3 MySQL Start-up Command Line Options...................................................................8

xii

MySQL for Beginners

Preface

2.7 Chapter Summary...............................................................................................................13


3 Database Basics........................................................................................................................ 3-1
3.1 Learning Objectives............................................................................................................. 2
3.2 Basics of Relational Databases............................................................................................ 3
3.2.1 What is a RDBMS?..................................................................................................... 3
3.2.2 Entities and Relationships........................................................................................... 3
3.2.3 RDBMS Database Structure........................................................................................4
3.3 SQL Language and MySQL.................................................................................................6
3.3.1 The Use of SQL with Databases..................................................................................6
3.3.2 Data Definition Language (DDL) Statements............................................................. 6
3.3.3 Data Manipulation Language (DML) Statements....................................................... 6
3.3.4 The Added-Value Provided by MySQL...................................................................... 7
3.4 Chapter Summary.................................................................................................................9
4 Database Design....................................................................................................................... 4-1
4.1 Learning Objectives............................................................................................................. 2
4.2 Database Modeling...............................................................................................................3
4.2.1 The Entity-Relationship Model................................................................................... 3
4.3 Keys......................................................................................................................................5
4.4 Normalization.......................................................................................................................7
4.4.1 Advantages and Disadvantages of Normalizing..........................................................7
4.4.2 Eliminating Data Inconsistencies................................................................................ 8
4.4.3 Normal Forms..............................................................................................................8
4.4.4 Making Intelligent Choices....................................................................................... 14
4.5 Data Types..........................................................................................................................15
4.6 Numeric Data Types...........................................................................................................16
4.6.1 Integer Data Types.....................................................................................................16
4.6.2 Floating-Point Data Types......................................................................................... 17
4.6.3 Fixed-Point Data Types............................................................................................. 18
4.6.4 BIT Data Types..........................................................................................................19
4.7 Temporal Data Types..........................................................................................................20
4.8 Character String Data Types.............................................................................................. 22
4.8.1 Text Category Data Types......................................................................................... 23
4.8.2 Integer Category Data Types..................................................................................... 24
4.8.3 Binary String Data Types.......................................................................................... 25
4.9 Data Type Considerations.................................................................................................. 27
4.10 The Meaning of NULL.................................................................................................... 28
4.10.1 When to Use NULL.................................................................................................28
4.10.2 When to Use NOT NULL....................................................................................... 28
4.11 Viewing the Database Structure....................................................................................... 29
4.11.1 Viewing a Database in 5 Easy Steps........................................................................29
4.11.2 Evaluating the Design Method................................................................................ 29
4.12 Chapter Summary.............................................................................................................33
xiii

MySQL for Beginners

Preface

5 Database Creation.....................................................................................................................5-1
5.1 Learning Objectives............................................................................................................. 2
5.2 Creating a New Database Structure..................................................................................... 3
5.2.1 Database Design Plan.................................................................................................. 3
5.3 Creating a Database..............................................................................................................6
5.4 Creating a Table................................................................................................................... 7
5.4.1 SHOW CREATE TABLE............................................................................................8
5.4.2 Column Options...........................................................................................................9
5.4.3 Table Options.............................................................................................................10
5.4.4 Table Indexing........................................................................................................... 11
5.4.5 Table Constraints....................................................................................................... 13
5.5 Chapter Summary...............................................................................................................17
6 Basic Queries............................................................................................................................6-1
6.1 Learning Objectives............................................................................................................. 2
6.2 The SELECT Statement....................................................................................................... 3
6.2.1 Basic Uses of SELECT............................................................................................... 4
6.2.2 SELECT with DISTINCT........................................................................................... 5
6.2.3 SELECT with WHERE............................................................................................... 8
6.2.4 SELECT with ORDER BY....................................................................................... 11
6.2.5 SELECT with LIMIT................................................................................................ 13
6.3 MySQL Workbench for SQL Development....................................................................... 16
6.4 Troubleshooting..................................................................................................................20
6.4.1 Warnings and Errors.................................................................................................. 20
6.4.2 SQL Modes for Syntax Checking..............................................................................21
6.5 Chapter Summary...............................................................................................................23
7 Database Maintenance..............................................................................................................7-1
7.1 Learning Objectives............................................................................................................. 2
7.2 Deleting an Entire Database.................................................................................................3
7.2.1 The DROP DATABASE Statement.............................................................................3
7.2.2 CAUTION: When Using DROP DATABASE............................................................3
7.3 Maintaining Tables............................................................................................................... 5
7.3.1 Adding a New Table from an Existing Table...............................................................5
7.3.2 Create a Temporary Table............................................................................................6
7.3.3 Copy and Existing Table Structure.............................................................................. 7
7.3.4 The DROP TABLE Statement..................................................................................... 7
7.4 Maintaining Columns...........................................................................................................9
7.4.1 Adding/Removing Columns........................................................................................ 9
7.4.2 Modifying Columns...................................................................................................10
7.5 Indexes and Constraints..................................................................................................... 13
7.5.1 Adding/Removing Indexes and Constraints.............................................................. 13
7.6 Chapter Summary...............................................................................................................17
8 Data Manipulation.................................................................................................................... 8-1
xiv

MySQL for Beginners

Preface

8.1 Learning Objectives............................................................................................................. 2


8.2 Delete/Modify Table Row Data........................................................................................... 3
8.2.1 Safety Tips................................................................................................................... 3
8.3 The INSERT Statement........................................................................................................ 4
8.4 The REPLACE Statement.................................................................................................... 6
8.5 The UPDATE Statement...................................................................................................... 9
8.5.1 UPDATE...ORDER BY.............................................................................................10
8.5.2 UPDATE...LIMIT......................................................................................................11
8.6 The DELETE Statement.....................................................................................................12
8.6.1 Using DELETE with ORDER BY and LIMIT......................................................... 12
8.7 Chapter Summary...............................................................................................................17
9 Functions.................................................................................................................................. 9-1
9.1 Learning Objectives............................................................................................................. 2
9.2 Simple Functions..................................................................................................................3
9.2.1 Functions in MySQL Expressions............................................................................... 3
9.2.2 String Functions...........................................................................................................3
9.2.3 Temporal Functions..................................................................................................... 8
9.2.4 Numeric Functions.................................................................................................... 11
9.2.5 Flow Control Functions.............................................................................................13
9.3 Grouping With Functions................................................................................................... 16
9.3.1 Why Use Aggregate Functions?................................................................................ 16
9.3.2 Grouping with SELECT...GROUP BY..................................................................... 17
9.3.3 GROUP BY...HAVING............................................................................................. 19
9.3.4 GROUP BY...WITH ROLLUP..................................................................................19
9.3.5 IGNORE SPACE....................................................................................................... 21
9.4 Chapter Summary...............................................................................................................25
10 Joining Tables....................................................................................................................... 10-1
10.1 Learning Objectives........................................................................................................... 2
10.2 Combining Multiple Tables................................................................................................3
10.2.1 Categories of Joins.................................................................................................... 4
10.3 Inner Joins.......................................................................................................................... 5
10.3.1 Comma Joins............................................................................................................. 5
10.3.2 Table Name Aliases................................................................................................... 7
10.3.3 INNER JOIN Keywords............................................................................................7
10.4 Outer Joins ...................................................................................................................... 10
10.4.1 OUTER JOIN Keywords.........................................................................................10
10.5 Chapter Summary.............................................................................................................16
11 Exporting and Importing Data.............................................................................................. 11-1
11.1 Learning Objectives........................................................................................................... 2
11.2 Exporting Data................................................................................................................... 3
11.2.1 Exporting with a Query............................................................................................. 3
11.2.2 Exporting with 'mysqldump'......................................................................................4
xv

MySQL for Beginners

Preface

11.3 Importing Data................................................................................................................... 8


11.3.1 Importing with SQL Statement Files......................................................................... 8
11.3.2 Importing with LOAD DATA INFILE...................................................................... 8
11.3.3 Importing with 'mysqlimport'.................................................................................. 12
11.3.4 Importing with ODBC............................................................................................. 13
11.4 Chapter Summary.............................................................................................................17
12 Subqueries............................................................................................................................ 12-1
12.1 Learning Objectives........................................................................................................... 2
12.2 What is a Subquery?...........................................................................................................3
12.2.1 Why use a Subquery?................................................................................................ 3
12.2.2 Placement of Subqueries........................................................................................... 3
12.3 Categories of Subqueries....................................................................................................5
12.3.1 Non-Correlated Subqueries....................................................................................... 5
12.3.2 Correlated Subqueries............................................................................................... 6
12.4 Subquery Types.................................................................................................................. 7
12.4.1 Result Table Types.....................................................................................................7
12.4.2 Subquery Type/Placement Cross-Reference Chart....................................................8
12.5 Placing Subqueries............................................................................................................. 9
12.5.1 Column Designation Subquery..................................................................................9
12.5.2 FROM Subquery..................................................................................................... 10
12.5.3 WHERE Subquery...................................................................................................11
12.6 Other Subquery Uses........................................................................................................18
12.6.1 Finding Mistmatches............................................................................................... 18
12.6.2 Modifying Tables using Subqueries........................................................................ 18
12.6.3 Converting Joins to Subqueries............................................................................... 19
12.6.4 Subquery Tips..........................................................................................................20
12.7 Converting Subqueries to Joins........................................................................................21
12.8 Chapter Summary.............................................................................................................24
13 Supplemental Information.................................................................................................... 13-1
13.1 Learning Objectives........................................................................................................... 2
13.2 Creating VIEWs................................................................................................................. 3
13.2.1 Using the CREATE VIEW Statement....................................................................... 3
13.2.2 Displaying VIEW Information.................................................................................. 5
13.2.3 VIEW Restrictions.....................................................................................................6
13.3 Transactions........................................................................................................................8
13.3.1 Basics of Transactions............................................................................................... 8
13.3.2 Transaction Statements..............................................................................................9
13.3.3 What is ACID?........................................................................................................ 10
13.4 Storage Engines................................................................................................................11
13.4.1 Storage Engines and MySQL.................................................................................. 11
13.4.2 The MyISAM Storage Engine.................................................................................14
13.4.3 The InnoDB Storage Engine................................................................................... 15
xvi

MySQL for Beginners

Preface

13.4.4 The MEMORY Storage Engine...............................................................................16


13.4.5 Other Engines.......................................................................................................... 17
13.5 Retrieving Metadata......................................................................................................... 18
13.5.1 Using INFORMATION_SCHEMA........................................................................ 18
13.5.2 INFORMATION_SCHEMA Tables........................................................................19
13.5.3 Viewing the INFORMATION_SCHEMA Database............................................... 20
13.6 MySQL GUI's.................................................................................................................. 22
13.6.1 MySQL Enterprise Monitor.....................................................................................22
13.6.2 MySQL Workbench.................................................................................................26
13.7 Chapter Summary.............................................................................................................48
14 Conclusion............................................................................................................................ 14-1
14.1 Learning Objectives........................................................................................................... 2
14.2 Training and Certification Website....................................................................................3
14.3 Course Evaluation.............................................................................................................. 5
14.4 Thank you!......................................................................................................................... 6
14.5 Q&A Session...................................................................................................................... 7
Appendix A MySQL Lab Preparation........................................................................Appendix A-0
A.1 MySQL Server installation on Microsoft Windows............................................................ 2
A.2 MySQL Server installation on Linux.................................................................................. 4
A.3 MySQL 'world' database file installation.............................................................................5
Appendix B Quiz and Lab Solutions......................................................................... Appendix B-0
B.1 Quiz Solutions..................................................................................................................... 2
B.2 Lab and Further Practice Solutions..................................................................................... 5
B.2.1 Chapter 1.....................................................................................................................5
B.2.2 Chapter 2.....................................................................................................................5
B.2.3 Chapter 3.....................................................................................................................5
B.2.4 Chapter 4.....................................................................................................................5
B.2.5 Chapter 5.....................................................................................................................8
B.2.6 Chapter 6...................................................................................................................17
B.2.7 Chapter 7...................................................................................................................25
B.2.8 Chapter 8...................................................................................................................36
B.2.9 Chapter 9...................................................................................................................45
B.2.10 Chapter 10...............................................................................................................49
B.2.11 Chapter 11............................................................................................................... 54
B.2.12 Chapter 12...............................................................................................................59
B.2.13 Chapter 13...............................................................................................................62
Appendix C Glossary of MySQL Terms.................................................................... Appendix C-0
C.1 MySQL Glossary of Terms..................................................................................................2

xvii