• No results found

All books in the Developer’s Library are written by expert technology practitioners who are especially skilled at organizing and presenting information in a way that’s useful for other programmers

N/A
N/A
Protected

Academic year: 2021

Share "All books in the Developer’s Library are written by expert technology practitioners who are especially skilled at organizing and presenting information in a way that’s useful for other programmers"

Copied!
1326
0
0

Loading.... (view fulltext now)

Full text

(1)www.it-ebooks.info.

(2) MySQL Fourth Edition. www.it-ebooks.info. ®.

(3) Developer’s Library ESSENTIAL REFERENCES FOR PROGRAMMING PROFESSIONALS. Developer’s Library books are designed to provide practicing programmers with unique, high-quality references and tutorials on the programming languages and technologies they use in their daily work. All books in the Developer’s Library are written by expert technology practitioners who are especially skilled at organizing and presenting information in a way that’s useful for other programmers. Key titles include some of the best, most widely acclaimed books within their topic areas: PHP & MySQL Web Development. Python Essential Reference. Luke Welling & Laura Thomson ISBN 978-0-672-32916-6. David Beazley ISBN-13: 978-0-672-32862-6. MySQL. Programming in Objective-C. Paul DuBois ISBN-13: 978-0-672-32938-8. Stephen G. Kochan ISBN-13: 978-0-321-56615-7. Linux Kernel Development. PostgreSQL. Robert Love ISBN-13: 978-0-672-32946-3. Korry Douglas ISBN-13: 978-0-672-33015-5. Developer’s Library books are available at most retail and online bookstores, as well as by subscription from Safari Books Online at safari.informit.com.. Developer’s Library informit.com/devlibrary. www.it-ebooks.info.

(4) MySQL. ®. Fourth Edition Paul DuBois. Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Cape Town • Sydney • Tokyo • Singapore • Mexico City. www.it-ebooks.info.

(5) MySQL ® Fourth Edition. Acquisitions Editor Mark Taber. Copyright © 2009 by Pearson Education, Inc. All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein.. Development Editor Michael Thurston. ISBN-13: 978-0-672-32938-8 ISBN-10: 0-672-32938-7. Indexer Cheryl Lenser. Library of Congress Cataloging-in-Publication Data DuBois, Paul, 1956MySQL / Paul DuBois. — 4th ed. p. cm. Includes index. ISBN 978-0-672-32938-8 (pbk.) 1. SQL (Computer program language) 2. MySQL (Electronic resource) 3. Database management. I. Title. QA76.73.S67D588 2009 005.13’3—dc22 2008030855. Printed in the United States of America First Printing August 2008. Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Pearson cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark.. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book.. Bulk Sales Pearson offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 [email protected] For sales outside of the U.S., please contact International Sales [email protected]. www.it-ebooks.info. Managing Editor Kristy Hart Project Editor Jovana San Nicolas-Shirley. Proofreaders Leslie Joseph Water Crest Publishing Technical Editors Stephen Frein Tim Boronczyk Publishing Coordinator Vanessa Evans Cover Designer Gary Adair Compositor Jake McFarland.

(6) Contents at a Glance Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 I: General MySQL Use 1 Getting Started with MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2 Using SQL to Manage Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 3 Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 4 Stored Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 5 Query Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 II: Using MySQL Programming Interfaces 6 Introduction to MySQL Programming . . . . . . . . . . . . . . . . . . . . . . . . 341 7 Writing MySQL Programs Using C . . . . . . . . . . . . . . . . . . . . . . . . . . 359 8 Writing MySQL Programs Using Perl DBI. . . . . . . . . . . . . . . . . . . . . . 435 9 Writing MySQL Programs Using PHP . . . . . . . . . . . . . . . . . . . . . . . . 527 III: MySQL Administration 10 Introduction to MySQL Administration . . . . . . . . . . . . . . . . . . . . . . . 579 11 The MySQL Data Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585 12 General MySQL Administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609 13 Access Control and Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699 14 Database Maintenance, Backups, and Replication . . . . . . . . . . . . . . 737 IV: Appendixes A Obtaining and Installing Software . . . . . . . . . . . . . . . . . . . . . . . . . . 777 B C D E F. Data Type Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797 Operator and Function Reference . . . . . . . . . . . . . . . . . . . . . . . . . . 813 System, Status, and User Variable Reference. . . . . . . . . . . . . . . . . . 889 SQL Syntax Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937 MySQL Program Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1037. Note: Appendixes G, H, and I are located online and are accessible either by registering this book at informit.com/register or by visiting www.kitebird.com/mysql-book.. G C API Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1121 H Perl DBI API Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1177 I PHP API Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1207 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1225. www.it-ebooks.info.

(7) Table of Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Why Choose MySQL? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Already Running Another RDBMS? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Tools Provided with MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 What You Can Expect from This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Road Map to This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Part I: General MySQL Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Part II: Using MySQL Programming Interfaces . . . . . . . . . . . . . . . . . . . . . . . 6 Part III: MySQL Administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Part IV: Appendixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Versions of Software Covered in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Additional Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11. I: General MySQL Use 1 Getting Started with MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.1 How MySQL Can Help You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2 A Sample Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.2.1 The U.S. Historical League . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.2.2 The Grade-Keeping Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1.2.3 How the Sample Database Applies to You . . . . . . . . . . . . . . . . . . . . 20 1.3 Basic Database Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.3.1 Structural Terminology. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 1.3.2 Query Language Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.3.3 MySQL Architectural Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.4 A MySQL Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.4.1 Obtaining the Sample Database Distribution . . . . . . . . . . . . . . . . . . 26 1.4.2 Preliminary Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 1.4.3 Establishing and Terminating Connections to the MySQL Server . . . . 28 1.4.4 Executing SQL Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 1.4.5 Creating a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 1.4.6 Creating Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34. www.it-ebooks.info.

(8) Contents. 1.4.7 Adding New Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 1.4.8 Resetting the sampdb Database to a Known State . . . . . . . . . . . . . 57 1.4.9 Retrieving Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 1.4.10 Deleting or Updating Existing Rows . . . . . . . . . . . . . . . . . . . . . . . . 91 1.5 Tips for Interacting with mysql . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 1.5.1 Simplifying the Connection Process . . . . . . . . . . . . . . . . . . . . . . . . . 93 1.5.2 Issuing Statements with Less Typing . . . . . . . . . . . . . . . . . . . . . . . . 95 1.6 Where to Now? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100. 2 Using SQL to Manage Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 2.1 The Server SQL Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 2.2 MySQL Identifier Syntax and Naming Rules . . . . . . . . . . . . . . . . . . . . . . 103 2.3 Case Sensitivity in SQL Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 2.4 Character Set Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 2.4.1 Specifying Character Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 2.4.2 Determining Character Set Availability and Current Settings. . . . . . . 109 2.4.3 Unicode Support. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 2.5 Selecting, Creating, Dropping, and Altering Databases . . . . . . . . . . . . . . 112 2.5.1 Selecting Databases. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 2.5.2 Creating Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 2.5.3 Dropping Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 2.5.4 Altering Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 2.6 Creating, Dropping, Indexing, and Altering Tables . . . . . . . . . . . . . . . . . . 114 2.6.1 Storage Engine Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 2.6.2 Creating Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 2.6.3 Dropping Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 2.6.4 Indexing Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 2.6.5 Altering Table Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 2.7 Obtaining Database Metadata. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 2.7.1 Obtaining Metadata with SHOW . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 2.7.2 Obtaining Metadata with INFORMATION_SCHEMA . . . . . . . . . . . . 147 2.7.3 Obtaining Metadata from the Command Line . . . . . . . . . . . . . . . . . 149 2.8 Performing Multiple-Table Retrievals with Joins . . . . . . . . . . . . . . . . . . . . 150 2.8.1 The Inner Join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 2.8.2 Qualifying References to Columns from Joined Tables . . . . . . . . . . . 153 2.8.3 Left and Right (Outer) Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154. www.it-ebooks.info. vii.

(9) viii. Contents. 2.9 Performing Multiple-Table Retrievals with Subqueries . . . . . . . . . . . . . . . 158 2.9.1 Subqueries with Relative Comparison Operators . . . . . . . . . . . . . . . 159 2.9.2 IN and NOT IN Subqueries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 2.9.3 ALL, ANY, and SOME Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . 161 2.9.4 EXISTS and NOT EXISTS Subqueries . . . . . . . . . . . . . . . . . . . . 162 2.9.5 Correlated Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 2.9.6 Subqueries in the FROM Clause . . . . . . . . . . . . . . . . . . . . . . . . . . 163 2.9.7 Rewriting Subqueries as Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 2.10 Performing Multiple-Table Retrievals with UNION . . . . . . . . . . . . . . . . . 165 2.11 Using Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 2.12 Multiple-Table Deletes and Updates. . . . . . . . . . . . . . . . . . . . . . . . . . . 173 2.13 Performing Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 2.13.1 Using Transactions to Ensure Safe Statement Execution . . . . . . . . 176 2.13.2 Using Transaction Savepoints . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 2.13.3 Transaction Isolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 2.13.4 Non-Transactional Approaches to Transactional Problems . . . . . . . 182 2.14 Foreign Keys and Referential Integrity . . . . . . . . . . . . . . . . . . . . . . . . . 185 2.14.1 Creating and Using Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . 187 2.14.2 Living Without Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 2.15 Using FULLTEXT Searches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 2.15.1 Natural Language FULLTEXT Searches. . . . . . . . . . . . . . . . . . . . 196 2.15.2 Boolean Mode FULLTEXT Searches . . . . . . . . . . . . . . . . . . . . . . 197 2.15.3 Query Expansion FULLTEXT Searches . . . . . . . . . . . . . . . . . . . . 199 2.15.4 Configuring the FULLTEXT Search Engine . . . . . . . . . . . . . . . . . . 200. 3 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 3.1 Data Value Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 3.1.1 Numeric Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 3.1.2 String Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 3.1.3 Date and Time (Temporal) Values. . . . . . . . . . . . . . . . . . . . . . . . . . 213 3.1.4 Spatial Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 3.1.5 Boolean Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 3.1.6 The NULL Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 3.2 MySQL Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 3.2.1 Overview of Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 3.2.2 Specifying Column Types in Table Definitions . . . . . . . . . . . . . . . . . 217 3.2.3 Specifying Column Default Values . . . . . . . . . . . . . . . . . . . . . . . . . 218. www.it-ebooks.info.

(10) Contents. 3.2.4 Numeric Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 3.2.5 String Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 3.2.6 Date and Time Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 3.2.7 Spatial Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 3.3 How MySQL Handles Invalid Data Values . . . . . . . . . . . . . . . . . . . . . . . . 252 3.4 Working with Sequences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 3.4.1 General AUTO_INCREMENT Properties . . . . . . . . . . . . . . . . . . . . . 254 3.4.2 Storage Engine-Specific AUTO_INCREMENT Properties . . . . . . . . . 256 3.4.3 Issues to Consider with AUTO_INCREMENT Columns . . . . . . . . . . 259 3.4.4 Tips for Working with AUTO_INCREMENT Columns . . . . . . . . . . . . 260 3.4.5 Generating Sequences Without AUTO_INCREMENT . . . . . . . . . . . . 262 3.5 Expression Evaluation and Type Conversion . . . . . . . . . . . . . . . . . . . . . . 264 3.5.1 Writing Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 3.5.2 Type Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 3.6 Choosing Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 3.6.1 What Kind of Values Will the Column Hold? . . . . . . . . . . . . . . . . . . 282 3.6.2 Do Your Values Lie Within Some Particular Range? . . . . . . . . . . . . . 285 3.6.3 Inter-Relatedness of Data Type Choice Issues . . . . . . . . . . . . . . . . 286. 4 Stored Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 4.1 Compound Statements and Statement Delimiters . . . . . . . . . . . . . . . . . 290 4.2 Stored Functions and Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 4.2.1 Privileges for Stored Functions and Procedures. . . . . . . . . . . . . . . . 294 4.2.2 Stored Procedure Parameter Types. . . . . . . . . . . . . . . . . . . . . . . . . 295 4.3 Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 4.4 Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 4.5 Security for Stored Programs and Views . . . . . . . . . . . . . . . . . . . . . . . . 300. 5 Query Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 5.1 Using Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 5.1.1 Benefits of Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 5.1.2 Costs of Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 5.1.3 Choosing Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 5.2 The MySQL Query Optimizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 5.2.1 How the Optimizer Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 5.2.2 Using EXPLAIN to Check Optimizer Operation . . . . . . . . . . . . . . . . 316 5.3 Choosing Data Types for Efficient Queries . . . . . . . . . . . . . . . . . . . . . . . 322. www.it-ebooks.info. ix.

(11) x. Contents. 5.4 Loading Data Efficiently . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 5.5 Scheduling and Locking Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 5.5.1 Changing Statement Scheduling Priorities . . . . . . . . . . . . . . . . . . . 331 5.5.2 Using Delayed Inserts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 5.5.3 Using Concurrent Inserts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 5.5.4 Locking Levels and Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . 333 5.6 Administrative-Level Optimizations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 5.6.1 Using MyISAM Key Caches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336 5.6.2 Using the Query Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 5.6.3 Hardware Optimizations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339. II: Using MySQL Programming Interfaces 6 Introduction to MySQL Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 341 6.1 Why Write Your Own MySQL Programs? . . . . . . . . . . . . . . . . . . . . . . . . . 341 6.2 APIs Available for MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 6.2.1 The C API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 6.2.2 The Perl DBI API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 6.2.3 The PHP API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 6.3 Choosing an API. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 6.3.1 Execution Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 6.3.2 Performance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 6.3.3 Development Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 6.3.4 Portability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357. 7 Writing MySQL Programs Using C . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 7.1 Compiling and Linking Client Programs . . . . . . . . . . . . . . . . . . . . . . . . . 360 7.2 Connecting to the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 7.3 Handling Errors and Processing Command Options . . . . . . . . . . . . . . . . 367 7.3.1 Checking for Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 7.3.2 Getting Connection Parameters at Runtime . . . . . . . . . . . . . . . . . . 371 7.3.3 Incorporating Option-Processing into a MySQL Client Program . . . . . 384 7.4 Processing SQL Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389 7.4.1 Handling Statements That Modify Rows . . . . . . . . . . . . . . . . . . . . . 390 7.4.2 Handling Statements That Return a Result Set. . . . . . . . . . . . . . . . 391 7.4.3 A General-Purpose Statement Handler . . . . . . . . . . . . . . . . . . . . . . 394 7.4.4 Alternative Approaches to Statement Processing . . . . . . . . . . . . . . 396 7.4.5 mysql_store_result() Versus mysql_use_result() . . . . 398. www.it-ebooks.info.

(12) Contents. 7.4.6 Using Result Set Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 7.4.7 Encoding Special Characters and Binary Data . . . . . . . . . . . . . . . . 405 7.5 An Interactive Statement-Execution Program . . . . . . . . . . . . . . . . . . . . . 409 7.6 Writing Clients That Include SSL Support. . . . . . . . . . . . . . . . . . . . . . . . 410 7.7 Using the Embedded Server Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . 416 7.7.1 Writing an Embedded Server Application . . . . . . . . . . . . . . . . . . . . 416 7.7.2 Producing the Application Executable Binary . . . . . . . . . . . . . . . . . . 419 7.8 Using Multiple-Statement Execution. . . . . . . . . . . . . . . . . . . . . . . . . . . . 420 7.9 Using Server-Side Prepared Statements . . . . . . . . . . . . . . . . . . . . . . . . 422. 8 Writing MySQL Programs Using Perl DBI . . . . . . . . . . . . . . . . . . . . . . . 435 8.1 Perl Script Characteristics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436 8.2 Perl DBI Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 8.2.1 DBI Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 8.2.2 A Simple DBI Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 8.2.3 Handling Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 8.2.4 Handling Statements That Modify Rows . . . . . . . . . . . . . . . . . . . . . 446 8.2.5 Handling Statements That Return a Result Set. . . . . . . . . . . . . . . . 447 8.2.6 Quoting Special Characters in Statement Strings . . . . . . . . . . . . . . 457 8.2.7 Placeholders and Prepared Statements . . . . . . . . . . . . . . . . . . . . . 460 8.2.8 Binding Query Results to Script Variables. . . . . . . . . . . . . . . . . . . . 463 8.2.9 Specifying Connection Parameters . . . . . . . . . . . . . . . . . . . . . . . . . 464 8.2.10 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468 8.2.11 Using Result Set Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471 8.2.12 Performing Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475 8.3 Putting DBI to Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477 8.3.1 Generating the Historical League Directory . . . . . . . . . . . . . . . . . . . 478 8.3.2 Sending Membership Renewal Notices. . . . . . . . . . . . . . . . . . . . . . 484 8.3.3 Historical League Member Entry Editing . . . . . . . . . . . . . . . . . . . . . 490 8.3.4 Finding Historical League Members with Common Interests . . . . . . 496 8.3.5 Putting the Historical League Directory Online . . . . . . . . . . . . . . . . 497 8.4 Using DBI in Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500 8.4.1 Setting Up Apache for CGI Scripts . . . . . . . . . . . . . . . . . . . . . . . . . 502 8.4.2 A Brief CGI.pm Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503 8.4.3 Connecting to the MySQL Server from Web Scripts . . . . . . . . . . . . . 510 8.4.4 A Web-Based Database Browser . . . . . . . . . . . . . . . . . . . . . . . . . . 513 8.4.5 A Grade-Keeping Project Score Browser . . . . . . . . . . . . . . . . . . . . . 517. www.it-ebooks.info. xi.

(13) xii. Contents. 8.4.6 Historical League Common-Interest Searching . . . . . . . . . . . . . . . . 521. 9 Writing MySQL Programs Using PHP . . . . . . . . . . . . . . . . . . . . . . . . . . 527 9.1 PHP Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529 9.1.1 A Simple PHP Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531 9.1.2 Using PHP Library Files for Code Encapsulation . . . . . . . . . . . . . . . 534 9.1.3 A Simple Data-Retrieval Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539 9.1.4 Processing Statement Results. . . . . . . . . . . . . . . . . . . . . . . . . . . . 543 9.1.5 Testing for NULL Values in Query Results . . . . . . . . . . . . . . . . . . . 547 9.1.6 Using Prepared Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547 9.1.7 Using Placeholders to Handle Data Quoting Issues. . . . . . . . . . . . . 548 9.1.8 Handling Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550 9.2 Putting PHP to Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552 9.2.1 An Online Score-Entry Application . . . . . . . . . . . . . . . . . . . . . . . . . 552 9.2.2 Creating an Interactive Online Quiz . . . . . . . . . . . . . . . . . . . . . . . . 565 9.2.3 Historical League Online Member Entry Editing . . . . . . . . . . . . . . . . 570. III: MySQL Administration 10 Introduction to MySQL Administration . . . . . . . . . . . . . . . . . . . . . . . . . 579 10.1 MySQL Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580 10.2 General MySQL Administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581 10.3 Access Control and Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582 10.4 Database Maintenance, Backups, and Replication . . . . . . . . . . . . . . . . 582. 11 The MySQL Data Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585 11.1 Location of the Data Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586 11.2 Structure of the Data Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587 11.2.1 How the MySQL Server Provides Access to Data. . . . . . . . . . . . . . 588 11.2.2 Representation of Databases in the Filesystem . . . . . . . . . . . . . . 590 11.2.3 Representation of Tables in the Filesystem . . . . . . . . . . . . . . . . . 590 11.2.4 Representation of Views and Triggers in the Filesystem . . . . . . . . 592 11.2.5 How SQL Statements Map onto Table File Operations . . . . . . . . . . 592 11.2.6 Operating System Constraints on Database Object Names . . . . . . 593 11.2.7 Factors That Affect Maximum Table Size. . . . . . . . . . . . . . . . . . . . 596 11.2.8 Implications of Data Directory Structure for System Performance. . 597 11.2.9 MySQL Status and Log Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599 11.3 Relocating Data Directory Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . 602. www.it-ebooks.info.

(14) Contents. 11.3.1 Relocation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602 11.3.2 Relocation Precautions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603 11.3.3 Assessing the Effect of Relocation. . . . . . . . . . . . . . . . . . . . . . . . 603 11.3.4 Relocating the Entire Data Directory. . . . . . . . . . . . . . . . . . . . . . . 604 11.3.5 Relocating Individual Databases . . . . . . . . . . . . . . . . . . . . . . . . . 604 11.3.6 Relocating Individual Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606 11.3.7 Relocating the InnoDB Shared Tablespace . . . . . . . . . . . . . . . . . . 606 11.3.8 Relocating Status and Log Files. . . . . . . . . . . . . . . . . . . . . . . . . . 607. 12 General MySQL Administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609 12.1 Securing a New MySQL Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . 610 12.1.1 Establishing Passwords for the Initial MySQL Accounts . . . . . . . . . 610 12.1.2 Setting Up Passwords for a Second Server. . . . . . . . . . . . . . . . . . 615 12.2 Arranging for MySQL Server Startup and Shutdown . . . . . . . . . . . . . . . 616 12.2.1 Running the MySQL Server On Unix . . . . . . . . . . . . . . . . . . . . . . . 616 12.2.2 Running the MySQL Server On Windows. . . . . . . . . . . . . . . . . . . . 621 12.2.3 Specifying Server Startup Options . . . . . . . . . . . . . . . . . . . . . . . . 624 12.2.4 Stopping the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626 12.2.5 Regaining Control of the Server When You Cannot Connect to It . . 626 12.3 Controlling How the Server Listens for Connections . . . . . . . . . . . . . . . 629 12.4 Managing MySQL User Accounts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630 12.4.1 High-Level MySQL Account Management . . . . . . . . . . . . . . . . . . . 631 12.4.2 Granting Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634 12.4.3 Displaying Account Privileges. . . . . . . . . . . . . . . . . . . . . . . . . . . . 643 12.4.4 Revoking Privileges and Removing Users . . . . . . . . . . . . . . . . . . . 643 12.4.5 Changing Passwords or Resetting Lost Passwords . . . . . . . . . . . . 644 12.5 Maintaining Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645 12.5.1 The Error Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648 12.5.2 The General Query Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649 12.5.3 The Slow-Query Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649 12.5.4 The Binary Log and the Binary Log Index File . . . . . . . . . . . . . . . . 650 12.5.5 The Relay Log and the Relay Log Index File . . . . . . . . . . . . . . . . . 652 12.5.6 Using Log Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 652 12.5.7 Log Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653 12.6 Tuning the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 660 12.6.1 Checking and Setting System Variable Values. . . . . . . . . . . . . . . . 661 12.6.2 General-Purpose System Variables. . . . . . . . . . . . . . . . . . . . . . . . 665 12.6.3 Checking Status Variable Values . . . . . . . . . . . . . . . . . . . . . . . . . 667. www.it-ebooks.info. xiii.

(15) xiv. Contents. 12.7 Storage Engine Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669 12.7.1 Selecting Which Storage Engines a Server Supports . . . . . . . . . . . 669 12.7.2 Configuring the MyISAM Storage Engine . . . . . . . . . . . . . . . . . . . . 671 12.7.3 Configuring the InnoDB Storage Engine . . . . . . . . . . . . . . . . . . . . 674 12.7.4 Configuring the Falcon Storage Engine . . . . . . . . . . . . . . . . . . . . . 680 12.8 Enabling or Disabling LOCAL Capability for LOAD DATA . . . . . . . . . . . 681 12.9 Internationalization and Localization Issues . . . . . . . . . . . . . . . . . . . . . 681 12.9.1 Configuring Time Zone Support . . . . . . . . . . . . . . . . . . . . . . . . . . 682 12.9.2 Selecting the Language for Error Messages . . . . . . . . . . . . . . . . . 684 12.9.3 Configuring Character Set Support . . . . . . . . . . . . . . . . . . . . . . . . 684 12.10 Running Multiple Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685 12.10.1 General Multiple Server Issues . . . . . . . . . . . . . . . . . . . . . . . . . 686 12.10.2 Configuring and Compiling Different Servers . . . . . . . . . . . . . . . . 688 12.10.3 Strategies for Specifying Startup Options . . . . . . . . . . . . . . . . . . 690 12.10.4 Using mysqld_multi for Server Management . . . . . . . . . . . . . 691 12.10.5 Running Multiple Servers on Windows . . . . . . . . . . . . . . . . . . . . 693 12.11 Updating MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695. 13 Access Control and Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699 13.1 Internal Security: Preventing Unauthorized Filesystem Access . . . . . . . . 700 13.1.1 How to Steal Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701 13.1.2 Securing Your MySQL Installation. . . . . . . . . . . . . . . . . . . . . . . . . 702 13.2 External Security: Preventing Unauthorized Network Access . . . . . . . . . 709 13.2.1 Structure and Contents of the MySQL Grant Tables. . . . . . . . . . . . 709 13.2.2 How the Server Controls Client Access . . . . . . . . . . . . . . . . . . . . 719 13.2.3 A Privilege Puzzle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724 13.2.4 Grant Table Risks to Avoid. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728 13.3 Setting Up Secure Connections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 731. 14 Database Maintenance, Backups, and Replication . . . . . . . . . . . . . . . . 737 14.1 Principles of Preventive Maintenance. . . . . . . . . . . . . . . . . . . . . . . . . . 737 14.2 Performing Database Maintenance with the Server Running . . . . . . . . . 739 14.2.1 Locking Individual Tables for Read-Only or Read/Write Access . . . . 740 14.2.2 Locking All Databases for Read-Only Access. . . . . . . . . . . . . . . . . 743 14.3 General Preventative Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . 743 14.3.1 Using the Server’s Auto-Recovery Capabilities. . . . . . . . . . . . . . . . 744 14.3.2 Scheduling Preventive Maintenance . . . . . . . . . . . . . . . . . . . . . . . 745 14.4 Making Database Backups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746. www.it-ebooks.info.

(16) Contents. 14.4.1 Making Text Backups with mysqldump . . . . . . . . . . . . . . . . . . . . 748 14.4.2 Making Binary Database Backups . . . . . . . . . . . . . . . . . . . . . . . . 751 14.4.3 Backing Up InnoDB or Falcon Tables . . . . . . . . . . . . . . . . . . . . . . 754 14.5 Copying Databases to Another Server . . . . . . . . . . . . . . . . . . . . . . . . . 755 14.5.1 Copying Databases Using a Backup File. . . . . . . . . . . . . . . . . . . . 755 14.5.2 Copying Databases from One Server to Another . . . . . . . . . . . . . . 756 14.6 Checking and Repairing Database Tables. . . . . . . . . . . . . . . . . . . . . . . 757 14.6.1 Using the Server to Check and Repair Tables . . . . . . . . . . . . . . . . 758 14.6.2 Using mysqlcheck to Check and Repair Tables . . . . . . . . . . . . . 759 14.6.3 Using myisamchk to Check and Repair Tables . . . . . . . . . . . . . . 760 14.7 Using Backups for Data Recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763 14.7.1 Recovering Entire Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764 14.7.2 Recovering Individual Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764 14.7.3 Re-Executing Statements in Binary Log Files . . . . . . . . . . . . . . . . 765 14.7.4 Coping with InnoDB Auto-Recovery Problems . . . . . . . . . . . . . . . . 767 14.8 Setting Up Replication Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768 14.8.1 How Replication Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769 14.8.2 Establishing a Master-Slave Replication Relationship . . . . . . . . . . 770 14.8.3 Binary Logging Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773 14.8.4 Using a Replication Slave for Making Backups . . . . . . . . . . . . . . . 774. IV: Appendixes A Obtaining and Installing Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777 A.1 Obtaining the sampdb Sample Database Distribution . . . . . . . . . . . . . . 777 A.2 Obtaining MySQL and Related Software . . . . . . . . . . . . . . . . . . . . . . . . . 778 A.3 Choosing a Version of MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 780 A.4 Installing MySQL on Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 780 A.4.1 Creating a Login Account for the MySQL User . . . . . . . . . . . . . . . . . 782 A.4.2 Obtaining and Installing a MySQL Distribution on Unix. . . . . . . . . . . 782 A.4.3 Post-Installation Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 786 A.4.4 Installing Perl DBI Support on Unix . . . . . . . . . . . . . . . . . . . . . . . . 789 A.4.5 Installing Apache and PHP on Unix. . . . . . . . . . . . . . . . . . . . . . . . . 790 A.5 Installing MySQL on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 792 A.5.1 Installing Perl DBI Support on Windows . . . . . . . . . . . . . . . . . . . . . 796 A.5.2 Installing Apache and PHP on Windows . . . . . . . . . . . . . . . . . . . . . 796. www.it-ebooks.info. xv.

(17) xvi. Contents. B Data Type Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797 B.1 Numeric Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799 B.1.1 Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799 B.1.2 Fixed-Point Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801 B.1.3 Floating-Point Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801 B.1.4 BIT Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803 B.2 String Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803 B.2.1 Binary String Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805 B.2.2 Non-Binary String Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807 B.2.3 ENUM and SET Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809 B.3 Date and Time Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809 B.4 Spatial Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 811. C Operator and Function Reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813 C.1 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814 C.1.1 Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814 C.1.2 Grouping Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 815 C.1.3 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816 C.1.4 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817 C.1.5 Bit Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823 C.1.6 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824 C.1.7 Cast Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825 C.1.8 Pattern-Matching Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826 C.2 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 830 C.2.1 Comparison Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 831 C.2.2 Cast Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833 C.2.3 Numeric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 834 C.2.4 String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840 C.2.5 Date and Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852 C.2.6 Summary Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 868 C.2.7 Security and Compression Functions . . . . . . . . . . . . . . . . . . . . . . . 871 C.2.8 Advisory Locking Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875 C.2.9 Spatial Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 877 C.2.10 XML Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883 C.2.11 Miscellaneous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 883. www.it-ebooks.info.

(18) Contents. D System, Status, and User Variable Reference . . . . . . . . . . . . . . . . . . . . 889 D.1 System Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 889 D.2 Session-Only System Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921 D.3 Status Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924 D.3.1 InnoDB Status Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930 D.3.2 Query Cache Status Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933 D.3.3 SSL Status Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 934 D.4 User-Defined Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 935. E SQL Syntax Reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937 E.1 SQL Statement Syntax (Non-Compound Statements) . . . . . . . . . . . . . . . 938 E.2 Compound Statement Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1028 E.2.1 Control Structure Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 1029 E.2.2 Declaration Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1031 E.2.3 Cursor Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033 E.3 Comment Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1033. F MySQL Program Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1037 F.1 Displaying a Program’s Help Message . . . . . . . . . . . . . . . . . . . . . . . . . 1038 F.2 Specifying Program Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1039 F.2.1 Standard MySQL Program Options . . . . . . . . . . . . . . . . . . . . . . . . 1041 F.2.2 Option Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1045 F.2.3 Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1049 F.3 myisamchk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1051 F.3.1 Standard Options Supported by myisamchk . . . . . . . . . . . . . . . . 1052 F.3.2 Options Specific to myisamchk . . . . . . . . . . . . . . . . . . . . . . . . . 1053 F.3.3 Variables for myisamchk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1056 F.4 myisampack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1058 F.4.1 Standard Options Supported by myisampack . . . . . . . . . . . . . . . 1058 F.4.2 Options Specific to myisampack . . . . . . . . . . . . . . . . . . . . . . . . 1058 F.5 mysql . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1059 F.5.1 Standard Options Supported by mysql . . . . . . . . . . . . . . . . . . . . 1060 F.5.2 Options Specific to mysql . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1061 F.5.3 Variables for mysql . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1065 F.5.4 mysql Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1066 F.5.5 mysql Prompt Definition Sequences . . . . . . . . . . . . . . . . . . . . . . 1068. www.it-ebooks.info. xvii.

(19) xviii. Contents. F.6 mysql.server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1070 F.6.1 Options Supported by mysql.server . . . . . . . . . . . . . . . . . . . . 1070 F.7 mysql_config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1071 F.7.1 Options Specific to mysql_config . . . . . . . . . . . . . . . . . . . . . . 1071 F.8 mysql_install_db . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1071 F.8.1 Standard Options Supported by mysql_install_db . . . . . . . . . 1072 F.8.2 Options Specific to mysql_install_db . . . . . . . . . . . . . . . . . . 1072 F.9 mysqladmin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1072 F.9.1 Standard Options Supported by mysqladmin . . . . . . . . . . . . . . . 1073 F.9.2 Options Specific to mysqladmin . . . . . . . . . . . . . . . . . . . . . . . . 1073 F.9.3 Variables for mysqladmin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1074 F.9.4 mysqladmin Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1074 F.10 mysqlbinlog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1076 F.10.1 Standard Options Supported by mysqlbinlog . . . . . . . . . . . . . 1076 F.10.2 Options Specific to mysqlbinlog . . . . . . . . . . . . . . . . . . . . . . 1077 F.10.3 Variables for mysqlbinlog . . . . . . . . . . . . . . . . . . . . . . . . . . . 1079 F.11 mysqlcheck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1079 F.11.1 Standard Options Supported by mysqlcheck . . . . . . . . . . . . . . 1079 F.11.2 Options Specific to mysqlcheck . . . . . . . . . . . . . . . . . . . . . . . 1080 F.12 mysqld . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1083 F.12.1 Standard Options Supported by mysqld . . . . . . . . . . . . . . . . . . 1083 F.12.2 Options Specific to mysqld . . . . . . . . . . . . . . . . . . . . . . . . . . . 1084 F.12.3 Variables for mysqld . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1100 F.13 mysqld_multi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1101 F.13.1 Standard Options Supported by mysqld_multi . . . . . . . . . . . . 1101 F.13.2 Options Specific to mysqld_multi . . . . . . . . . . . . . . . . . . . . . 1101 F.14 mysqld_safe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1102 F.14.1 Standard Options Supported by mysqld_safe . . . . . . . . . . . . . 1102 F.14.2 Options Specific to mysqld_safe . . . . . . . . . . . . . . . . . . . . . . 1102 F.15 mysqldump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1104 F.15.1 Standard Options Supported by mysqldump . . . . . . . . . . . . . . . 1105 F.15.2 Options Specific to mysqldump . . . . . . . . . . . . . . . . . . . . . . . . 1105 F.15.3 Data Format Options for mysqldump . . . . . . . . . . . . . . . . . . . . 1112 F.15.4 Variables for mysqldump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1112 F.16 mysqlhotcopy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1113 F.16.1 Standard Options Supported by mysqlhotcopy . . . . . . . . . . . . 1114 F.16.2 Options Specific to mysqlhotcopy . . . . . . . . . . . . . . . . . . . . . 1114. www.it-ebooks.info.

(20) Contents. F.17 mysqlimport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1116 F.17.1 Standard Options Supported by mysqlimport . . . . . . . . . . . . . 1117 F.17.2 Options Specific to mysqlimport . . . . . . . . . . . . . . . . . . . . . . 1117 F.17.3 Data Format Options for mysqlimport . . . . . . . . . . . . . . . . . . 1118 F.18 mysqlshow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1119 F.18.1 Standard Options Supported by mysqlshow . . . . . . . . . . . . . . . 1119 F.18.2 Options Specific to mysqlshow . . . . . . . . . . . . . . . . . . . . . . . . 1119 F.19 perror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1120 F.19.1 Standard Options Supported by perror . . . . . . . . . . . . . . . . . . 1120 Note: Appendixes G, H, and I are located online and are accessible either by registering this book at informit.com/register or by visiting www.kitebird.com/mysql-book.. G C API Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1121 G.1 Compiling and Linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1122 G.2 C API Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1123 G.2.1 Scalar Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1123 G.2.2 Non-Scalar Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1124 G.2.3 Accessor Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1135 G.3 C API Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1136 G.3.1 Client Library Initialization and Termination Routines. . . . . . . . . . . 1136 G.3.2 Connection Management Routines . . . . . . . . . . . . . . . . . . . . . . . 1137 G.3.3 Error-Reporting Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1149 G.3.4 Statement Construction and Execution Routines . . . . . . . . . . . . . 1150 G.3.5 Result Set Processing Routines. . . . . . . . . . . . . . . . . . . . . . . . . . 1152 G.3.6 Information Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1161 G.3.7 Transaction Control Routines. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1164 G.3.8 Multiple Result Set Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1164 G.3.9 Prepared Statement Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . 1165 G.3.10 Administrative Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1173 G.3.11 Threaded Client Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1175 G.3.12 Debugging Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1175. H Perl DBI API Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1177 H.1 Writing Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1178 H.2 DBI Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1178 H.2.1 DBI Class Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1180 H.2.2 Database-Handle Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1185. www.it-ebooks.info. xix.

(21) xx. Contents. H.2.3 Statement-Handle Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1191 H.2.4 General Handle Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1195 H.2.5 MySQL-Specific Administrative Methods . . . . . . . . . . . . . . . . . . . . 1196 H.3 DBI Utility Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1197 H.4 DBI Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1198 H.4.1 Database-Handle Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1198 H.4.2 General Handle Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1199 H.4.3 MySQL-Specific Database-Handle Attributes . . . . . . . . . . . . . . . . . 1200 H.4.4 Statement-Handle Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1201 H.4.5 MySQL-Specific Statement-Handle Attributes . . . . . . . . . . . . . . . . 1203 H.4.6 Dynamic Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1205 H.5 DBI Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1205. I PHP API Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1207 I.1 Writing PHP Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1207 I.2 PDO Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1208 I.3 PDO Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1208 I.3.1 PDO Class Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1209 I.3.2 PDOStatement Object Methods. . . . . . . . . . . . . . . . . . . . . . . . . 1215 I.3.3 PDOException Object Methods. . . . . . . . . . . . . . . . . . . . . . . . . 1222 I.3.4 PDO Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1223. Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1225. www.it-ebooks.info.

(22) About the Author Paul DuBois is a writer, database administrator, and leader in the open source and MySQL communities. He has contributed to the online documentation for MySQL and is the author of MySQL and Perl for the Web (New Riders), MySQL Cookbook, Using csh and tcsh, and Software Portability with imake (O’Reilly). He is currently a technical writer with the MySQL documentation team at Sun Microsystems.. Acknowledgments Acknowledgments are presented here by edition.. Fourth Edition My technical reviewers, Stephen Frein and Tim Boronczyk, identified many points that needed correction or clarification. Ulf Wendel and Johannes Schlüter made comments and corrections on the PHP material. My thanks to each of them. The staff at Pearson responsible for this edition were Mark Taber, Acquisitions Editor; Michael Thurston, Development Editor; Jovana San Nicolas-Shirley, Project Editor; Jake McFarland, Compositor; Cheryl Lenser, Indexer; and Gary Adair, Cover Designer. To my wife Karen, my continued thanks and gratitude for her encouragement and support throughout this effort.. Third Edition The third edition enjoyed careful technical review by Zak Greant and Chris Newman. Their efforts improved the manuscript at many points. Monty and the developers at MySQL AB also provided insight in response to my questions. The people at Pearson responsible for this edition were Shelley Johnston, Acquisitions Editor; Damon Jordan, Development Editor; and Andy Beaster, Project Editor. I am happy to recognize that my wife Karen again deserves special credit for her support during yet more revision and rewriting.. Second Edition For the second edition, the technical reviewers once again played a crucial role in finding errors and making corrections and clarifications. Hang Lau and Shane Kirk served as reviewers. I’d also like to thank Monty Widenius, Alexander Barkov, Jani Tolonen, and the other MySQL developers for patiently enduring my many questions and supplying answers that made their way into these pages. The New Riders staff that brought this edition to life were Stephanie Wall, Associate Publisher; Chris Zahn, Development Editor; Lori Lyons, Senior Project Editor; Pat Kinyon, Copy Editor; Cheryl Lenser, Indexer; and Stacey Richwine-DeRome, Compositor. And, as always, my wife Karen provided the behind-the-scenes support that readers do not see, but without which this book would be much poorer.. www.it-ebooks.info.

(23) First Edition This book benefited greatly from the comments, corrections, and criticisms provided by the technical reviewers: David Axmark,Vijay Chaugule, Chad Cunningham, Bill Gerrard, Jijo George John, Fred Read, Egon Schmid, and Jani Tolonen. Special thanks goes to Michael “Monty”Widenius, the principal MySQL developer, who not only reviewed the manuscript, but also fielded hundreds of questions that I sent his way during the course of writing the book. Naturally, any errors that remain are my own. I’d also like to thank Tomas Karlsson, Colin McKinnon, Sasha Pachev, Eric Savage, Derick H. Siddoway, and Bob Worthy, who reviewed the initial proposal and helped shape the book into its present form. The staff at New Riders are responsible first for conceiving this book and then for turning my scribblings into the finished work you hold in your hands. Laurie Petrycki acted as Executive Editor. Katie Purdum, Acquisitions Editor, helped me get under way and took the heat when I missed deadlines. Leah Williams did double duty not only as Development Editor but as Copy Editor; she put in many, many late hours, especially in the final stages of the project. Cheryl Lenser and Tim Wright produced the index. John Rahm served as Project Editor. Debra Neel proofread the manuscript. Gina Rexrode and Wil Cruz, Compositors, laid out the book in the form you see now. My thanks to each of them. Most of all, I want to express my appreciation to my wife, Karen, for putting up with another book, and for her understanding and patience as I disappeared, sometimes for days on end, into “the writing zone.” Her support made the task easier on many occasions, and I am pleased to acknowledge her contribution; she helped me write every page.. www.it-ebooks.info.

(24) We Want to Hear from You! As the reader of this book, you are our most important critic and commentator.We value your opinion and want to know what we’re doing right, what we could do better, what areas you’d like to see us publish in, and any other words of wisdom you’re willing to pass our way. You can email or write me directly to let me know what you did or didn’t like about this book—as well as what we can do to make our books stronger. Please note that I cannot help you with technical problems related to the topic of this book, and that due to the high volume of mail I receive, I might not be able to reply to every message. When you write, please be sure to include this book’s title and author as well as your name and phone or email address. I will carefully review your comments and share them with the author and editors who worked on the book. Email: Mail:. [email protected] Mark Taber Associate Publisher Pearson Education 800 East 96th Street Indianapolis, IN 46240 USA. Reader Services Visit our website and register this book at informit.com/register for convenient access to any updates, downloads, or errata that might be available for this book.. www.it-ebooks.info.

(25) This page intentionally left blank. www.it-ebooks.info.

(26) Introduction A relational database management system (RDBMS) is an essential tool in many environments, from traditional uses in business, research, and educational contexts, to applications such as powering search engines on the Internet. However, despite the importance of a good database system for managing and accessing information resources, many organizations have found them to be out of reach of their financial resources. Historically, database systems have been an expensive proposition, with vendors charging healthy fees both for software and for support.Also, because database engines often had substantial hardware requirements to run with any reasonable performance, the cost was even greater. The situation is different now, on both the hardware and software sides of the picture. Small desktop systems and servers are inexpensive but powerful, and there is a thriving movement devoted to writing high-performance operating systems for them.These operating systems are available free over the Internet or at the cost of an inexpensive CD.They include several BSD Unix derivatives (FreeBSD, NetBSD, OpenBSD) as well as various distributions of Linux (Fedora, Debian, Gentoo, SuSE, to name a few). Production of free operating systems has proceeded in concert with—and to a large extent has been made possible by—the development of freely available tools like gcc, the GNU C compiler.These efforts to make software available to anyone who wants it are part of the Open Source movement. Open Source projects have produced many important pieces of software. For example,Apache is the most widely used Web server on the Internet. Perl, Python, and Ruby are well-established general-purpose scripting languages, and PHP is a language that is popular due largely to the ease with which it enables dynamic Web pages to be written.These all stand in contrast to proprietary solutions that lock you into high-priced products from vendors that don’t even provide source code. Database software has become more accessible, too, and Open Source database systems are freely available. One of these is MySQL, a SQL client/server relational database management system originating from Scandinavia. MySQL includes an SQL server, client programs for accessing the server, administrative tools, and a programming interface for writing your own programs. MySQL’s roots begin in 1979, with the UNIREG database tool created by Michael “Monty”Widenius for the Swedish company TcX. In 1994,TcX began searching for an RDBMS with an SQL interface for use in developing Web applications.They tested some commercial servers, but found all too slow for TcX’s large tables.They also took a look at mSQL, but it lacked certain features that TcX required. Consequently, Monty began. www.it-ebooks.info.

(27) 2. Introduction. developing a new server.The programming interface was explicitly designed to be similar to the one used by mSQL because several free tools were available for mSQL, and by using a similar interface, those same tools could be used for MySQL with a minimum of porting effort. In 1995, David Axmark of Detron HB began to push for TcX to release MySQL on the Internet. David also worked on the documentation and on getting MySQL to build with the GNU configure utility. MySQL 3.11.1 was unleashed on the world in 1996 in the form of binary distributions for Linux and Solaris.Today, MySQL works on many more platforms and is available in both binary and source form.The company MySQL AB was formed to provide distributions of MySQL under both Open Source and commercial licenses, and to offer technical support, monitoring services, and training. In 2008, Sun Microsystems acquired MySQL AB and the commitment to Open Source remains strong (Sun was already moving in the direction of making many of its products available under Open Source licensing). Initially, MySQL became widely popular because of its speed and simplicity. But there was criticism, too, because it lacked features such as transactions and foreign key support. MySQL continued to develop, adding not only those features but others such as replication, subqueries, stored procedures, views, and triggers.These capabilities take MySQL into the realm of enterprise applications.As a result, people who once would have considered only “big iron” database systems for their applications now give serious consideration to MySQL. MySQL is portable and runs on commercial operating systems (such as Mac OS X, HP-UX, and Windows) and on hardware all the way up to enterprise servers. Furthermore, its performance rivals any database system you care to put up against it, and it can handle large databases with billions of rows. In the business world, MySQL’s presence continues to increase as companies discover it to be capable of handling their database needs at a fraction of what they are used to paying for commercial licensing and support. MySQL lies squarely within the picture that unfolds before us: freely available operating systems running on powerful but inexpensive hardware, putting substantial processing power and capabilities in the hands of more individuals and businesses than ever before, on a wider variety of systems than ever before.This lowering of the economic barriers to computing puts powerful database solutions within reach of more people and organizations than at any time in the past. Organizations that once could only dream of putting the power of a high-performance RDBMS to work for them now can do so for very little cost.This is true for individuals as well. For example, I use MySQL with Perl, PHP, and Apache on my Apple laptop running Mac OS X.This enables me to carry my work with me anywhere.Total cost: the cost of the laptop.. Why Choose MySQL? If you’re looking for a free or low-cost database management system, several are available from which to choose, such as MySQL, PostgreSQL, or SQLite.When you compare MySQL with other database systems, think about what’s most important to you.. www.it-ebooks.info.

(28) Why Choose MySQL?. Performance, support, features (such as SQL conformance or extensions), licensing conditions and restrictions, and price all are factors to take into account. Given these considerations, MySQL has many attractive features to offer: n. Speed. MySQL is fast. Its developers contend that MySQL is about the fastest database system you can get.You can investigate this claim by visiting http://www. mysql.com/why-mysql/benchmarks/, a performance-comparison page on the MySQL Web site.. n. Ease of use. MySQL is a high-performance but relatively simple database system and is much less complex to set up and administer than larger systems.. n. Query language support. MySQL understands SQL (Structured Query Language), the standard language of choice for all modern database systems.. n. Capability. The MySQL server is multi-threaded, so many clients can connect to it at the same time. Each client can use multiple databases simultaneously.You can access MySQL interactively using several interfaces that let you enter queries and view the results: command-line clients,Web browsers, or GUI clients. In addition, programming interfaces are available for many languages, such as C, Perl, Java, PHP, Python, and Ruby.You can also access MySQL using applications that support ODBC and .NET (protocols developed by Microsoft).This gives you the choice of using prepackaged client software or writing your own for custom applications.. n. Connectivity and security. MySQL is fully networked, and databases can be accessed from anywhere on the Internet, so you can share your data with anyone, anywhere. But MySQL has access control so that one person who shouldn’t see another’s data cannot.To provide additional security, MySQL supports encrypted connections using the Secure Sockets Layer (SSL) protocol.. n. Portability. MySQL runs on many varieties of Unix and Linux, as well as on other systems such as Windows and NetWare. MySQL runs on hardware from high-end servers down to small personal computers (even palmtop devices).. n. Small size. MySQL has a modest distribution size, especially compared to the huge disk space footprint of certain other database systems.. n. Availability and cost. MySQL is an Open Source project available under multiple licensing terms. First, it is available under the terms of the GNU General Public License (GPL).This means that MySQL is available without cost for most in-house uses. Second, for organizations that prefer or require formal arrangements or that do not want to be bound by the conditions of the GPL, commercial licenses are available.. n. Open distribution and source code. MySQL is easy to obtain; just use your Web browser. If you don’t understand how something works, are curious about an algorithm, or want to perform a security audit, you can get the source code and. www.it-ebooks.info. 3.

(29) 4. Introduction. examine it. If you think you’ve found a bug, please report it; the developers want to know. What about support? Good question; a database system isn’t much use if you can’t get help for it.This book is one form of assistance, and I like to think that it’s useful in that regard. (The fact that the book has reached its fourth edition suggests that it accomplishes that goal.) There are other resources open to you as well, and you’ll find that MySQL has good support: n. n. n. The MySQL Reference Manual is included in MySQL distributions, and also is available online and in printed form.The Reference Manual regularly receives good marks in the MySQL user community.This is important, because the value of a good product is diminished if no one can figure out how to use it. Technical support contracts, monitoring services, and training classes are available from Sun. There are several active MySQL mailing lists to which anyone may subscribe.These lists have many helpful participants, including several MySQL developers.As a support resource, many people find these lists invaluable.. The MySQL community, developers and nondevelopers alike, is very responsive. Answers to questions on the mailing lists often arrive within minutes.When bugs are reported, the developers generally fix them quickly, and fixes become available daily over the Internet. Contrast this with the often-frustrating experience of navigating the Byzantine support channels of big vendors.You’ve been there? Me, too. If you are in the database-selection process, MySQL is an ideal candidate for evaluation.You can try MySQL with no risk or financial commitment. If you get stuck, you can use the mailing lists to get help.An evaluation costs some of your time, but that’s true no matter what database system you’re considering—and it’s a safe bet that your installation and setup time for MySQL will be less than for many other systems.. Already Running Another RDBMS? If you’re currently running another database system but feel constrained by it, you definitely should consider MySQL. Perhaps performance of your current system is a concern, or it’s proprietary and you don’t like being locked into it. Perhaps you’d like to run on hardware that’s not supported by your current system, or your software is provided in binary-only format but you want to have the source available. Or maybe it just costs too much! All of these are reasons to look into MySQL. Use this book to familiarize yourself with MySQL’s capabilities, contact the MySQL sales crew, ask questions on the mailing lists, and you’ll find the answers you need to make a decision. One thing to keep in mind is that although all major database engines support SQL, each supports a somewhat different dialect. Check the chapters in this book that deal with MySQL’s SQL dialect and data types.You may decide that the version of SQL. www.it-ebooks.info.

(30) What You Can Expect from This Book. supported by your current RDBMS is too different and that porting your applications would involve significant effort. Part of your evaluation should be to try porting a few examples, of course.This will give you valuable experience in making an assessment.There is an ongoing commitment by the MySQL developers to an increasing conformance to standard SQL.That has the practical consequence of eliminating porting roadblocks as time goes on, so your porting effort may turn out to be easier than you expect.. Tools Provided with MySQL MySQL distributions include the following tools: n. An SQL server. This is the engine that powers MySQL and provides access to your databases.. n. Client and utility programs. These include an interactive client program that enables you to enter queries directly and view the results.Also available are several administrative and utility programs that help you run your site: One allows you to monitor and control the server; others let you import data, perform backups, check tables for problems, and more.. n. A client library for writing your own programs. You can write client programs in C because the library is in C, but the library also can be linked into other language processors such as Perl, PHP, or Ruby to provide the basis for MySQL interfaces in those languages.. In addition to the software provided with MySQL itself, MySQL is used by many talented and capable people who like writing software to enhance their productivity and who are willing to share that software.The result is that you have access to a variety of third-party tools that make MySQL easier to use or that extend its reach into areas such as Web site development.. What You Can Expect from This Book By reading this book, you’ll learn how to use MySQL effectively so that you can get your work done more productively.You’ll be able to figure out how to get your information into a database, and you’ll learn how to get it back out by formulating queries that give you the answers to the questions you want to ask of that data. You don’t need to be a programmer to understand or use SQL.This book will show you how it works. But there’s more to understanding how to use a database system properly than knowing SQL syntax.This book emphasizes MySQL’s unique capabilities and shows how to use them. You’ll also see how MySQL integrates with other tools.The book shows how to use MySQL with Perl and PHP to generate dynamic Web pages created from the result of. www.it-ebooks.info. 5.

(31) 6. Introduction. database queries.You’ll learn how to write your own programs that access MySQL databases.All of these enhance MySQL’s capabilities to handle the requirements of your particular applications. If you’ll be responsible for administering a MySQL installation, this book will tell you what your duties are and how to carry them out.You’ll learn how to create user accounts, perform database backups, set up replication, and make sure your site is secure.. Road Map to This Book This book is organized into four parts.The first concentrates on general concepts of database use.The second focuses on writing your own programs that use MySQL.The third is aimed at those readers who have administrative duties.The fourth provides a set of reference appendixes.. Part I: General MySQL Use n. n. n. n. n. Chapter 1,“Getting Started with MySQL.” Discusses how MySQL can be useful to you, provides a tutorial that introduces the interactive MySQL client program, covers the basics of SQL, and demonstrates MySQL’s general capabilities. Chapter 2,“Using SQL to Manage Data.” Every major RDBMS now available understands SQL, but every database engine implements a slightly different SQL dialect.This chapter discusses SQL with particular emphasis on those features that make MySQL distinctive. Chapter 3,“Data Types.” Discusses the data types that MySQL provides for storing your information, the properties and limitations of each type, when and how to use them, how to choose between similar types, expression evaluation, and type conversion. Chapter 4,“Stored Programs.” Discusses how to write and use SQL programs that are stored on the server side.Types of programs available to you are stored functions and procedures, triggers, and events. Chapter 5,“Query Optimization.” Discusses how to make your queries run more efficiently.. Part II: Using MySQL Programming Interfaces n. n. Chapter 6,“Introduction to MySQL Programming.” Discusses some of the application programming interfaces (APIs) available for MySQL and provides a general comparison of the APIs that the book covers in detail. Chapter 7,“Writing MySQL Programs Using C.” Discusses how to write C programs using the API provided by the MySQL C client library.. www.it-ebooks.info.

References

Related documents

In the 1990s the UNHCR Executive Committee recognized “that the involuntary displacement of persons within their own countries is a problem of global dimensions, and that the plight

Apart from foosball, the interchangeable table top allows you to play slide hockey, billiards, table tennis, shuffleboard, chess, cards, backgammon, bowling and checkers?. What is

[r]

To understand the fiscal implications of increased energy storage capacity in urban water systems, CU budgets are explored, electrical rate schedules are

Pannel thickness Materialstärke CAME DÉMONTABLE Removable cam Demontierbarer Riegel CAME OU EMBOUT Means of locking Verriegelungssystem OPTIONS Option Wahlmöglichkeit 1017 - 04 SH

Mercado 511 SCRA 305 (2006), the Court held that a special power of attorney is necessary for an agent to borrow money, unless it be urgent and indispensable

8.My reaction of this statement is almost all data collection methods have some biases associated with them,there fore high correlaltions among data obtained on the same variable

– Interestingly, federal respondents were more likely to believe a private cloud model would best meet their organization‘s needs (37.0% compared to 23.6% state and local