Tải bản đầy đủ (.pdf) (990 trang)

mysql cookbook 2nd edition

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (11.31 MB, 990 trang )

www.it-ebooks.info
www.it-ebooks.info
New to OSCON this year:
OSCON Data
The O’Reilly Open Source Convention
Learn about open source technologies for gathering,
storing, and analyzing data with practical techniques and
tools you can immediately put to use at this
rst-of-its-kind open source event.
Register Now & Save 20%
use discount code OS11DEBK
July 25–27, 2011
Portland, OR
oscon.com/data
www.it-ebooks.info
MySQL Cookbook


www.it-ebooks.info
www.it-ebooks.info
SECOND EDITION
MySQL Cookbook

Paul DuBois
Beijing

Cambridge

Farnham

Köln



Paris

Sebastopol

Taipei

Tokyo
www.it-ebooks.info
www.it-ebooks.info
MySQL Cookbook™, Second Edition
by Paul DuBois
Copyright © 2007, 2002 O’Reilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (). For more information, contact our corporate/
institutional sales department: (800) 998-9938 or
Editors: Brian Jepson and Andy Oram
Copy Editor: Mary Anne Weeks Mayo
Production Editor: Adam Witwer
Proofreader: Sada Preisch
Indexer: Joe Wizda
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrators: Robert Romano and Jessamyn Read
Printing History:
October 2002: First Edition.
November 2006: Second Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of

O’Reilly Media, Inc. MySQL Cookbook, the image of a green anole, and related trade dress are trademarks
of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book and O’Reilly Media, Inc. was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
[C]
www.it-ebooks.info
www.it-ebooks.info
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
1. Using the mysql Client Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.0 Introduction 1
1.1 Setting Up a MySQL User Account 2
1.2 Creating a Database and a Sample Table 4
1.3 Starting and Stopping mysql 6
1.4
Specifying Connection Parameters Using Option Files 8
1.5 Protecting Option Files from Other Users 11
1.6 Mixing Command-Line and Option File Parameters 11
1.7
What to Do if mysql Cannot Be Found 12
1.8
Issuing SQL Statements 14
1.9
Canceling a Partially Entered Statement 15
1.10
Repeating and Editing SQL Statements 16

1.11
Using Auto-Completion for Database and Table Names 17
1.12
Telling mysql to Read Statements from a File 18
1.13
Telling mysql to Read Statements from Other Programs 21
1.14
Entering an SQL One-Liner 22
1.15
Using Copy and Paste as a mysql Input Source 23
1.16
Preventing Query Output from Scrolling off the Screen 23
1.17
Sending Query Output to a File or to a Program 25
1.18
Selecting Tabular or Tab-Delimited Query Output Format 26
1.19
Specifying Arbitrary Output Column Delimiters 27
1.20
Producing HTML or XML Output 29
1.21
Suppressing Column Headings in Query Output 31
1.22
Making Long Output Lines More Readable 31
1.23
Controlling mysql’s Verbosity Level 33
1.24
Logging Interactive mysql Sessions 33
1.25
Creating mysql Scripts from Previously Executed Statements 34

1.26
Using User-Defined Variables in SQL Statements 35
1.27
Numbering Query Output Lines 38
1.28
Using mysql as a Calculator 39
v
www.it-ebooks.info
1.29 Using mysql in Shell Scripts 40
2. Writing MySQL-Based Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.0 Introduction 47
2.1 Connecting, Selecting a Database, and Disconnecting 52
2.2
Checking for Errors 66
2.3
Writing Library Files 74
2.4
Issuing Statements and Retrieving Results 87
2.5
Handling Special Characters and NULL Values in Statements 103
2.6 Handling Special Characters in Identifiers 114
2.7
Identifying NULL Values in Result Sets 115
2.8 Techniques for Obtaining Connection Parameters 120
2.9 Conclusion and Words of Advice 132
3. Selecting Data from Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
3.0 Introduction 133
3.1 Specifying Which Columns to Select 135
3.2
Specifying Which Rows to Select 136

3.3 Giving Better Names to Query Result Columns 137
3.4
Using Column Aliases to Make Programs Easier to Write 140
3.5 Combining Columns to Construct Composite Values 141
3.6
WHERE Clauses and Column Aliases 142
3.7
Debugging Comparison Expressions 143
3.8
Removing Duplicate Rows 144
3.9
Working with NULL Values 145
3.10
Writing Comparisons Involving NULL in Programs 147
3.11
Sorting a Result Set 148
3.12
Using Views to Simplify Table Access 150
3.13
Selecting Data from More Than One Table 151
3.14
Selecting Rows from the Beginning or End of a Result Set 153
3.15
Selecting Rows from the Middle of a Result Set 155
3.16
Choosing Appropriate LIMIT Values 157
3.17
What to Do When LIMIT Requires the “Wrong” Sort Order 160
3.18
Calculating LIMIT Values from Expressions 161

4. Table Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
4.0
Introduction 163
4.1
Cloning a Table 163
4.2
Saving a Query Result in a Table 164
4.3
Creating Temporary Tables 167
4.4
Checking or Changing a Table’s Storage Engine 169
4.5
Generating Unique Table Names 170
vi | Table of Contents
www.it-ebooks.info
5. Working with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
5.0 Introduction 173
5.1 String Properties 174
5.2 Choosing a String Data Type 177
5.3
Setting the Client Connection Character Set Properly 180
5.4 Writing String Literals 181
5.5
Checking a String’s Character Set or Collation 184
5.6
Changing a String’s Character Set or Collation 185
5.7 Converting the Lettercase of a String 187
5.8
Converting the Lettercase of a “Stubborn” String 188
5.9

Controlling Case Sensitivity in String Comparisons 190
5.10 Pattern Matching with SQL Patterns 193
5.11 Pattern Matching with Regular Expressions 196
5.12
Controlling Case Sensitivity in Pattern Matching 200
5.13 Breaking Apart or Combining Strings 202
5.14 Searching for Substrings 205
5.15 Using FULLTEXT Searches 206
5.16 Using a FULLTEXT Search with Short Words 210
5.17 Requiring or Excluding FULLTEXT Search Words 212
5.18 Performing Phrase Searches with a FULLTEXT Index 213
6. Working with Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
6.0
Introduction 215
6.1
Choosing a Temporal Data Type 216
6.2
Changing MySQL’s Date Format 218
6.3
Setting the Client Time Zone 222
6.4
Determining the Current Date or Time 224
6.5
Using TIMESTAMP to Track Row Modification Times 225
6.6
Extracting Parts of Dates or Times 228
6.7
Synthesizing Dates or Times from Component Values 234
6.8
Converting Between Temporal Data Types and Basic Units 236

6.9
Calculating the Interval Between Two Dates or Times 240
6.10
Adding Date or Time Values 245
6.11
Calculating Ages 251
6.12
Shifting a Date-and-Time Value to a Different Time Zone 255
6.13
Finding the First Day, Last Day, or Length of a Month 257
6.14
Calculating Dates by Substring Replacement 259
6.15
Finding the Day of the Week for a Date 260
6.16
Finding Dates for Any Weekday of a Given Week 261
6.17
Performing Leap Year Calculations 264
6.18
Canonizing Not-Quite-ISO Date Strings 267
6.19
Treating Dates or Times as Numbers 268
6.20
Forcing MySQL to Treat Strings as Temporal Values 270
Table of Contents | vii
www.it-ebooks.info
6.21 Selecting Rows Based on Their Temporal Characteristics 271
7. Sorting Query Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
7.0 Introduction 275
7.1 Using ORDER BY to Sort Query Results 276

7.2
Using Expressions for Sorting 280
7.3
Displaying One Set of Values While Sorting by Another 282
7.4
Controlling Case Sensitivity of String Sorts 285
7.5
Date-Based Sorting 288
7.6 Sorting by Calendar Day 290
7.7
Sorting by Day of Week 292
7.8 Sorting by Time of Day 293
7.9 Sorting Using Substrings of Column Values 294
7.10 Sorting by Fixed-Length Substrings 295
7.11
Sorting by Variable-Length Substrings 297
7.12 Sorting Hostnames in Domain Order 302
7.13 Sorting Dotted-Quad IP Values in Numeric Order 304
7.14 Floating Values to the Head or Tail of the Sort Order 306
7.15 Sorting in User-Defined Orders 310
7.16 Sorting ENUM Values 311
8. Generating Summaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
8.0
Introduction 315
8.1
Summarizing with COUNT(  ) 317
8.2
Summarizing with MIN(  ) and MAX(  ) 320
8.3
Summarizing with SUM(  ) and AVG(  ) 321

8.4
Using DISTINCT to Eliminate Duplicates 323
8.5
Finding Values Associated with Minimum and Maximum
Values 325
8.6
Controlling String Case Sensitivity for MIN(  ) and MAX(  ) 327
8.7
Dividing a Summary into Subgroups 329
8.8
Summaries and NULL Values 332
8.9
Selecting Only Groups with Certain Characteristics 335
8.10
Using Counts to Determine Whether Values Are Unique 336
8.11
Grouping by Expression Results 337
8.12
Categorizing Noncategorical Data 338
8.13
Controlling Summary Display Order 342
8.14
Finding Smallest or Largest Summary Values 344
8.15
Date-Based Summaries 346
8.16
Working with Per-Group and Overall Summary Values
Simultaneously 348
8.17
Generating a Report That Includes a Summary and a List 351

viii | Table of Contents
www.it-ebooks.info
9. Obtaining and Using Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
9.0 Introduction 355
9.1 Obtaining the Number of Rows Affected by a Statement 357
9.2 Obtaining Result Set Metadata 359
9.3
Determining Whether a Statement Produced a Result Set 369
9.4 Using Metadata to Format Query Output 370
9.5
Listing or Checking Existence of Databases or Tables 374
9.6
Accessing Table Column Definitions 376
9.7 Getting ENUM and SET Column Information 383
9.8
Using Table Structure Information in Applications 385
9.9
Getting Server Metadata 390
9.10 Writing Applications That Adapt to the MySQL Server Version 391
9.11 Determining the Default Database 392
9.12
Monitoring the MySQL Server 393
9.13 Determining Which Storage Engines the Server Supports 395
10. Importing and Exporting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
10.0 Introduction 397
10.1
Importing Data with LOAD DATA and mysqlimport 401
10.2 Specifying the Datafile Location 403
10.3 Specifying the Structure of the Datafile 405
10.4

Dealing with Quotes and Special Characters 408
10.5
Importing CSV Files 409
10.6
Reading Files from Different Operating Systems 409
10.7
Handling Duplicate Key Values 410
10.8
Obtaining Diagnostics About Bad Input Data 411
10.9
Skipping Datafile Lines 413
10.10
Specifying Input Column Order 414
10.11
Preprocessing Input Values Before Inserting Them 414
10.12
Ignoring Datafile Columns 416
10.13
Exporting Query Results from MySQL 417
10.14
Exporting Tables as Text Files 420
10.15
Exporting Table Contents or Definitions in SQL Format 421
10.16
Copying Tables or Databases to Another Server 423
10.17
Writing Your Own Export Programs 425
10.18
Converting Datafiles from One Format to Another 429
10.19

Extracting and Rearranging Datafile Columns 431
10.20
Using the SQL Mode to Control Bad Input Data Handling 434
10.21
Validating and Transforming Data 436
10.22
Using Pattern Matching to Validate Data 439
10.23
Using Patterns to Match Broad Content Types 441
10.24
Using Patterns to Match Numeric Values 442
10.25
Using Patterns to Match Dates or Times 444
Table of Contents | ix
www.it-ebooks.info
10.26 Using Patterns to Match Email Addresses or URLs 448
10.27 Using Table Metadata to Validate Data 449
10.28 Using a Lookup Table to Validate Data 452
10.29
Converting Two-Digit Year Values to Four-Digit Form 455
10.30
Performing Validity Checking on Date or Time Subparts 456
10.31 Writing Date-Processing Utilities 459
10.32
Using Dates with Missing Components 464
10.33 Importing Non-ISO Date Values 465
10.34
Exporting Dates Using Non-ISO Formats 466
10.35 Importing and Exporting NULL Values 467
10.36 Guessing Table Structure from a Datafile 469

10.37 Exchanging Data Between MySQL and Microsoft Access 472
10.38 Exchanging Data Between MySQL and Microsoft Excel 473
10.39 Exporting Query Results as XML 476
10.40 Importing XML into MySQL 479
10.41 Epilogue 481
11. Generating and Using Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
11.0 Introduction 485
11.1 Creating a Sequence Column and Generating Sequence Values 486
11.2 Choosing the Data Type for a Sequence Column 489
11.3
The Effect of Row Deletions on Sequence Generation 491
11.4
Retrieving Sequence Values 494
11.5
Renumbering an Existing Sequence 498
11.6
Extending the Range of a Sequence Column 500
11.7
Reusing Values at the Top of a Sequence 501
11.8
Ensuring That Rows Are Renumbered in a Particular Order 502
11.9
Starting a Sequence at a Particular Value 503
11.10
Sequencing an Unsequenced Table 505
11.11
Using an AUTO_INCREMENT Column to Create Multiple
Sequences 506
11.12
Managing Multiple Simultaneous AUTO_INCREMENT Values 511

11.13
Using AUTO_INCREMENT Values to Relate Tables 512
11.14
Using Sequence Generators as Counters 515
11.15
Generating Repeating Sequences 519
11.16
Numbering Query Output Rows Sequentially 520
12. Using Multiple Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
12.0
Introduction 521
12.1
Finding Rows in One Table That Match Rows in Another 522
12.2
Finding Rows with No Match in Another Table 530
12.3
Comparing a Table to Itself 535
12.4
Producing Master-Detail Lists and Summaries 540
x | Table of Contents
www.it-ebooks.info
12.5 Enumerating a Many-to-Many Relationship 543
12.6 Finding Rows Containing Per-Group Minimum or Maximum
Values 548
12.7
Computing Team Standings 552
12.8
Using a Join to Fill or Identify Holes in a List 558
12.9 Calculating Successive-Row Differences 563
12.10

Finding Cumulative Sums and Running Averages 565
12.11 Using a Join to Control Query Output Order 569
12.12
Combining Several Result Sets in a Single Query 571
12.13 Identifying and Removing Mismatched or Unattached Rows 576
12.14 Performing a Join Between Tables in Different Databases 579
12.15 Using Different MySQL Servers Simultaneously 580
12.16 Referring to Join Output Column Names in Programs 583
13. Statistical Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587
13.0
Introduction 587
13.1 Calculating Descriptive Statistics 588
13.2 Per-Group Descriptive Statistics 591
13.3 Generating Frequency Distributions 593
13.4 Counting Missing Values 596
13.5 Calculating Linear Regressions or Correlation Coefficients 598
13.6
Generating Random Numbers 600
13.7
Randomizing a Set of Rows 602
13.8
Selecting Random Items from a Set of Rows 605
13.9
Assigning Ranks 606
14. Handling Duplicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 611
14.0
Introduction 611
14.1
Preventing Duplicates from Occurring in a Table 612
14.2

Dealing with Duplicates When Loading Rows into a Table 614
14.3
Counting and Identifying Duplicates 618
14.4
Eliminating Duplicates from a Table 622
14.5
Eliminating Duplicates from a Self-Join Result 626
15. Performing Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
15.0
Introduction 631
15.1
Choosing a Transactional Storage Engine 632
15.2
Performing Transactions Using SQL 634
15.3
Performing Transactions from Within Programs 635
15.4
Using Transactions in Perl Programs 638
15.5
Using Transactions in Ruby Programs 640
15.6
Using Transactions in PHP Programs 641
15.7
Using Transactions in Python Programs 642
Table of Contents | xi
www.it-ebooks.info
15.8 Using Transactions in Java Programs 643
15.9 Using Alternatives to Transactions 644
16. Using Stored Routines, Triggers, and Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647
16.0 Introduction 647

16.1
Creating Compound-Statement Objects 649
16.2
Using a Stored Function to Encapsulate a Calculation 651
16.3
Using a Stored Procedure to “Return” Multiple Values 653
16.4
Using a Trigger to Define Dynamic Default Column Values 654
16.5 Simulating TIMESTAMP Properties for Other Date
and Time Types 657
16.6 Using a Trigger to Log Changes to a Table 659
16.7 Using Events to Schedule Database Actions 662
17. Introduction to MySQL on the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
17.0 Introduction 665
17.1 Basic Principles of Web Page Generation 667
17.2
Using Apache to Run Web Scripts 671
17.3 Using Tomcat to Run Web Scripts 682
17.4
Encoding Special Characters in Web Output 692
18. Incorporating Query Results into Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
18.0
Introduction 701
18.1
Displaying Query Results as Paragraph Text 702
18.2
Displaying Query Results as Lists 704
18.3
Displaying Query Results as Tables 716
18.4

Displaying Query Results as Hyperlinks 721
18.5
Creating a Navigation Index from Database Content 725
18.6
Storing Images or Other Binary Data 730
18.7
Retrieving Images or Other Binary Data 737
18.8
Serving Banner Ads 740
18.9
Serving Query Results for Download 742
18.10
Using a Template System to Generate Web Pages 745
19. Processing Web Input with MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765
19.0
Introduction 765
19.1
Writing Scripts That Generate Web Forms 768
19.2
Creating Single-Pick Form Elements from Database Content 771
19.3
Creating Multiple-Pick Form Elements from Database Content 787
19.4
Loading a Database Record into a Form 792
19.5
Collecting Web Input 797
19.6
Validating Web Input 808
19.7
Storing Web Input in a Database 809

xii | Table of Contents
www.it-ebooks.info
19.8 Processing File Uploads 812
19.9 Performing Searches and Presenting the Results 819
19.10 Generating Previous-Page and Next-Page Links 822
19.11
Generating “Click to Sort” Table Headings 826
19.12
Web Page Access Counting 831
19.13 Web Page Access Logging 835
19.14
Using MySQL for Apache Logging 837
20. Using MySQL-Based Web Session Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845
20.0
Introduction 845
20.1 Using MySQL-Based Sessions in Perl Applications 849
20.2 Using MySQL-Based Storage in Ruby Applications 854
20.3 Using MySQL-Based Storage with the PHP Session Manager 858
20.4 Using MySQL for Session-Backing Store with Tomcat 869
A. Obtaining MySQL Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 879
B. Executing Programs from the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885
C. JSP and Tomcat Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 893
D. References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925
Table of Contents | xiii
www.it-ebooks.info
www.it-ebooks.info
Preface
The MySQL database management system has gained a large following in recent years.
This has been true especially in the Linux and open source communities, but MySQL

has an increasing foothold in the commercial sector as well. MySQL is well liked for
several reasons: it’s fast, and it’s easy to set up, use, and administer. MySQL runs under
many varieties of Unix and Windows, and MySQL-based programs can be written in
many languages. Historically, MySQL has been especially popular for constructing
database-backed web sites that involve dynamic content generation. Moreover, with
the introduction of features in MySQL 5.0 such as views, triggers, and stored proce-
dures and functions, the penetration of MySQL into other areas of application devel-
opment is on the upswing.
With MySQL’s popularity comes the need to address the questions posed by its users
about how to solve specific problems. That is the purpose of MySQL Cookbook. It’s
designed to serve as a handy resource to which you can turn when you need quick
solutions or techniques for attacking particular types of questions that come up when
you use MySQL. Naturally, because it’s a cookbook, it contains recipes: straightfor-
ward instructions you can follow rather than develop your own code from scratch. It’s
written using a problem-and-solution format designed to be extremely practical and to
make the contents easy to read and assimilate. It contains many short sections, each
describing how to write a query, apply a technique, or develop a script to solve a prob-
lem of limited and specific scope. This book doesn’t attempt to develop full-fledged,
complex applications. Instead, it’s intended to assist you in developing such applica-
tions yourself by helping you get past problems that have you stumped.
For example, a common question is, “How can I deal with quotes and special characters
in data values when I’m writing queries?” That’s not difficult, but figuring out how to
do it is frustrating when you’re not sure where to start. This book demonstrates what
to do; it shows you where to begin and how to proceed from there. This knowledge
will serve you repeatedly, because after you see what’s involved, you’ll be able to apply
the technique to any kind of data, such as text, images, sound or video clips, news
articles, compressed files, or PDF documents. Another common question is, “Can I
access data from multiple tables at the same time?” The answer is “Yes,” and it’s easy
to do because it’s just a matter of knowing the proper SQL syntax. But it’s not always
xv

www.it-ebooks.info
clear how until you see examples, which this book gives you. Other things that you’ll
learn from this book include:
• How to use SQL to select, sort, and summarize rows.
• How to find matches or mismatches between rows in two tables.
• How to perform a transaction.
• How to determine intervals between dates or times, including age calculations.
• How to identify or remove duplicate rows.
• How to store images into MySQL and retrieve them for display in web pages.
• How to get LOAD DATA to read your datafiles properly or find which values in the file
are invalid.
• How to use strict mode to prevent entry of bad data into your database.
• How to copy a table or a database to another server.
• How to generate sequence numbers to use as unique row identifiers.
• How to write stored procedures and functions.
• How to use a view as a “virtual table.”
• How to set up triggers that activate to perform specific data-handling operations
when you insert or update table rows.
• How to create database events that execute according to a schedule.
One part of knowing how to use MySQL is understanding how to communicate with
the server—that is, how to use SQL, the language through which queries are formula-
ted. Therefore, one major emphasis of this book is on using SQL to formulate queries
that answer particular kinds of questions. One helpful tool for learning and using SQL
is the mysql client program that is included in MySQL distributions. By using this client
interactively, you can send SQL statements to the server and see the results. This is
extremely useful because it provides a direct interface to SQL. The mysql client is so
useful, in fact, that the entire first chapter is devoted to it.
But the ability to issue SQL queries alone is not enough. Information extracted from a
database often needs to be processed further or presented in a particular way to be
useful. What if you have queries with complex interrelationships, such as when you

need to use the results of one query as the basis for others? Or what if you need to
generate a specialized report with very specific formatting requirements? These prob-
lems bring us to the other major emphasis of the book—how to write programs that
interact with the MySQL server through an application programming interface (API).
When you know how to use MySQL from within the context of a programming lan-
guage, you gain the ability to exploit MySQL’s capabilities in the following ways:
• You can remember the result from a query and use it at a later time.
• You have full access to the expressive power of a general-purpose programming
language. This enables you to make decisions based on success or failure of a query,
xvi | Preface
www.it-ebooks.info
or on the content of the rows that are returned, and then tailor the actions taken
accordingly.
• You can format and display query results however you like. If you’re writing a
command-line script, you can generate plain text. If it’s a web-based script, you
can generate an HTML table. If it’s an application that extracts information for
transfer to some other system, you might generate a datafile expressed in XML.
When you combine SQL with a general purpose programming language, you have an
extremely flexible framework for issuing queries and processing their results. Program-
ming languages increase your capabilities by giving you a great deal of additional power
to perform complex database operations. This doesn’t mean this book is complicated,
though. It keeps things simple, showing how to construct small building blocks by
using techniques that are easy to understand and easily mastered.
I’ll leave it to you to combine these techniques in your own programs, which you can
do to produce arbitrarily complex applications. After all, the genetic code is based on
only four nucleic acids, but these basic elements have been combined to produce the
astonishing array of biological life we see all around us. Similarly, there are only 12
notes in the scale, but in the hands of skilled composers, they can be interwoven to
produce a rich and endless variety of music. In the same way, when you take a set of
simple recipes, add your imagination, and apply them to the database programming

problems you want to solve, you can produce applications that perhaps are not works
of art, but are certainly useful and will help you and others be more productive.
Who This Book Is For
This book should be useful for anybody who uses MySQL, ranging from individuals
who want to use a database for personal projects such as a blog or Wiki, to professional
database and web developers. The book should also appeal to people who do not now
use MySQL, but would like to. For example, it should be useful if you want to learn
about databases but realize that a “big” database system such as Oracle isn’t the best
choice as a learning tool.
If you’re relatively new to MySQL, you’ll probably find lots of ways to use it here that
you hadn’t thought of. If you’re more experienced, you’ll probably be familiar with
many of the problems addressed here, but you may not have had to solve them before
and should find the book a great timesaver; take advantage of the recipes given in the
book, and use them in your own programs rather than figuring out how to write the
code from scratch.
The book also can be useful for people who aren’t even using MySQL. You might
suppose that because this is a MySQL cookbook and not a PostgreSQL cookbook or
an InterBase cookbook that it won’t apply to database systems other than MySQL. To
some extent that’s true, because some of the SQL constructs are MySQL-specific. But
many of the queries use standard SQL that is portable to many other database engines,
Preface | xvii
www.it-ebooks.info
so you should be able to use them with little or no modification. In addition, several
programming language interfaces provide database-independent access methods; you
use them the same way regardless of which type of database server you connect to.
The material ranges from introductory to advanced, so if a recipe describes techniques
that seem obvious to you, skip it. Or if you find that you don’t understand a recipe, it
may be best to set it aside for a while and come back to it later, perhaps after reading
some of the preceding recipes.
More advanced readers may wonder on occasion why, in a book on MySQL, I some-

times provide explanatory material on certain basic topics that are not directly MySQL-
related, such as how to set environment variables. I decided to do this based on my
experience in helping people who are just getting started with MySQL. One thing that
makes MySQL attractive is that it is easy to use, which makes it a popular choice for
people without extensive background in databases. However, many of these same peo-
ple also tend to be thwarted by simple impediments to more effective use of MySQL,
as evidenced by the common question, “How can I avoid having to type the full path-
name of mysql each time I invoke it?” Experienced readers will recognize immediately
that this is simply a matter of setting the PATH environment variable to include the
directory where mysql is installed. But other readers will not, particularly Windows
users who are used to dealing only with a graphical interface and, more recently, Mac
OS X users who find their familiar user interface now augmented by the powerful but
sometimes mysterious command line provided by the Terminal application. If that
describes you, I hope that you’ll find these more elementary sections helpful for knock-
ing down barriers that keep you from using MySQL more easily. If you’re a more
advanced user, just skip over such sections.
What’s in This Book
It’s very likely when you use this book that you’ll have an application in mind you’re
trying to develop but are not sure how to implement certain pieces of it. In this case,
you’ll already know what type of problem you want to solve, so you should search the
table of contents or the index looking for a recipe that shows how to do what you want.
Ideally, the recipe will be just what you had in mind. Failing that, you should be able
to find a recipe for a similar problem that you can adapt to suit the issue at hand. I try
to explain the principles involved in developing each technique so that you’ll be able
to modify it to fit the particular requirements of your own applications.
Another way to approach this book is to just read through it with no specific problem
in mind. This can help you because it will give you a broader understanding of the
things MySQL can do, so I recommend that you page through the book occasionally.
It’s a more effective tool if you have a general familiarity with it and know the kinds of
problems it addresses.

As you get into later chapters, you’ll sometimes find recipes that assume a knowledge
of topics covered in earlier chapters. This also applies within a chapter, where later
xviii | Preface
www.it-ebooks.info
sections often use techniques discussed earlier in the chapter. If you jump into a chapter
and find a recipe that uses a technique with which you’re not familiar, check the table
of contents or the index to find where the technique is covered. You should find that
it’s been explained earlier. For example, if you find that a recipe sorts a query result
using an ORDER BY clause that you don’t understand, turn to Chapter 7, which discusses
various sorting methods and explains how they work.
The following paragraphs summarize each chapter to give you an overview of the book’s
contents.
Chapter 1, Using the mysql Client Program, describes how to use the standard MySQL
command-line client. mysql is often the first or primary interface to MySQL that people
use, and it’s important to know how to exploit its capabilities. This program enables
you to issue queries and see their results interactively, so it’s good for quick experi-
mentation. You can also use it in batch mode to execute canned SQL scripts or send
its output into other programs. In addition, the chapter discusses other ways to use
mysql, such as how to number output lines or make long lines more readable, how to
generate various output formats, and how to log mysql sessions.
Chapter 2, Writing MySQL-Based Programs, demonstrates the basic elements of
MySQL programming: how to connect to the server, issue queries, retrieve the results,
and handle errors. It also discusses how to handle special characters and NULL values in
queries,
how to write library files to encapsulate code for commonly used operations,
and describes various ways to gather the parameters needed for making connections
to the server.
Chapter 3, Selecting Data from Tables, covers several aspects of the SELECT statement,
which is the primary vehicle for retrieving data from the MySQL server: specifying
which columns and rows you want to retrieve, performing comparisons, dealing with

NULL values, and selecting one section of a query result. Later chapters cover some of
these topics in more detail, but this chapter provides an overview of the concepts on
which they depend. You should read it if you need some introductory background on
row selection or you don’t yet know a lot about SQL.
Chapter 4, Table Management, covers table cloning, copying results into other tables,
using temporary tables, and checking or changing a table’s storage engine.
Chapter 5, Working with Strings, describes how to deal with string data. It covers char-
acter sets and collations, string comparisons, dealing with case-sensitivity issues,
pattern matching, breaking apart and combining strings, and performing FULLTEXT
searches.
Chapter 6, Working with Dates and Times, shows how to work with temporal data. It
describes MySQL’s date format and how to display date values in other formats. It also
covers how to use MySQL’s special TIMESTAMP data type, how to set the time zone,
conversion between different temporal units, how to perform date arithmetic to com-
pute intervals or generate one date from another, and leap-year calculations.
Preface | xix

www.it-ebooks.info
Chapter 7, Sorting Query Results, describes how to put the rows of a query result in the
order you want. This includes specifying the sort direction, dealing with NULL values,
accounting for string case sensitivity, and sorting by dates or partial column values. It
also provides examples that show how to sort special kinds of values, such as domain
names, IP numbers, and ENUM values.
Chapter 8, Generating Summaries, shows techniques that are useful for assessing the
general characteristics of a set of data, such as how many values it contains or what its
minimum, maximum, or average values are.
Chapter 9, Obtaining and Using Metadata, discusses how to get information about the
data that a query returns, such as the number of rows or columns in the result, or the
name and type of each column. It also shows how to ask MySQL what databases and
tables are available or find out about the structure of a table and its columns.

Chapter 10, Importing and Exporting Data, describes how to transfer information be-
tween MySQL and other programs. This includes how to convert files from one format
to another, extract or rearrange columns in datafiles, check and validate data, rewrite
values such as dates that often come in a variety of formats, and how to figure out which
data values cause problems when you load them into MySQL with LOAD DATA.
Chapter 11, Generating and Using Sequences, discusses AUTO_INCREMENT columns,
MySQL’s mechanism for producing sequence numbers. It shows how to generate new
sequence values or determine the most recent value, how to resequence a column, how
to begin a sequence at a given value, and how to set up a table so that it can maintain
multiple sequences at once. It also shows how to use AUTO_INCREMENT values to maintain
a master-detail relationship between tables, including some of the pitfalls to avoid.
Chapter 12, Using Multiple Tables, shows how to perform joins, which are operations
that combine rows in one table with those from another. It demonstrates how to com-
pare tables to find matches or mismatches, produce master-detail lists and summaries,
enumerate many-to-many relationships, and update or delete rows in one table based
on the contents of another.
Chapter 13, Statistical Techniques, illustrates how to produce descriptive statistics, fre-
quency distributions, regressions, and correlations. It also covers how to randomize a
set of rows or pick a row at random from the set.
Chapter 14, Handling Duplicates, discusses how to identify, count, and remove dupli-
cate rows—and how to prevent them from occurring in the first place.
Chapter 15, Performing Transactions, shows how to handle multiple SQL statements
that must execute together as a unit. It discusses how to control MySQL’s auto-commit
mode, and how to commit or roll back transactions, and demonstrates some work-
arounds you can use for non-transactional storage engines.
Chapter 16, Using Stored Routines, Triggers, and Events, describes how to write stored
functions and procedures that are stored on the server side, triggers that activate when
tables are modified, and events that execute on a scheduled basis.
xx | Preface
www.it-ebooks.info

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×