The Definitive Guide to MySQL, Second EditionApress © 2004 824 pages Provides a thorough introduction to the installation, configuration, implementation, and administration of MySQL... W
Trang 1The Definitive Guide to MySQL, Second Edition
Apress © 2004 (824 pages)
Provides a thorough introduction to the installation, configuration, implementation, and administration of MySQL.
Trang 3The Definitive Guide to MySQL, Second Edition, is the
perfect all-in-one guide to MySQL Whether you’re new
to Web application development or you’re already an experienced database analyst, this book describes all
of the facets of MySQL configuration, administration, and database application development Greatly
expanded from its widely-praised first edition, this
updated revision provides additional information about MySQL tools such as MySQL Control Center and
the author of Definitive Guide to Excel VBA, Second Edition, published by Apress.
Trang 4Michael Kofler
Translated by David Kramer
The Definitive Guide to MySQL, Second Edition Copyright © 2004 byMichael Kofler
All rights reserved No part of this work may be reproduced or transmitted
in any form or by any means, electronic or mechanical, including
photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner andthe publisher
ISBN (pbk): 1-59059-144-5
Trademarked names may appear in this book Rather than use a
trademark symbol with every occurrence of a trademarked name, we usethe names only in an editorial fashion and to the benefit of the trademarkowner, with no intention of infringement of the trademark
Trang 5The information in this book is distributed on an "as is" basis, withoutwarranty Although every precaution has been taken in the preparation ofthis work, neither the author nor Apress shall have any liability to anyperson or entity with respect to any loss or damage caused or alleged to
be caused directly or indirectly by the information contained in this work.The source code for this book is available to readers at
http://www.apress.com, in the Downloads section You will need toanswer questions pertaining to this book in order to successfully
download the code
About the Author
Trang 6Kofler is also the author of Definitive Guide to Excel VBA, Second
Edition, published by Apress.
About the Translator
DAVID KRAMER EARNED HIS PH.D in mathematics at the University ofMaryland, and his M.A in music at Smith College For many years heworked in academia, first as a professor of mathematics and computerscience, and later as a director of academic computing Over the pasteight years he has edited hundreds of books in mathematics and thesciences, and has translated a number of books in a variety of fields,
including Definitive Guide to Excel VBA, by Michael Kofler; Enterprise JavaBeans 2.1, by Stefan Denninger and Ingo Peters; and Cryptography
in C and C++, by Michael Welschenbach, all published by Apress Other translations include The Game's Afoot! Game Theory in Myth and
Paradox, by Alexander Mehlmann; the children's musical Red Riding! Red Riding!, by Ernst Ekker with music by Sergei Dreznin; In Quest of Tomorrow's Medicines, by Jürgen Drews; and the novel To Err Is Divine,
by Ágota Bozai, which will be published by Counterpoint Press in April2004
Trang 7MySQL is available for many applications free of charge (GPLlicense)
Since the licensing restrictions of GPL are unacceptable for manycommercial applications, there are reasonably priced commerciallicenses and optional support contracts
MySQL is on the verge of repeating in the database market thesuccess achieved by Linux in the operating system sector Incombination with PHP or Perl, MySQL is providing the databasesystem for more and more web sites (A favorite combination isLinux + Apache + MySQL + Perl or PHP Such systems are called
"LAMP systems" for short.) MySQL is not just for small web sites;
it is used by large firms with huge amounts of data, such as
Yahoo!, Slashdot, and Google
Trang 8This book provides a complete application- and example-oriented
introduction to the database system MySQL No previous knowledge,either of SQL or database design, is assumed
The introductory Part I of the book begins with an extensive introduction
on installation under Windows and Linux We also consider the
installation of components that are used in combination with MySQL
(Apache, PHP, phpMyAdmin, Perl, Connector/MyODBC) Building onthis, our first example will show the basic use of MySQL and PHP
Part II, "Fundamentals," provides a large amount of background material
on the use of various user interfaces, on the database language SQL, onthe proper design of databases, on the use of InnoDB tables, on the
access system of MySQL, and on many other topics on administration(such as backups, logging, and replication)
In Part III, "Programming," we emphasize the language PHP: Three
chapters cover basic techniques of programming and show how to
construct two extensive examples (bookkeeping, a discussion forum).Most of the PHP examples can be tried out live on my web site We dealwith other programming languages, too, with chapters devoted to Perl,Java, C/C++, and VB/VB.NET/C#
We end the main text with a reference section (Part IV) that provides anoverview of the SQL commands of MySQL, the commands and options
of the administrative tools, and the functions of important programminginterfaces (PHP, Perl, C, Java)
Finally, there are several appendices, comprising (A) a glossary of terms,(B) breaking news on the forthcoming MySQL version 4.1, (C) information
on the example files for this book (available at www.apress.com), and(D) a bibliography with suggestions for further reading
In combination with the example databases and programs, this book
should provide a good foundation for the development of your own
Trang 9database applications In this I wish you much fun and success Michael Kofler, August 2003 <mysql@kofler.cc> http://www.kofler.cc/mysql
Trang 10There is much that is new This book has been completely revised, fromthe first page to the last (It took as long to produce this second edition as
to write the first edition from scratch!) The following list indicates the mostimportant changes:
MySQL: The book now covers MySQL 4.0 and to some extent
MySQL 4.1 These changes appear throughout the entire book,from installation to the reference section to the appendixes
MySQL 4.0 and 4.1? How can a full-text search be accomplishedefficiently?
establishing connections
Administration: Everything related to administration is contained
in a single clearly structured chapter New sections deal with theadministration of InnoDB tables and server tuning (including
Query Cache)
PHP: All PHP examples have been reworked in PHP 4.3 The
HTML code generated from the PHP examples is now largely
Trang 11development of ADO.NET applications with the programminglanguages VB.NET and C#
Reference: The three reference chapters have been thoroughly
brought up to date and expanded with new sections (C functions,JDBC classes)
MySQL 4.1: Appendix B brings together all known changes inMySQL 4.1: improved character set support (including Unicode),
the use of subSELECTs, the management of geometric and
geographic data (GIS), the use of precompiled commands
(prepared statements), etc.
Trang 12Several chapters in this book deal with the programming of MySQLapplications in a variety of programming languages, such as PHP, Perl,
C, C++, Java, and Visual Basic These chapters assume that the reader
is familiar with the specific programming language (There is simply noroom in this book to offer an introduction to a number of programminglanguages.) In other words, you will profit from, say, the PHP chapter inthis book only if you are already familiar with the programming languagePHP
Trang 13To the extent that we are dealing with web applications, almost all of theapplications in this book can be tried out directly on my website
(www.kofler.cc) The source code for all the examples is available atwww.apress.com in the downloads section
In the longer program listings in this book you will find at the beginning ofthe example a comment line that specifies the file name appearing in theexample files on the web site, for example,
<! php-programming/simpleinput.php >
To save space, sometimes only the most interesting passages in theprogram code are printed
Trang 14The functionality of MySQL and of the programs, programming
languages, and libraries placed in its environment changes with everynew version—which sometimes appear weekly The following overviewindicates which versions I have worked with (explanations of the variousnames will appear at appropriate places in the book):
MySQL: Version 4.0.14 In most of the example programs,
however, compatibility with MySQL 3.23.n was taken into
account, since this version is still in use by many Internet serviceproviders On the other hand, the latest developments of MySQL4.1 were tested with a development version (alpha version, fromend of July 2003), as described in Appendix B
Trang 15Visual Basic, VBA, ADO: Visual Basic programs were
developed and tested with VB6 and VBA6 and ADO versions 2.1and 2.7
Trang 16SQL commands and functions, as well as methods, classes, and keywords in SQL, C, Java, PHP, Perl, VB, etc., generally appear in italic
(e.g., SELECT, mysql_query) (Note, however, that when the background
text is italic, as in the gray "Remark," "Tip," "Caution," and "Pointer"
boxes and some section headings, such commands and functions will appear in a roman font; e.g., SELECT.)
Unix/Linux user names also appear in italic (e.g., root, mysql).
MENU COMMANDS USE CAPS AND SMALL CAPS (e.g., FILE |OPEN)
MySQL options and configuration parameters appear in regularroman type (e.g., default-charset)
observe the following conventions:
\Windows\means the Windows directory (e.g., C:\Windows or
Trang 17Programs\means the directory under Windows for programinstallation (e.g., C:\Programs or D:\Program Files).Mysql\means the MySQL installation directory (e.g., C:\Mysql
or D:\Program Files\Mysql)
Trang 18Many commands will be presented in this book We will be moving backand forth between the Unix/Linux and Windows conventions The
following two commands are equivalent:
root# mysqladmin -u root -h localhost password xxx
> mysqladmin -u root -h localhost password xxx
In each case we have given the system prompt (root# for Unix/Linuxand > for Windows) You type in only what follows the prompt (here in
boldface type) Under Unix/Linux it is possible to divide long inputs over
several lines The lines are separated by means of the backslash symbol
\ We shall often use this convention in this book The following commandthus corresponds to the command above:
Trang 19I have attempted in this book to make as little use of abbreviations aspossible However, there are several abbreviations that will be usedrepeatedly without being introduced anew in each chapter:
Trang 20VBA the Microsoft Office package)
Trang 21Part I: Introduction
Trang 22Chapter 1: What Is MySQL?
Chapter 2: The Test Environment
Chapter 3: Introductory Example (An Opinion Poll)
Trang 23Chapter 1: What Is MySQL?
Trang 24This chapter begins with an overview of the most important conceptsfrom the world of databases and then delves into the possibilities andlimitations of MySQL What is MySQL? What can it do, and what is itunable to do?
In addition to describing the central functions of MySQL, we shall alsodiscuss fully the issue of licensing MySQL When is one permitted to useMySQL without payment, and when is a license required?
Trang 25Before we can answer the central question of this chapter, namely, What
is MySQL? you and I must find a common language Therefore, this
section presents a rudimentary database glossary, without going intogreat detail (If you have already had significant dealings with relationaldatabases, you can skip the next couple of pages in good conscience.)
There is scarcely to be found a term that is less precise than database A
database can be a list of addresses residing in a spreadsheet program(such as Excel), or it can be the administration files of a
telecommunications firm in which several million calls are registereddaily, their charges accurately calculated, monthly bills computed, andwarning letters sent to those who are in arrears A simple database can
be a stand-alone operation (residing locally on a computer for a singleuser), while others may be used simultaneously by thousands of users,with the data parceled out among several computers and dozens of harddrives The size of a database can range from a few kilobytes into theterabytes.[1]
In ordinary usage, the word "database" is used to refer to the actual data,the resulting database files, the database system (such as MySQL orOracle), or a database client (such as a PHP script or a program written
in C++) Thus there arises a great potential for confusion as soon as twopeople begin to converse on the subject of databases
Relations, Database Systems, Servers, and Clients
A database is an ordered collection of data, which is normally stored in one or more associated files The data are structured as tables, where
cross references among tables are possible The existence of such
relations among the tables leads to the database being called a relational database.
Let us clarify matters with an example A database might consist of atable with data on a firm's customers (name, address, etc.), a table withdata on the products the firm offers, and finally, a table containing the
Trang 26in the other two tables (for example, via customer and product numbers).MySQL, Oracle, the Microsoft SQL server, and IBM DB2 are examples of
relational database systems Such a system includes the programs for
managing relational databases Among the tasks of a relational databasesystem are not only the secure storage of data, but also such jobs as theprocessing of commands for querying, analyzing, and sorting existingdata and for storing new data All of this should be able to take place notonly on a single computer, but over a network as well Instead of a
database system we shall often speak of a database server.
Where there are servers, there are clients Every program that is
connected to the database system is called a database client Database
clients have the job of simplifying the use of the database for the enduser No user of a database system in his or her right mind would wish tocommunicate directly with the database server That is much too abstractand inconvenient (Let programmers worry about such direct
communication!) Instead, the user has a right to expect convenient
tables, listboxes, and so on to enable the location of data or to input newdata
Database clients can assume a variety of forms, and indeed, they areoften not recognized by the user as database programs at all Someexamples of this type of client are HTML pages for the display and input
of messages in an on-line discussion group, a traditional program withseveral windows for managing addresses and appointments, and a Perlscript for executing administrative tasks There is thus wide scope fordatabase programming
Relational Versus Object-Oriented Database Systems
Relational databases have dominated the database world for decades,and they are particularly well suited for business data, which usually lendthemselves to structuring in the form of tables Except for the followingtwo paragraphs, this entire book discusses only relational databases(though we shall not always stress this point)
Trang 27databases can store free-standing objects (without having to arrangethem in tables) Although in recent years there has been a trend in thedirection of object-oriented programming languages (such as Object-Store, O2, Caché), object-oriented databases have found only a smallmarket niche
Note that relational databases can be accessed by means of object-oriented programming languages However, that does not turn a
relational database into an object-oriented one Object-oriented databasesystems enable direct access to objects defined in the programming
language in question and the storage of such objects in the databasewithout conversion (persistency) It is precisely this that is not possiblewith relational database systems, in which everything must be structured
modified) However, for efficient use of the data it is necessary that from
Trang 28To create such a list, one formulates queries The result of the query is
again a table; however, it is one that exists in active memory (RAM) andnot on the hard drive
To formulate a query one uses SQL instructions, which are commands forselecting and extracting data The abbreviation SQL stands for
Structured Query Language, which has become a standard in the
formulation of database queries Needless to say, every producer of adatabase system offers certain extensions to this standard, which dilutesthe goal of compatibility among various database systems
necessary for the database file, and second, the index must be updatedeach time the data are altered, and this costs time (Thus an index savestime in the reading of data, but it costs time in entering and altering data
It thus depends on the use to which the data are to be put whether anindex is on the whole a net plus or minus in the quest for efficiency.)
A special case of an index is a primary index,or primary key,which is
distinguished in that the primary index must ensure a unique reference to
a record Often, for this purpose one simply uses a running index number(ID number) Primary indexes play a significant role in relational
databases, and they can speed up access to data considerably
[1]It all started with the megabyte, which is about one million bytes Aterabyte is 1024 gigabytes, which in turn is approximately one thousand
Trang 29"large," while "giga-" is derived from the Greek word for "giant." In turn,
"tera-" is from the Greek word for "monster." It would appear that
numbers once regarded as large, gigantic, or even monstrously hugehave become part of our everyday vocabulary
Trang 30MySQL is a relational database system If you can believe many diehardMySQL fans, MySQL is faster, more reliable, and cheaper—or, simplyput, better—than any other database system (including commercial
systems such as Oracle and DB2) Many MySQL opponents continue tochallenge this viewpoint, going even so far as to assert that MySQL is noteven a relational database system We can safely say that there is alarge bandwidth of opinion
The fact is that there is an ever increasing number of MySQLusers, and the overwhelming majority of them are quite satisfiedwith MySQL Thus for these users we may say that MySQL is
http://www.hughes.com.au/
Trang 31The following list shows the most important properties of MySQL Thissection is directed to the reader who already has some knowledge ofrelational databases We will use some terminology from the relationaldatabase world without defining our terms exactly On the other hand, theexplanations should make it possible for database novices to understand
is, they query data, save changes, etc The clients can run on thesame computer as the server or on another computer
SQL compatibility: MySQL supports as its database language—
as its name suggests—SQL (Structured Query Language) SQL
is a standardized language for querying and updating data andfor the administration of a database
Trang 32Replication: Replication allows the contents of a database to be
copied (replicated) onto a number of computers In practice, this
is done for two reasons: to increase protection against systemfailure (so that if one computer goes down, another can be putinto service) and to improve the speed of database queries
Transactions: In the context of a database system, a transaction
means the execution of several database operations as a block.The database system ensures that either all of the operations arecorrectly executed or none of them This holds even if in the
middle of a transaction there is a power failure, the computercrashes, or some other disaster occurs Thus, for example, it
cannot occur that a sum of money is withdrawn from account A but fails to be deposited in account B due to some type of system
error Transactions also give programmers the possibility of
interrupting a series of already executed commands (a sort ofrevocation) In many situations this leads to a considerable
simplification of the programming process
In spite of popular opinion, MySQL has supported transactions for
a long time One should note here that MySQL can store tables in
a variety of formats The default table format is called MyISAM,and this format does not support transactions But there are anumber of additional formats that do support transactions Themost popular of these is InnoDB, which will be described in itsown chapter
Foreign key constraints: These are rules that ensure that there
Trang 33Programming languages: There are quite a number of APIs
(application programming interfaces) and libraries for the
development of MySQL applications For client programming youcan use, among others, the languages C, C++, Java, Perl, PHP,Python, and Tcl
Platform independence: It is not only client applications that run
under a variety of operating systems; MySQL itself (that is, theserver) can be executed under a number of operating systems.The most important are Apple Macintosh OS X, Linux, MicrosoftWindows, and the countless Unix variants, such as AIX, BSDI,FreeBSD, HP-UX, OpenBSD, Net BSD, SGI Iris, and Sun Solaris
Speed: MySQL is considered a very fast database program This
speed has been backed up by a large number of benchmark
tests (though such tests—regardless of the source—should beconsidered with a good dose of skepticism)
Trang 34Many of the shortcomings listed in this section can be found on the to-dolist of the team of MySQL developers, or have already been
implemented
Pointer The documentation for MySQL is not at all silent on the
subject of shortcomings or missing features There is a quitereadable section in the MySQL documentation on the topic
"How standards-compatible is MySQL?" There you will findextensive information on the points at which MySQL fails tocomply with current standards Often, a reason for theshortcoming is provided, and sometimes as well somepointers on how to get around the difficulty:
http://www.mysql.com/doc/en/Compatibility.html
Note also that all of the shortcomings mentioned here are onthe to-do list of the developers and will be alleviated in futureversions of MySQL
ANSI-SQL/92 Limitations
ANSI SQL/92 is a standardized definition of the database query languageSQL Many commercial database systems are largely compatible withthis standard and also offer many extensions
MySQL is also conspicuous for its countless extensions, but
unfortunately, the compatibility is not so extensive as is the case for otherdatabase systems The points described in greater detail in what follows
(subSELECTs, views, triggers, stored procedures) are examples of the
lack of ANSI-SQL/92 compatibility
These limitations usually make it impossible (or merely very difficult) toadapt existing databases and the associated SQL code from other
database systems into MySQL Conversely, it is almost as difficult to
transfer a MySQL solution to another database system if you have notscrupulously employed only the ANSI-SQL/92-conforming features of
MySQL (The temptation to use the useful proprietary features proves in
Trang 36executed by the server during certain database operations If adatabase is incapable on its own of ensuring referential integrity,then it is usually triggers that are brought into play to assist inthis For example, a trigger can be executed every time a record
is to be deleted, which would test whether this operation is
permissible and prohibit the action if need be
The lack of subSELECTs, views, SPs, and triggers does not greatly
restrict the possibilities open to client programmers, but it does lead to asituation in which the program logic is transferred from the server level tothe client level The result is more complex or expensive client
programming than would otherwise be the case, leading to redundancies
in code and problems with maintenance and alteration of code
Further Limitations
The following limitations actually have nothing to do with ANSI-SQL/92but nonetheless play a significant role in practice
MySQL 4.0 supports a variety of character sets and the
associated sort orders, but at present can have only a single sort order active at a time If the sort order must be changed, MySQL
Trang 37MySQL 4.0 is unable to deal with Unicode or other mutibyte
character strings (Of course, it is possible to treat such data asbinary objects, but there are no functions for dealing with
character strings, in particular, no usable sorting or comparisonalgorithms.) This limitation, too, has been removed in version 4.1
Trang 38Even for connoisseurs of MySQL it is a bit of a challenge to keep track ofwhich version of MySQL is current and which functions are contained inwhich versions This section provides some information on MySQL
numbering
As of March 2002 there are four main versions being worked on by theMySQL development team:
when MySQL 4.1.n will be at a point where it can be
recommended for production use, probably not before 2004
MySQL 5.0.n: The first published version in this series is
expected in 2004 It is completely open as to when the first stable
Trang 39process and that new functions and even incompatible changesare to be expected Although an alpha version is not publisheduntil it contains no known errors, it is highly probable that manyundiscovered errors still lurk within Loss of data during testing of
Production means that the MySQL development community has
the impression that the version is mature and stable enough that
it can be used for mission-critical purposes
According to the MySQL documentation, in production versions, onlycorrections, and no new functionality, are to be expected However, thishas not always held true in the past Of particular note is the case of
MySQL 3.23.n After the version had been declared stable (3.23.32),
there came general support for InnoDB and BDB tables (3.23.34), andlater, integrity rules for InnoDB tables (3.23.44) Furthermore, many minorextensions were introduced As a rule, MySQL developers are pleasedwith such extensions, but at the same time, compatibility problems
among different production versions can arise
In practice, this means that a new MySQL version (that is, n.n.0) always
Trang 40kind In the past, it frequently occurred that particular functions became