The Definitive Guide to MySQL, Second Edition ISBN:1590591445 by Michael Kofler Apress © 2004 (824 pages) Provides a thorough introduction to the installation, configuration, implementation, and administration of MySQL Table of Contents The Definitive Guide to MySQL, Second Edition Preface Part I - Introduction Chapter 1 Chapter 2 Chapter 3 - What Is MySQL? - The Test Environment - Introductory Example (An Opinion Poll) Part II - Fundamentals Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 - User Interfaces - Database Design - An Introduction to SQL - SQL Recipes - InnoDB Tables and Transactions - Security - Administration and Server Configuration Part III - Programming PHP — Fundamentals and Programming Techniques Chapter 12 - PHP — Library Management Chapter 13 - PHP — Discussion Forum Chapter 11 - Chapter 14 Chapter 15 Chapter 16 Chapter 17 - Perl - Java, JSP, JDBC - C and C++ - Visual Basic, C#, ODBC Part IV - Reference Chapter 18 - SQL Reference Chapter 19 - MySQL Tools Chapter 20 - API Reference Part V - Appendices Appendix A - Glossary Appendix B - MySQL 4.1 Appendix C - Example Files Appendix D - Bibliography Index List of Figures List of Tables Back Cover The 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 phpMyAdmin, database APIs from several popular programming languages available with Linux and Windows and the latest features found in MySQL 4.1 About the Author Michael Kofler earned his PH.D in computer science at Graz Technical University He has written a number of successful computer books on topics such as Visual Basic, Linux, Mathematica, and Maple Kofler is also the author of Definitive Guide to Excel VBA, Second Edition, published by Apress The Definitive Guide to MySQL, Second Edition Michael Kofler Translated by David Kramer The Definitive Guide to MySQL, Second Edition Copyright © 2004 by Michael 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 and the 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 use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark Translator, Editor, and Compositor: David Kramer Editorial Board: Dan Appleman Craig Berry Gary Cornell Tony Davis Steven Rycroft Julian Skinner Martin Streicher Jim Sumser Karen Watterson Gavin Wray John Zukowski Assistant Publisher: Grace Wong Production Manager: Kari Brooks Proofreader: Elizabeth Berry Cover Designer: Kurt Krames Manufacturing Manager: Tom Debolski Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 175 Fifth Avenue, New York, NY, 10010 and outside the United States by Springer-Verlag GmbH & Co KG, Tiergartenstr 17, 69112 Heidelberg, Germany In the United States: phone 1-800-SPRINGER, email orders@springer-ny.com, or visit http://www.springerny.com Outside the United States: fax +49 6221 345229, email orders@springer.de, or visit http://www.springer.de For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710 Phone 510-549-5930, fax 510-549-5939, email info@apress.com, or visit http://www.apress.com The information in this book is distributed on an "as is" basis, without warranty Although every precaution has been taken in the preparation of this work, neither the author nor Apress shall have any liability to any person 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 to answer questions pertaining to this book in order to successfully download the code About the Author MICHAEL KOFLER EARNED HIS PH.D in computer science at Graz Technical University He has written a number of successful computer books on topics such as Visual Basic, Linux, Mathematica, and Maple Kofler 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 of Maryland, and his M.A in music at Smith College For many years he worked in academia, first as a professor of mathematics and computer science, and later as a director of academic computing Over the past eight years he has edited hundreds of books in mathematics and the sciences, 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 April 2004 Preface MYSQL IS THE MOST WIDELY used database system in the Open Source sector There are many reasons why this is so: MySQL is fast MySQL is stable MySQL is easy to learn MySQL runs on popular operating systems (Windows, Linux, Mac OS X, various flavors of Unix) MySQL applications can be created in a great variety of programming languages (such as C, C++, C#, Java, Perl, PHP, Python, VB, and VB.NET) MySQL is extensively documented on the Internet, and there are many books on the subject available MySQL is available for many applications free of charge (GPL license) Since the licensing restrictions of GPL are unacceptable for many commercial applications, there are reasonably priced commercial licenses and optional support contracts MySQL is on the verge of repeating in the database market the success achieved by Linux in the operating system sector In combination with PHP or Perl, MySQL is providing the database system for more and more web sites (A favorite combination is Linux + 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 What Does This Book Offer? This 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 on this, 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, on the 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 deal with 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 an overview of the SQL commands of MySQL, the commands and options of the administrative tools, and the functions of important programming interfaces (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 database applications In this I wish you much fun and success Michael Kofler, August 2003 http://www.kofler.cc/mysql What Is New in the Second Edition? There is much that is new This book has been completely revised, from the 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 most important 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 User interface: This book describes the new user interface MySQL Control Center and the latest version of phpMyAdmin SQL recipes: There is a new chapter in the introduction on SQL that provides answers to frequently asked SQL questions: How can I process the first/last n data records? How can duplicate records be located? How are subSELECT commands formed in MySQL 4.0 and 4.1? How can a full-text search be accomplished efficiently? InnoDB tables, transactions, rules for integrity: A new chapter is devoted to the InnoDB table format InnoDB tables enable the execution of transactions with row level locking and the protection of the integrity of relations between tables (foreign key constraints) Security, access rights: Chapter 9 deals with all the innovations dealing with access rights (MySQL privileges) A new section gives a number of practical tips on solving problems with establishing connections Administration: Everything related to administration is contained in a single clearly structured chapter New sections deal with the administration 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 Chapter 12: PHP — Library Management Figure 12-1: The search form Figure 12-2: Results of a search for book titles beginning with the letter M Figure 12-3: Results of a search for authors whose last name begins with K Figure 12-4: Links to additional pages with search results Figure 12-5: Simple book title input Figure 12-6: Input of a new book title (phase 1) Figure 12-7: Checking the input (phase 2) Figure 12-8: Listbox for languages Figure 12-9: Hierarchical listbox for selecting the book category Figure 12-10: Selection or new input of a publisher name in the second input phase Figure 12-11: Select or delete a category Figure 12-12: Adding the category Visual Basic in the group All books | Computer books | Programming Chapter 13: PHP — Discussion Forum Figure 13-1: Login dialog for the myforum discussion forum Figure 13-2: Establishing a new myforum account Figure 13-3: List of all discussion forums Figure 13-4: List of all postings to testforum Figure 13-5: A single posting Figure 13-6: An entire discussion thread Figure 13-7: Form for composing a message Figure 13-8: Hierarchical representation of three threads (ordered by flow of the discussion) Figure 13-9: Flat representation of the ten most recent messages from Figure 13-8 (ordered by time ofcreation) Figure 13-10: Hierarchical representation of the most recent ten messages (ordered according to the discussion flow) Figure 13-11: Representation of messages via forumread.php Chapter 14: Perl Figure 14-1: Searching for book titles Figure 14-2: Simple book title input Chapter 15: Java, JSP, JDBC Figure 15-1: A first JSP example Figure 15-2: JSP example with traditional Java code Chapter 17: Visual Basic, C#, ODBC Figure 17-1: Determining the Connector/ODBC version number Figure 17-2: Basic Connector/ODBC setting Figure 17-3: Connector/ODBC Options Figure 17-4: Selection of the tables to be imported into Access Figure 17-5: Excel pivot table Figure 17-6: Selecting the desired columns in MS Query Figure 17-7: MS Query in development mode Figure 17-8: DataEnvironment dialog for setting the connection properties Figure 17-9: Visual Basic program to test Recordset properties Figure 17-10: Visual Basic example program with the MSHFlexGrid for representing data from the mylibrary database Figure 17-11: Visual Basic example program for changing the titles table Figure 17-12: Visual Basic example program for inserting a new book title Figure 17-13: The photograph is loaded from a BLOB field and displayed Figure 17-14: ADO.NET example program Figure 17-15: Form for storing a new book title Figure 17-16: Graphical user interface for mssql2mysql List of Tables Chapter 1: What Is MySQL? Table 1-1: MySQL functions past and future Chapter 4: User Interfaces Table 4-1: mysql commands in interactive mode Chapter 5: Database Design Table 5-1: A book database: first attempt Table 5-2: A book database: first normal form Table 5-3: title table: second normal form Table 5-4: author table: second normal form Table 5-5: title table: second normal form Table 5-6: author table: second normal form Table 5-7: rel_title_author table: second normal form Table 5-8: title table: third normal form Table 5-9: publisher table: third normal form Table 5-10: Database representation of the hierarchy in Figure 5-3 Table 5-11: Query result in the mylibrary database Table 5-12: categories table with hierNr column Table 5-13: Some entries in the rel_cat_parent table Table 5-14: messages table with records of a discussion thread Table 5-15: messages table with records of a discussion thread Chapter 7: SQL Recipes Table 7-1: Boolean search expressions (IN BOOLEAN MODE) Chapter 8: InnoDB Tables and Transactions Table 8-1: Coordinating transactions Chapter 9: Security Table 9-1: MySQL privileges Table 9-2: Structure of the user table Table 9-3: Default setting for the user table under Unix/Linux Table 9-4: Default setting for the user table under Windows Table 9-5: Structure of the db table Table 9-6: Default setting of the db table under Unix/Linux Table 9-7: Structure of the host table Table 9-8: Structure of the tables_priv table Table 9-9: Structure of the columns_priv table Chapter 10: Administration and Server Configuration Table 10-1: Query cache status variables Chapter 11: PHP — Fundamentals and Programming Techniques Table 11-1: Superglobal fields (available since PHP 4.1) Chapter 15: Java, JSP, JDBC Table 15-1: Components of the connection URL Table 15-2: Optional parameters in the connection URL (with default settings) ... Kofler is also the author of Definitive Guide to Excel VBA, Second Edition, published by Apress The Definitive Guide to MySQL, Second Edition Michael Kofler Translated by David Kramer The Definitive Guide to MySQL, Second Edition Copyright © 2004 by... the 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 most important changes: MySQL: The book now covers MySQL 4.0 and to some extent... List of Figures List of Tables Back Cover The 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