The Definitive Guide to MySQL5
Third Edition
Michael Kofler
Translated By David Kramer
The Definitive Guide to MySQL 5 Copyright © 2005 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-535-1
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
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.
Lead Editor: Jason Gilmore
Translator and Editor: David Kramer
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore, Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser
Associate Publisher: Grace Wong Project Manager: Beth Christmas Copy Edit Manager: Nicole LeClerc
Assistant Production Director: Kari Brooks-Copony Production Editor: Laura Cheu
Compositor: Linda Weidemann, Wolf Creek Press Proofreader: April Eddy
Artist: Kinetic Publishing Services, LLC Interior Designer: Van Winkle Design Group Cover Designer: Kurt Krames
Manufacturing Manager: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com.
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, e-mail 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(s) 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.comin the Source Code section.
Contents at a Glance
About the Author
. . . xxiiiAbout the Translator
. . . xxvPreface
. . . xxviiPART 1 ■ ■ ■ Introduction ■ CHAPTER 1 What Is MySQL?
. . . 3■ CHAPTER 2 The Test Environment
. . . 17■ CHAPTER 3 Introductory Example (An Opinion Poll with PHP)
. . . 47PART 2 ■ ■ ■ Administrative Tools and User Interfaces ■ CHAPTER 4 mysql, mysqladmin, and mysqldump
. . . 61■ CHAPTER 5 MySQL Administrator and MySQL Query Browser
. . . 71■ CHAPTER 6 phpMyAdmin
. . . 87■ CHAPTER 7 Microsoft Office, OpenOffice/StarOffice
. . . 117PART 3 ■ ■ ■ Fundamentals ■ CHAPTER 8 Database Design
. . . 137■ CHAPTER 9 An Introduction to SQL
. . . 189■ CHAPTER 10 SQL Recipes
. . . 217■ CHAPTER 11 Access Administration and Security
. . . 263■ CHAPTER 12 GIS Functions
. . . 299■ CHAPTER 13 Stored Procedures and Triggers
. . . 317■ CHAPTER 14 Administration and Server Configuration
. . . 345iii
PART 4 ■ ■ ■ Programming
■ CHAPTER 15 PHP
. . . 403■ CHAPTER 16 Perl
. . . 481■ CHAPTER 17 Java (JDBC and Connector/J)
. . . 507■ CHAPTER 18 C
. . . 527■ CHAPTER 19 Visual Basic 6/VBA
. . . 545■ CHAPTER 20 Visual Basic .NET and C#
. . . 573PART 5 ■ ■ ■ Reference ■ CHAPTER 21 SQL Reference
. . . 599■ CHAPTER 22 MySQL Tools and Options
. . . 667■ CHAPTER 23 API Reference
. . . 693PART 6 ■ ■ ■ Appendixes ■ APPENDIX A Glossary
. . . 723■ APPENDIX B The Files for this Book
. . . 729■ APPENDIX C Bibliography
. . . 731■ INDEX
. . . 733Contents
About the Author
. . . xxiiiAbout the Translator
. . . xxvPreface
. . . xxviiPART 1 ■ ■ ■ Introduction ■ CHAPTER 1 What Is MySQL?
. . . 3What Is a Database?
. . . 3Relations, Database Systems, Servers, and Clients
. . . 3Relational Versus Object-Oriented Database Systems
. . . 4Tables, Records, Fields, Queries, SQL, Index, Keys
. . . 4MySQL
. . . 5Features of MySQL
. . . 5Limitations of MySQL
. . . 7MySQL Version Numbers
. . . 8Alpha, Beta, Gamma, Production (Generally Available)
. . . 9MySQL Functions Ordered by Version Number
. . . 9MySQL Licensing
. . . 11Rights and Duties with Respect to the GPL
. . . 11Use of MySQL with an Open Source License
. . . 12Use of MySQL with a Commercial License
. . . 12Commercial Licenses for Client Libraries (Connector/ODBC, Connector/J, etc.)
. . . 13Client Licenses for the PHP Project, F(L)OSS Exception
. . . 14MySQL Version Names
. . . 14Support Contracts
. . . 15Alternatives to MySQL
. . . 16Summary
. . . 16■ CHAPTER 2 The Test Environment
. . . 17Windows or Unix/Linux?
. . . 17MySQL in Practice (Public Internet Server)
. . . 17Development Environment
. . . 18Installation Under Windows
. . . 18v
Installing Apache 2.0
. . . 18Installing MySQL 5.0
. . . 20Installing PHP 5.0
. . . 23Installing Perl
. . . 25Installation Under SUSE Linux 9.3
. . . 27Installing Apache 2, PHP 5, and Perl
. . . 27Installing MySQL 5.0
. . . 30Installation Under Red Hat Enterprise Linux 4
. . . 31Installing Apache 2
. . . 31Installing MySQL 5
. . . 32Compiling PHP 5
. . . 33Installing Perl 5.8
. . . 36Compiling the MySQL Developer Version (Linux)
. . . 36Installing Bitkeeper
. . . 37Downloading the MySQL Developer Version
. . . 37Compiling MySQL
. . . 37Setting Up the mysql Database for Managing Access Privileges
. . . 37Configuration Files and Init-V Scripts
. . . 37Starting the MySQL Server
. . . 38Configuring Apache
. . . 38Configuration Files
. . . 38Elementary Settings
. . . 39Access Privileges for Individual Directories (.htaccess)
. . . 40Configuring PHP
. . . 43Configuring MySQL
. . . 45Launching the MySQL Server
. . . 45■ CHAPTER 3 Introductory Example (An Opinion Poll with PHP)
. . . 47Overview
. . . 47Assumptions
. . . 47Database Development
. . . 48Executing the mysql Command-Line Interpreter
. . . 49Setting Up the Database
. . . 50Creating Tables
. . . 50Why Make It Complicated, When It Could Be So Much Easier?
. . . 52The Questionnaire
. . . 53Questionnaire Evaluation and Displaying Results
. . . 53The mysql Interface Versus the mysqli Interface
. . . 54Establishing a Connection to the Database
. . . 54Evaluating the Data and Storing It in the Database
. . . 54Displaying the Survey Results
. . . 55Program Code (results.php)
. . . 55The Resulting HTML Code
. . . 57Ideas for Improvements
. . . 57PART 2 ■ ■ ■ Administrative Tools and User Interfaces
■ CHAPTER 4 mysql, mysqladmin, and mysqldump
. . . 61mysql
. . . 61Launching mysql
. . . 62mysql Options
. . . 63Using mysql Interactively
. . . 64Tips for Using mysql Under Unix/Linux
. . . 66Tips for Using mysql Under Windows
. . . 67Processing SQL Files with mysql
. . . 69mysqladmin
. . . 70mysqldump
. . . 70■ CHAPTER 5 MySQL Administrator and MySQL Query Browser
. . . 71Installation
. . . 71Establishing a Connection
. . . 72MySQL Administrator
. . . 73Server Information
. . . 74Service Control
. . . 74Startup Variables
. . . 75User Administration
. . . 75Server Connections
. . . 78Health
. . . 78Server Logs
. . . 78Backup
. . . 78Restore
. . . 80Replication Status
. . . 81Catalogs
. . . 81MySQL Query Browser
. . . 82Input and Execution of SQL Commands
. . . 82Changing Data in SELECT Results
. . . 84History and Bookmarks for SQL Commands
. . . 84Executing Several Commands at Once (Scripts)
. . . 84Stored Procedures
. . . 85MySQL Help
. . . 85■C O N T E N T S vii
■ CHAPTER 6 phpMyAdmin
. . . 87Installation and Configuration
. . . 87Installing phpMyAdmin Files
. . . 88Configuring phpMyAdmin
. . . 88config Authentication
. . . 89http and Cookie Authentication
. . . 91User Management, Securing MySQL
. . . 94Securing MySQL
. . . 94Creating New Users
. . . 95Creating and Editing Databases
. . . 98Creating a Database
. . . 98Editing Existing Tables
. . . 100Setting Foreign Key Rules
. . . 100Documenting the Database Structure
. . . 101Viewing, Inserting, and Editing Data
. . . 102Executing SQL Commands
. . . 104Import and Export
. . . 105Database Backup (SQL File)
. . . 106Exporting Tables (CSV Text File)
. . . 107Importing a Database or Tables (SQL File)
. . . 107Inserting Table Data (Text File)
. . . 108Server Administration
. . . 109Auxiliary Functions
. . . 110Creating a Database for phpMyAdmin
. . . 110SQL Bookmarks and History
. . . 111Saving Information on Relations
. . . 111Creating a PDF Relational Diagram
. . . 113Transformations (Alternative Representation of Column Contents)
. . . 115■ CHAPTER 7 Microsoft Office, OpenOffice/StarOffice
. . . 117Installing Connector/ODBC
. . . 117Setting Up ODBC Data Sources
. . . 118Microsoft Access
. . . 120Importing and Exporting Tables
. . . 122Converter: Access
➤MySQL (exportsql.txt)
. . . 123Microsoft Excel
. . . 125Importing Data with MS Query
. . . 125Connector/J Installation
. . . 127Connector/J
. . . 127Installation
. . . 127OpenOffice/StarOffice Base
. . . 128Creating a Connection to a MySQL Database
. . . 128Tables
. . . 130Queries
. . . 131Forms, Reports, and Additional Functions
. . . 132OpenOffice/StarOffice Data Source View
. . . 133Establishing a Data Source
. . . 134Importing Data
. . . 134PART 3 ■ ■ ■ Fundamentals ■ CHAPTER 8 Database Design
. . . 137Further Reading
. . . 137Table Types
. . . 138MyISAM Tables
. . . 138InnoDB Tables
. . . 139HEAP Tables
. . . 140Temporary Tables
. . . 141Other Table Types
. . . 141Table Files
. . . 142MySQL Data Types
. . . 142Integers (xxxINT)
. . . 142Fixed-Point Numbers (DECIMAL)
. . . 144Date and Time (DATE, TIME, DATETIME, TIMESTAMP)
. . . 145Character Strings (CHAR, VARCHAR, xxxTEXT)
. . . 147Binary Data (xxxBLOB and BIT)
. . . 151Options and Attributes
. . . 152Tips and Tricks on Database Design
. . . 153Rules for Good Database Design
. . . 153Tips for Naming
. . . 153Tips on the Design Process
. . . 153Normalization Rules
. . . 154A First Attempt
. . . 155The First Normal Form
. . . 155Second Normal Form
. . . 156Third Normal Form
. . . 158Normalization Theory
. . . 159Managing Hierarchies
. . . 160Hierarchy Problems
. . . 161Building the Hierarchy Tree
. . . 162Searching for Lower-Ranked Categories in the categories Table
. . . 164Searching for Higher-Ranked Categories in the categories Table
. . . 165■C O N T E N T S ix
Relations
. . . 1661:1 Relations
. . . 1661:n Relations
. . . 167n:m Relations
. . . 168Primary and Foreign Keys
. . . 168Primary Key
. . . 168Foreign Keys
. . . 169Referential Integrity (Foreign Key Constraints)
. . . 170Indexes
. . . 173Ordinary Indexes, Unique Indexes, Primary Indexes
. . . 174Full-Text Index
. . . 175Query and Index Optimization
. . . 176Views
. . . 178The Definition of a View
. . . 178Example Database mylibrary (Library Management)
. . . 180Properties of the Database
. . . 181Properties of the Tables
. . . 181Example Database myforum (Discussion Group)
. . . 183The Discussion Group Database myforum
. . . 183Hierarchies Among Messages
. . . 184Example Database Exceptions (Special Cases)
. . . 185The Table testall
. . . 186The Table text_text
. . . 186The Table test_blob
. . . 186The Table test_date
. . . 186The Table test_enum
. . . 186The Table test_null
. . . 186The Table test_sort1
. . . 186The Table test_sort2
. . . 187The Tables importtable1, importtable2, exporttable
. . . 187■ CHAPTER 9 An Introduction to SQL
. . . 189Introduction
. . . 189DML, DDL, and DCL
. . . 190Simple Queries (SELECT)
. . . 190Determining the Number of Data Records (Lines)
. . . 191Determining the Number of Distinct Data Records (DISTINCT)
. . . 191Column Restriction
. . . 191Limiting the Number of Resulting Records (LIMIT)
. . . 192Determining the Number of Records Suppressed by LIMIT
(SQL_CALC_FOUND_ROWS, FOUND_ROWS)
. . . 192Sorting Records (ORDER BY)
. . . 193Choosing the Sort Order
. . . 194Trying Out Different Sort Orders
. . . 194Selecting Data Records (WHERE, HAVING)
. . . 195Linking Data from Several Tables
. . . 197JOINs Across Two Tables
. . . 197JOINs Across Three or More Tables
. . . 198Uniting Query Results
. . . 200Grouped Queries, Aggregate Functions
. . . 201Aggregate Functions
. . . 201GROUP_CONCAT: Aggregate Functions
. . . 202GROUP BY for Several Columns
. . . 203GROUP BY WITH ROLLUP
. . . 204Altering Data (INSERT, UPDATE, and DELETE)
. . . 205Executing a Backup
. . . 205Inserting Data Records (INSERT)
. . . 206Altering Data Records (UPDATE)
. . . 207Deleting Data Records (DELETE)
. . . 208Creating New Tables, Databases, and Indexes
. . . 209Creating a Database (CREATE DATABASE)
. . . 210Creating Tables (CREATE TABLE)
. . . 210Creating Indexes (CREATE INDEX)
. . . 211Changing the Table Design (ALTER TABLE)
. . . 212Deleting Databases and Tables (DROP)
. . . 213Automatic Table Design Changes (Silent Column Changes)
. . . 213The SHOW Command
. . . 214INFORMATION_SCHEMA Tables
. . . 215■ CHAPTER 10 SQL Recipes
. . . 217Character Strings
. . . 217Basic Functions
. . . 217Changing the Character Set
. . . 218Setting the Client Character Set
. . . 219Pattern Matching
. . . 220Date and Time
. . . 221Syntax for Date and Time
. . . 222Calculating with Date and Time
. . . 223Unix Timestamps
. . . 225Time Zones
. . . 227ENUMs and SETs
. . . 229ENUM
. . . 229SET
. . . 229■C O N T E N T S xi
Variables and Conditionals (IF, CASE)
. . . 230Variables
. . . 230IF Queries
. . . 232CASE Branching
. . . 232Copying Data from One Table to Another
. . . 233Creating a New Table by Copying
. . . 233Copying into Existing Tables
. . . 234Pivot Tables
. . . 234Pivot Tables for Book Titles, Languages, and Categories
. . . 235Pivot Table Query Results by Month
. . . 236SubSELECTs
. . . 237Syntax Variants
. . . 237Examples
. . . 239Ensuring the Integrity of Data
. . . 240Searching for Titles Without Authors
. . . 240Searching for Invalid Publisher Links: Invalid Records in 1 : n Relations
. . . 241Searching for Invalid Links Between Authors and Titles (n : m Relation)
. . . 241Tracking Down Duplicates
. . . 242Bringing a Table into Normal Form
. . . 243Processing the First or Last n Records
. . . 244Searching Data (SELECT)
. . . 244Changing Records (UPDATE and DELETE)
. . . 245Delete All Threads Except the Last 500
. . . 246Selecting Random Records
. . . 246Selection Without a random Column
. . . 246Selection with a random Column
. . . 247Selection on the Basis of an id Column
. . . 247Full-Text Search
. . . 248Fundamentals
. . . 248Book Search
. . . 251Forum Search
. . . 252Locking
. . . 253Syntax
. . . 254GET_LOCK and RELEASE_LOCK
. . . 254Transactions
. . . 255Why Transactions
. . . 255Controlling Transactions
. . . 256Trying Out Transactions
. . . 257Transactions and Locking
. . . 258Isolation Degree for Transactions
. . . 260Error Protection
. . . 261■ CHAPTER 11 Access Administration and Security
. . . 263Introduction
. . . 263Communication Between the Client and the MySQL Server
. . . 263Access Administration
. . . 265First Aid
. . . 268Protecting the MySQL Installation
. . . 269Creating a New Database and User
. . . 270Granting the Right to Create One’s Own Database
. . . 271Oops! I Forgot the root Password!
. . . 272The Internal Workings of the Access System
. . . 273Two-Tiered Access Control
. . . 273Privileges
. . . 273The mysql Database
. . . 276The user Table
. . . 277The user.Host Column
. . . 281The db and host Tables
. . . 284The tables_priv and columns_priv Tables
. . . 286The procs_priv Table
. . . 287Tools for Setting Access Privileges
. . . 287Changing Access Privileges with GRANT and REVOKE
. . . 288Viewing Access Privileges with SHOW GRANT
. . . 289Changing a Password with mysqladmin
. . . 290Secure Password Authentication Since MySQL 4.1
. . . 290Updating the Client Libraries
. . . 290old-passwords Mode
. . . 291Parallel Operation with Old and New Passwords
. . . 291Problems with Establishing a Connection
. . . 292Possible Causes of the Connection Difficulty
. . . 292Further Tips for Error-Checking
. . . 295System Security
. . . 295Security at the System Level
. . . 295Security Measures in the Storage of Critical Data
. . . 296The MySQL Server as Security Risk
. . . 296Running the MySQL Server Without root or Administrator Privileges
. . . . 296Network Security, Firewall
. . . 297■ CHAPTER 12 GIS Functions
. . . 299GIS Data Formats
. . . 299Specifying Coordinates
. . . 299Well-Known Text, Well-Known Binary (OpenGIS)
. . . 300■C O N T E N T S xiii
MySQL’s GIS Implementation
. . . 301Data Types
. . . 302Simple Geometric Functions
. . . 303Function for Spatial Analysis
. . . 306Indexing Geometric Data
. . . 308SQL Examples (the Database glacier)
. . . 309Creating the Table
. . . 309Inserting the Data
. . . 310Querying the Data
. . . 310SQL Examples (the Database opengeodb)
. . . 312Data Source and Import
. . . 313Import
. . . 313Radial Search
. . . 313■ CHAPTER 13 Stored Procedures and Triggers
. . . 317Why Stored Procedures? Why Triggers?
. . . 317Triggers
. . . 318Hello, SP World!
. . . 318The Command Interpreter mysql
. . . 319The MySQL Query Browser
. . . 320SP Implementation
. . . 322SP Administration
. . . 322Creating, Editing, and Deleting SPs
. . . 323Security
. . . 325Backup and Restoration of SPs
. . . 325SP Syntax and Language Elements
. . . 325General Syntax Rules
. . . 326Calling SPs (CALL)
. . . 327Parameters and Return Values
. . . 328Encapsulation of Commands (BEGIN-END)
. . . 329Branching
. . . 331Loops
. . . 331Error Handling (Handlers)
. . . 333Cursors
. . . 334SP Examples
. . . 336Adding a Category
. . . 336Adding a Title
. . . 337Determining Parent Categories
. . . 338Producing a List of Hierarchically Ordered Categories
. . . 339Triggers
. . . 341Creating a Trigger
. . . 342Deleting a Trigger
. . . 342Implementation Details and Administrative Assistance
. . . 342Functional Limitations
. . . 343An Example
. . . 343■ CHAPTER 14 Administration and Server Configuration
. . . 345First Steps
. . . 345Using Administrative Tools
. . . 345Setting the root Password
. . . 346MySQL Server Configuration File
. . . 347Restarting the MySQL Server
. . . 347Basic Configuration of the MySQL Server
. . . 348Directories
. . . 348Communication Settings
. . . 348Default Table Format
. . . 349Default Character Set and Sort Order for New Tables
. . . 349Time Zones
. . . 349The Language for Error Messages
. . . 350SQL Mode
. . . 350Backups
. . . 352Backing Up Databases (mysqldump)
. . . 352Restoring a Database (mysql)
. . . 355Fast Backups (mysqlhotcopy)
. . . 356Database Migration
. . . 358Importing and Exporting Text Files
. . . 360Special Characters in the Imported or Exported File
. . . 360Working with Character Strings, Numbers, Dates, and BLOBs
. . . 360Importing with LOAD DATA INFILE
. . . 361Importing with mysqlimport
. . . 364Exporting with SELECT … INTO OUTFILE
. . . 364Exporting with mysqldump
. . . 366Exporting with mysql in Batch Mode
. . . 366Logging
. . . 368Why Logging?
. . . 369Drawbacks
. . . 369Logging Changes (update log)
. . . 369Errors, Logins, and Slow Queries
. . . 372Administration of Logging Files
. . . 373Replication
. . . 374Introduction
. . . 374Setting Up the Replication Master System
. . . 376Setting Up the Replication Slave System
. . . 378Setting Up a Replication System with LOAD DATA
. . . 379Inside Replication
. . . 380Client Programming
. . . 382■C O N T E N T S xv
Administration of MyISAM Tables
. . . 383myisamchk
. . . 383Using myisamchk
. . . 384Speed Optimization, Memory Usage
. . . 384Shrinking and Optimizing MyISAM Tables
. . . 385Repairing MyISAM Tables
. . . 385Compressing MyISAM Tables (myisampack)
. . . 385Administration of InnoDB Tables
. . . 386Tablespace Administration
. . . 386Logging Files
. . . 390Server Tuning
. . . 393Optimal Memory Management
. . . 394Query Cache
. . . 396ISP Database Administration
. . . 397Working with ssh
. . . 398Working via phpMyAdmin
. . . 398Implementing Custom PHP Scripts for Administration
. . . 398Custom Perl Scripts for Administration
. . . 399PART 4 ■ ■ ■ Programming ■ CHAPTER 15 PHP
. . . 403mysql Functions
. . . 403Establishing a Connection
. . . 404Executing SQL Commands
. . . 405Evaluating SELECT Results
. . . 406Transactions
. . . 409Error Handling and Search
. . . 409mysqli Classes, Methods, and Properties
. . . 410Which Will It Be: mysql or mysqli?
. . . 410Availability Tests
. . . 411Overview of Classes
. . . 411Making the Connection
. . . 411Executing SQL Commands
. . . 413Evaluating SELECT Results (mysqli_result)
. . . 413Executing Several SQL Commands at Once
. . . 415SQL Commands with Parameters (Prepared Statements)
. . . 415Transactions
. . . 418Outsourcing Database Functions into a Class
. . . 418More Security with a Separate Password File
. . . 418Security and Convenience with the MyDb Class
. . . 419Displaying a SELECT Result As a Table
. . . 422Character Strings, Dates, Times, BLOBs, and NULL
. . . 424Character Strings and BLOBs
. . . 424Dates and Times
. . . 425NULL
. . . 426Inserting Linked Data Records
. . . 427Processing Input from Forms
. . . 427Code Structure
. . . 428Creating the Form
. . . 431Validation of Form Data
. . . 435Storing the Form Data
. . . 435Deleting a Title
. . . 438Room for Improvement
. . . 439Pagewise Representation of Search Results
. . . 439Code Structure
. . . 441Author Search
. . . 442Title Search
. . . 443Displaying Search Results
. . . 443Links to Additional Pages
. . . 445Managing Hierarchical Data
. . . 446Code Structure
. . . 447Displaying the Category Tree
. . . 448Inserting Subcategories
. . . 450Deleting Categories and Subcategories
. . . 452Searching for Parent Categories
. . . 453Searching for Subcategories
. . . 454Speed Optimization
. . . 455Ground Rules for Efficient Code Execution
. . . 455Statistical and Benchmarking Help
. . . 456Example: Generating the Category List Efficiently
. . . 457Unicode
. . . 461Setting the HTML and HTTP Coding
. . . 461Binary Data (BLOBs) and Images
. . . 464Fundamentals and Programming Techniques
. . . 465Program Code
. . . 468Stored Procedures
. . . 471SP Administrator
. . . 472Installation
. . . 472Using the Program
. . . 472Code
. . . 475■C O N T E N T S xvii
■ CHAPTER 16 Perl
. . . 481Programming Techniques
. . . 481The Modules DBI and DBD::mysql
. . . 481Establishing a Connection to the Database
. . . 482Executing SQL Commands
. . . 483Evaluating SELECT Queries
. . . 485Character Strings, BLOBs, DATEs, SETs, ENUMs, and NULL
. . . 489DBD::mysql-specific Methods and Attributes
. . . 492Unicode
. . . 493Transactions
. . . 494Error-Handling
. . . 494Example: Deleting Invalid Data Records (mylibrary)
. . . 496CGI Example: Library Management (mylibrary)
. . . 497Book Search (mylibrary-find.pl)
. . . 497Simple Input of New Books (mylibrary-simpleinput.pl)
. . . 500CGI Unicode Example
. . . 503Book Search (mylibrary-find-utf8.pl)
. . . 504Input of New Books (mylibrary-simpleinput-utf8.pl)
. . . 505■ CHAPTER 17 Java (JDBC and Connector/J)
. . . 507Introduction
. . . 507The Java Installation
. . . 507Connector/J Installation
. . . 509Programming Techniques
. . . 511A First Example
. . . 512Establishing the Connection
. . . 513Problems in Establishing a Connection
. . . 515Executing SQL Commands
. . . 516Evaluating SELECT Queries
. . . 518Prepared Statements
. . . 522Transactions
. . . 522Batch Commands
. . . 523Working with Binary Data (BLOBs)
. . . 523■ CHAPTER 18 C
. . . 527The C API (libmysqlclient)
. . . 527Hello, World!
. . . 527Assumptions
. . . 527Introductory Example
. . . 528Compiling and Linking
. . . 529Makefile
. . . 530. . . 530
Establishing the Connection
. . . 532Evaluation of the Configuration File my.cnf
. . . 532Evaluation of Options in the Command Line
. . . 532Executing SQL Commands
. . . 533Simple Commands
. . . 533Executing Several Commands at a Time
. . . 535Prepared Statements
. . . 538Character Set Settings (Unicode)
. . . 542Working with Binary Data and Special Characters
. . . 542Storing Binary Data in a Database and Reading It
. . . 543Error Handling
. . . 544■ CHAPTER 19 Visual Basic 6/VBA
. . . 545Fundamentals and Terminology
. . . 545Connector/ODBC Options
. . . 546ADO Programming and Visual Basic/VBA
. . . 548Introduction
. . . 548Establishing the Connection
. . . 550Establishing the Connection with a DSN
. . . 550Establishing a Connection Without a DSN
. . . 550ADO Programming Techniques
. . . 553Example: authors Column for the titles Table
. . . 559Example: Adding a New Book Title
. . . 562Example: Storing an Image File in BLOB Format and Then Reading It
. . . 565Converter: Microsoft SQL Server to MySQL
. . . 567Properties of mssql2mysql
. . . 567Assumptions
. . . 567Restrictions
. . . 568How to Use It
. . . 568Setting Parameters
. . . 568VBMySQLDirect
. . . 570Installation
. . . 570Application
. . . 571Example
. . . 571■ CHAPTER 20 Visual Basic .NET and C#
. . . 573Communication Between ADO.NET and MySQL
. . . 573Establishing a Connection with Connector/Net
. . . 574Establishing the Connection with the ODBC Data Provider
. . . 578■C O N T E N T S xix
Programming Techniques
. . . 580Executing SQL Commands (MySqlCommand)
. . . 580Commands with Parameters (MySql Parameters)
. . . 581Evaluating Individual SELECT Results (ExecuteScalar)
. . . 582Reading SELECT Results (MySqlDataReader)
. . . 583DataSet, DataTable, MySqlDataAdapter
. . . 585Auxiliary Methods
. . . 587Error-Handling
. . . 587Windows.Forms and ASP.NET Controls
. . . 588Transactions
. . . 589Example: Storing New Titles in mylibrary
. . . 589Example: Storing an Image File in a BLOB and Then Reading It
. . . 592PART 5 ■ ■ ■ Reference ■ CHAPTER 21 SQL Reference
. . . 599Syntax
. . . 599Object Names
. . . 599Case Sensitivity
. . . 600Character Strings
. . . 600Character Set and Sort Order
. . . 601Numbers
. . . 602Automatic Transformation of Numbers and Character Strings
. . . 602Date and Time
. . . 602Binary Data
. . . 603Binary Numbers
. . . 603Comments
. . . 603Semicolons at the End of SQL Commands
. . . 603Operators
. . . 604Arithmetic Operators, Bit Operators
. . . 605Comparison Operators
. . . 605Pattern Matching with LIKE
. . . 605Pattern Matching with REGEXP
. . . 605Binary Character String Comparison
. . . 606Logical Operators
. . . 606Variables and Constants
. . . 607Variable Assignment
. . . 607Evaluating and Displaying Variables
. . . 607Global System Variables versus System Variables at the Connection Level
. . . 608SET PASSWORD
. . . 608Structured Variables
. . . 608MySQL Data Types
. . . 609Command Overview (Thematic)
. . . 611Command Reference (Alphabetical)
. . . 614Function Reference
. . . 652Arithmetic Functions
. . . 652Comparison Functions, Tests, Branching
. . . 653Type Conversion (Cast)
. . . 653String Processing
. . . 654Date and Time
. . . 656GROUP BY Functions
. . . 661Additional Functions
. . . 662GIS Data Types and Functions
. . . 663Language Elements for Stored Procedures and Triggers
. . . 665■ CHAPTER 22 MySQL Tools and Options
. . . 667Overview
. . . 667Common Options and Configuration Files
. . . 667Common Options
. . . 668Setting Options in Configuration Files
. . . 669Memory Specifications
. . . 671Options in Environment Variables (aka System Variables)
. . . 671Rules for Specifying Options
. . . 671mysqld (Server)
. . . 672Basic Settings
. . . 672Logging, Replication
. . . 674InnoDB Configuration
. . . 677Miscellaneous
. . . 679mysqld_safe (Server Start)
. . . 679mysql_install_db (New Installation of the mysql Database)
. . . 680mysql_fix_privileges (Updating the mysql Database)
. . . 680mysql_fix_extensions (Renaming MyISAM Files)
. . . 681mysql (SQL Command Interpreter)
. . . 681mysqladmin (Administration)
. . . 683mysqldump (Backup/Export)
. . . 684mysqlimport (Text Import, Bulk Import)
. . . 687mysqlshow (Displaying Information)
. . . 688Example
. . . 688myisamchk (Repairing MyISAM Files)
. . . 689myisampack (Compressing MyISAM Files)
. . . 690■C O N T E N T S xxi
■ CHAPTER 23 API Reference
. . . 693PHP API (mysql Interface)
. . . 693PHP-API (mysqli Interface)
. . . 697The mysqli Class
. . . 697mysqli_result Classes
. . . 698mysqli_stmt Class
. . . 699Perl DBI
. . . 699Common Variable Names
. . . 700Establishing the Connection
. . . 700Executing SQL Commands, Evaluating SELECT Queries
. . . 701Error Handling
. . . 703Auxiliary Functions
. . . 704MySQL-Specific Extension of the DBD::mysql Driver
. . . 704JDBC (Connector/J)
. . . 706Establishing a Connection
. . . 706Executing SQL Commands
. . . 707Processing SELECT Results (ResultSet Class)
. . . 708Transactions
. . . 709ADO.NET (Connector/Net)
. . . 710Establishing a Connection, Connection Properties
. . . 710Executing and Evaluating SQL Commands
. . . 710Altering Data in DataSet/DataTable
. . . 712Transactions
. . . 712C API
. . . 713Data Structures
. . . 713Connection and Administration
. . . 715Executing and Evaluating SQL Commands
. . . 717Prepared Statements
. . . 719PART 6 ■ ■ ■ Appendixes
■ APPENDIX A Glossary
. . . 723■ APPENDIX B The Files for this Book
. . . 729Trying Out the Example Programs
. . . 729Updates, Errata, Links, Discussion Forum
. . . 729■ APPENDIX C Bibliography
. . . 731■ INDEX
. . . 733About 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.
xxiii
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 higher education, first as a professor of math- ematics and computer science, and later as a director of academic computing. Since 1995 he has worked as an independent editor and translator. He has edited hundreds of books in mathematics and the sciences and has translated a number of books in a variety of fields, including The Defini- tive Guide to Excel VBA, by Michael Kofler; Cryptography in C and C++, by Michael Welschenbach;
and Enterprise JavaBeans 2.1, by Stefan Denninger and Ingo Peters, all published by Apress; Luck, Logic, and White Lies, by Jörg Bewersdorff; The Game’s Afoot! Game Theory in Myth and Paradox, by Alexander Mehlmann; and the novel To Err Is Divine, by Ágota Bozai.
xxv
Preface
M
ySQL 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 increasingly used as the database system for 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 NASA.
What Does This Book Offer?
This book provides a complete application- and example-oriented introduction to the database system MySQL. No previous knowledge of SQL or database design is assumed.
The introductory part of the book begins with an extensive introduction on installing MySQL, Apache, PHP, and Perl under Windows and Linux. We also consider the installation of components that are used in combination with MySQL. Building on this, our first example will show the basic use of MySQL and PHP.
The second part of the book introduces the most important administrative tools and user interfaces: mysql, mysqladmin, mysqldump; the programs MySQL Administrator, MySQL Query Browser; and finally phpMyAdmin. The latter program is particularly well suited for off-site admin- istration using a web browser. A separate chapter shows how Microsoft Office, Sun StarOffice, and OpenOffice can be used to access MySQL databases.
Part 3 provides a large amount of background material on the database language SQL, the proper design of databases, stored procedures, MySQL’s access system, and on many other topics on administration (such as backups, logging, and replication).
In Part 4, “Programming,” we emphasize the language PHP: In a long chapter you will learn about techniques of programming. A number of example programs show how to use the interfaces mysql and mysqli (new in PHP 5). We deal with other programming languages, too, with chapters devoted to Perl, Java, C, Visual Basic 6, as well as VB.NET and C#.
xxvii
We end the main text with a reference section (Part 5) 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, ADO.NET).
Finally, there are several appendices, comprising (A) a glossary of terms, (B) information on the example files for this book (available at www.apress.com), and 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.
What Is New in This Third Edition?
This edition is an extensive revision of the second edition. Most of the changes relate to changes in the MySQL server from version 4.1 to version 5.0. But there is also much that is new in areas surrounding MySQL, including new programming interfaces (e.g., mysqli in PHP 5) and new administrative tools.
The most important new developments are collected in the following list.
Changes from MySQL 4.1 to 5.0
• Support for Unicode and other new character sets (additions and changes throughout the book).
• Views (a new section in Chapter 8).
• INFORMATION_SCHEMA tables (a new section in Chapter 9).
• SubSELECTs (a new section in Chapter 10).
• Improved password authentication and additional privileges for access administration (Chapter 11).
• GIS functions (new Chapter 12).
• Stored procedures and triggers (new Chapter 13).
• New ways of administering InnoDB tables (a new section in Chapter 14).
• Various new SQL commands, functions, and data types (changes and additions in the entire book, with a reference in Chapter 21).
• Various new options for the MySQL server and its administrative tools (additions and changes in the entire book, with a reference in Chapter 22).
Changes in Surrounding Areas
• MySQL Administrator and MySQL Query Browser (new Chapter 5).
• Many new functions in phpMyAdmin (new Chapter 6).
• Database interface in OpenOffice/StarOffice (three new sections in Chapter 7).
• The object-oriented interface mysqli in PHP 5 (new Chapter 15, reference in Chapter 23).
• New functions in the C-API (Chapter 18, reference in Chapter 23).
• New ADO.NET driver Connector/Net (Chapter 20, reference in Chapter 23).
What Does This Book Not Offer?
Several chapters in this book deal with the programming of MySQL applications in a variety of programming languages, such as PHP, Perl, C, Java, and Visual Basic. These chapters assume that the reader is familiar with the specific programming language. (There is simply no room in this book to offer an introduction to a number of programming languages.) In other words, you will profit from, say, the PHP chapter in this book only if you are already familiar with the program- ming language PHP.
Example Programs, Source Code
The source code for all of the examples is available at www.apress.comin the downloadssection.
In the longer program listings in this book you will find at the beginning of the example a com- ment line that specifies the file name appearing in the example files on the web site, for example,
<!— php/titleform.php —>
To save space, sometimes only the most interesting passages in the program code are printed.
Versions
The functionality of MySQL and of the programs, programming languages, and libraries placed in its environment changes with every new version—which sometimes appear weekly. The following sum- mary indicates which versions I have worked with (explanations of the various names will appear at appropriate places in the book):
Apache: Version 2.n.
Connector/J: Versions 3.1.7 and 3.2.0.
Connector/ODBC (formerly MyODBC): Version 3.51.11.
gcc: Version 3.3.
Java: Versions 1.4.2 and 1.5.0.
Linux: MySQL and other programs were tested for this book under Linux and Windows. Under Linux, the distributions Red Hat Enterprise 4 and SUSE Professional 9.2 and 9.3 beta were used.
Microsoft Office: Office 2000.
Microsoft Visual Basic, VBA, ADO: Visual Basic programs were developed and tested with VB6, VBA6, and ADO version 2.8.
Microsoft Visual Basic .NET, C#, ADO.NET: Information presented is based on Visual Studio 2005 beta (.NET Framework 2.0).
Microsoft Windows: All tests under Windows were carried out with Windows XP SP2. The infor- mation given should also, in principle, be valid for Windows 2000 and for future versions of Windows.
MySQL: Version 5.0.3.
OpenOffice: Version 2.0 beta.
Perl: Version 5.8.
PHP: Versions 5.0 and 5.3.
phpMyAdmin: Version 2.6.1.
■P R E FA C E xxix
Notation
• SQL commands and functions, as well as methods, classes, and key words in SQL, C, Java, PHP, Perl, VB, etc., generally appear in italic (e.g., SELECT, mysql_query).
• Unix/Linux user names also appear in italic (e.g., root, mysql).
• MENU COMMANDS USECAPS AND SMALL CAPS(e.g., FILE| OPEN).
• File and directory names use a monospace font (e.g., /usr/localor C:\Windows).
• Programs and programming commands are in the monospace font as well (e.g., mysqlor cmd.exe).
• Program listings and command line input also appear in the monospace font.
SQL commands are generally written in UPPERCASE letters. This is not a syntactic necessity, but merely a typographical convention. MySQL does not distinguish between uppercase and lower- case in interpreting SQL commands.
In specifying Windows directories, we will often not write out the absolute path, since it depends in any case on the particular installation. We observe the following conventions:
Windows\means the Windows directory (e.g., C:\Windowsor D:\WinNT4).
Programs\means the directory under Windows for program installation (e.g., C:\Programsor D:\Program Files).
Mysql\means the MySQL installation directory (e.g., C:\Program Files\Mysql).
Commands
Many commands will be presented in this book. We will be moving back and 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/Linux and >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 command thus corresponds to the command above:
root# mysqladmin -u root -h localhost \ password xxx
In each case, xxxis to be replaced by the relevant text (in this example by your password). I have indicated that xxxis dummy text by the use of a slant font.
Abbreviations
I have attempted in this book to make as little use of abbreviations as possible. However, there are several abbreviations that will be used repeatedly without being introduced anew in each chapter:
ADO Active Data Objects (Microsoft database library).
ADO.NET ADO for .NET (incompatible with ADO!).
BLOB Binary Large Object (binary data block).
GIS Geographical information system.
GPL GNU Public License (important license for Open Source software).
HTML HyperText Markup Language (format for describing web documents).
InnoDB Not an abbreviation, but the name of a company that has developed a special table format for MySQL (InnoDB tables).
ISP Internet Service Provider.
MySQL The name of the company that developed the database system MySQL.
ODBC Open Database Connectivity (interface for database access, particularly popular under Windows).
PHP PHP Hypertext Preprocessor (a scripting programming language for HTML pages).
RPM Red Hat Packet Manager (a format for Linux software packages).
SQL Structured Query Language (database programming language).
URL Uniform Resource Locator (Internet address of the form http://www.company.com/
page.html).
VB Visual Basic (programming language).
VBA Visual Basic for Applications (programming language within the Microsoft Office package).
VBA.NET Visual Basic for Applications (programming language within the Microsoft Office package).
■P R E FA C E xxxi
Introduction
P A R T 1
■ ■ ■
What Is MySQL?
T
his chapter begins with an overview of the most important concepts from the world of databases and then delves into the possibilities and limitations of MySQL. What is MySQL? What can it do, and what is it unable to do?In addition to describing the central functions of MySQL, we shall also discuss fully the issue of licensing MySQL. When is one permitted to use MySQL without payment, and when is a license required?
What Is a Database?
Before 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 into great detail. (If you have already had significant dealings with relational databases, 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 registered daily, their charges accu- rately calculated, monthly bills computed, and warning letters sent to those who are in arrears.
A simple database can be a stand-alone operation (residing locally on a computer for a single user), while others may be used simultaneously by thousands of users, with the data parceled out among several computers and dozens of hard drives. The size of a database can range from a few kilobytes into the terabytes.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 or Oracle), 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 two people 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 a table with data on a firm’s customers (name, address, etc.), a table with data on the products the firm offers, and finally, a table
3
C H A P T E R 1
■ ■ ■
1 It all started with the megabyte, which is about one million bytes. A terabyte is 1024 gigabytes, which in turn is approximately one thousand megabytes. The prefix “mega-” comes from the Greek for “great,” or
“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 huge have
containing the firm’s orders. Through the table of orders it is possible to access the data in 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 sys- tems. Such a system includes the programs for managing relational databases. Among the tasks of a relational database system are not only the secure storage of data, but also such jobs as the processing of commands for querying, analyzing, and sorting existing data and for storing new data. All of this should be able to take place not only 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 sys- tem is called a database client. Database clients have the job of simplifying the use of the database for the end user. No user of a database system in his or her right mind would wish to communicate directly with the database server. That is much too abstract and 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 new data.
Database clients can assume a variety of forms, and indeed, they are often not recognized by the user as database programs at all. Some examples of this type of client are HTML pages for the display and input of messages in an on-line discussion group, a traditional program with several windows for managing addresses and appointments, and a Perl script for executing administrative tasks. There is thus wide scope for database 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 lend themselves to structuring in the form of tables. Except for the following two paragraphs, this entire book discusses only relational databases (though we shall not always stress this point).
Another kind of database is the object-oriented database. Such databases can store free-standing objects (without having to arrange them in tables). Although in recent years there has been a trend in the direction of object-oriented programming languages (such as Object-Store, O2, Caché), object- oriented databases have found only a small market 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 database systems enable direct access to objects defined in the programming language in question and the storage of such objects in the database without conversion (persistency). It is precisely this that is not possible with relational database systems, in which everything must be structured in tables.
Tables, Records, Fields, Queries, SQL, Index, Keys
We have already mentioned tables, which are the structures in which the actual data are located.
Every line in such a table is called a data record, or simply record, where the structure of each record is determined by the definition of the table. For example, in a table of addresses every record might contain fields for family name, given name, street, and so on. For every field there are precise condi- tions on the type of information that can be stored (such as a number in a particular format, or a character string with a predetermined maximum number of characters).
The description of a database consisting of several tables with all of its fields, relations, and indexes (see below) is called a database model. This model defines the construction of the data structures and at the same time provides the format in which the actual data are to be stored.
Tables usually contain their data in no particular order (more precisely, the order is usually that in which the data have been entered or modified). However, for efficient use of the data it is neces- sary that from these unordered data a list can be created that is ordered according to one or more
. For
example, one could obtain a list of all of one’s customers, ordered by ZIP code, who have ordered a rubber ducky within the past twelve months.
To 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) and not on the hard drive.
To formulate a query one uses SQL instructions, which are commands for selecting 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 a database system offers certain extensions to this standard, which dilutes the goal of compatibility among various database systems.
When tables get large, the speed at which a query can be answered depends significantly on whether there is a suitable index giving the order of the data fields. An index is an auxiliary table that contains only information about the order of the records. An index is also called a key.
An index speeds up access to data, but it has disadvantages as well. First, every index increases the amount of storage on the hard drive necessary for the database file, and second, the index must be updated each time the data are altered, and this costs time. (Thus an index saves time in the read- ing 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 an index 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.
MySQL
MySQL is a relational database system. If you can believe many diehard MySQL fans, MySQL is faster, more reliable, and cheaper—or, simply put, better—than any other database system (includ- ing commercial systems such as Oracle and DB2). Many MySQL opponents continue to challenge this viewpoint, going even so far as to assert that MySQL is not even a relational database system.
We can safely say that there is a large bandwidth of opinion.
• The fact is that there is an ever increasing number of MySQL users, and the overwhelming majority of them are quite satisfied with MySQL. Thus for these users we may say that MySQL is good enough.
• It is also the fact, however, that MySQL still lacks a number of features that are taken for granted with other database systems. If you require such features, then MySQL is (at least for the present) not the database system for you. MySQL is not a panacea.
Next we shall examine some of the possibilities and limitations of MySQL.
Features of MySQL
The following list shows the most important properties of MySQL. This section is directed to the reader who already has some knowledge of relational databases. We will use some terminology from the relational database world without defining our terms exactly. On the other hand, the explanations should make it possible for database novices to understand to some extent what we are talking about.
Relational Database System: Like almost all other database systems on the market, MySQL is a relational database system.
Client/Server Architecture: MySQL is a client/server system. There is a database server (MySQL) and arbitrarily many clients (application programs), which communicate with the server; that is, they query data, save changes, etc. The clients can run on the same computer k or the Internet).
C H A P T E R 1 ■ W H AT I S M YS Q L ? 5
Almost all of the familiar large database systems (Oracle, Microsoft SQL Server, etc.) are client/
server systems. These are in contrast to the file-server systems, which include Microsoft Access, dBase, and FoxPro. The decisive drawback to file-server systems is that when run over a net- work, they become extremely inefficient as the number of users grows.
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 and for the administration of a database.
There are several SQL dialects (about as many as there are database systems). MySQL adheres to the current SQL standard (at the moment SQL:2003), although with significant restrictions and a large number of extensions.
Through the configuration setting sql-modeyou can make the MySQL server behave for the most part compatibly with various database systems. Among these are IBM DB/2 and Oracle. (The set- ting sql-mode changes some of the syntax conventions, and performs no miracles. More details are to be had in Chapter 14.)
A readable and entertaining article on the topic of how MySQL differs from other current data- base systems can be found at http://sql-info.de/mysql/gotchas.html.
SubSELECTs: Since version 4.1, MySQL is capable of processing a query in the form SELECT * FROM table1 WHERE x IN (SELECT y FROM table2)
(There are also numerous syntax variants for subSELECTs.)
Views: Put simply, views relate to an SQL query that is viewed as a distinct database object and makes possible a particular view of the database. MySQL has supported views since version 5.0.
Stored procedures: Here we are dealing with SQL code that is stored in the database sys- tem. Stored procedures (SPs for short) are generally used to simplify certain steps, such as inserting or deleting a data record. For client programmers this has the advantage that they do not have to process the tables directly, but can rely on SPs. Like views, SPs help in the adminis- tration of large database projects. SPs can also increase efficiency. MySQL has supported SPs since version 5.0.
Triggers: Triggers are SQL commands that are automatically executed by the server in certain database operations (INSERT, UPDATE, and DELETE). MySQL has supported triggers in a lim- ited form from version 5.0, and additional functionality is promised for version 5.1.
Unicode: MySQL has supported all conceivable character sets since version 4.1, including Latin-1, Latin-2, and Unicode (either in the variant UTF8 or UCS2).
User interface: There are a number of convenient user interfaces for administering a MySQL server.
Full-text search: Full-text search simplifies and accelerates the search for words that are located within a text field. If you employ MySQL for storing text (such as in an Internet discus- sion group), you can use full-text search to implement simply an efficient search function.
Replication: 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 system failure (so that if one computer goes down, another can be put into 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 are correctly executed or none of them. This holds even if in the middle of a transaction there is a power failure, the computer crashes, 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 of revocation). 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 a number of additional formats that do support transactions. The most popular of these is InnoDB, which will be described extensively in this book.
Foreign key constraints: These are rules that ensure that there are no cross references in linked tables that lead to nowhere. MySQL supports foreign key constraints for InnoDB tables.
GIS functions: Since version 4.1, MySQL has supported the storing and processing of two- dimensional geographical data. Thus MySQL is well suited for GIS (geographic information systems) applications.
Programming languages: There are quite a number of APIs (application programming inter- faces) and libraries for the development of MySQL applications. For client programming you can use, among others, the languages C, C++, Java, Perl, PHP, Python, and Tcl.
ODBC: MySQL supports the ODBC interface Connector/ODBC. This allows MySQL to be addressed by all the usual programming languages that run under Microsoft Windows (Delphi, Visual Basic, etc.). The ODBC interface can also be implemented under Unix, though that is seldom necessary.
Windows programmers who have migrated to Microsoft’s new .NET platform can, if they wish, use the ODBC provider or the .NET interface Connector/NET.
Platform independence: It is not only client applications that run under a variety of operating systems; MySQL itself (that is, the server) can be executed under a number of operating systems.
The most important are Apple Macintosh OS X, Linux, Microsoft Windows, 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 be con- sidered with a good dose of skepticism).
Limitations of MySQL
Many of the shortcomings listed in this section can be found on the to-do list of the team of MySQL developers, or have already been implemented.
■ Tip
The documentation for MySQL is not at all silent on the subject of shortcomings or missing features. There is a quite readable section in the MySQL documentation on the topic “How standards-compatible is MySQL?”There you will find extensive information on the points at which MySQL fails to comply with current standards.
Often, a reason for the shortcoming is provided, and sometimes as well some pointers on how to get around the difficulty:http://www.mysql.com/doc/en/Compatibility.html.
• When MySQL is used with standard tables (table type MyISAM), then locking, that is, the temporary blocking of access to or alteration of database information, is in operation only for entire tables (table locking). You can circumvent the table-locking problem by imple- menting transaction-capable table formats, such as InnoDB, that support row locking.
C H A P T E R 1 ■ W H AT I S M YS Q L ? 7