www.it-ebooks.info www.it-ebooks.info New to OSCON this year: OSCON Data The O’Reilly Open Source Convention Learn about open source technologies for gathering, storing, and analyzing data with practical techniques and tools you can immediately put to use at this rst-of-its-kind open source event. Register Now & Save 20% use discount code OS11DEBK July 25–27, 2011 Portland, OR oscon.com/data www.it-ebooks.info MySQL Cookbook ™ www.it-ebooks.info www.it-ebooks.info SECOND EDITION MySQL Cookbook ™ Paul DuBois Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo www.it-ebooks.info www.it-ebooks.info MySQL Cookbook™, Second Edition by Paul DuBois Copyright © 2007, 2002 O’Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safari.oreilly.com). For more information, contact our corporate/ institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editors: Brian Jepson and Andy Oram Copy Editor: Mary Anne Weeks Mayo Production Editor: Adam Witwer Proofreader: Sada Preisch Indexer: Joe Wizda Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrators: Robert Romano and Jessamyn Read Printing History: October 2002: First Edition. November 2006: Second Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. MySQL Cookbook, the image of a green anole, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. [C] www.it-ebooks.info www.it-ebooks.info Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv 1. Using the mysql Client Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.0 Introduction 1 1.1 Setting Up a MySQL User Account 2 1.2 Creating a Database and a Sample Table 4 1.3 Starting and Stopping mysql 6 1.4 Specifying Connection Parameters Using Option Files 8 1.5 Protecting Option Files from Other Users 11 1.6 Mixing Command-Line and Option File Parameters 11 1.7 What to Do if mysql Cannot Be Found 12 1.8 Issuing SQL Statements 14 1.9 Canceling a Partially Entered Statement 15 1.10 Repeating and Editing SQL Statements 16 1.11 Using Auto-Completion for Database and Table Names 17 1.12 Telling mysql to Read Statements from a File 18 1.13 Telling mysql to Read Statements from Other Programs 21 1.14 Entering an SQL One-Liner 22 1.15 Using Copy and Paste as a mysql Input Source 23 1.16 Preventing Query Output from Scrolling off the Screen 23 1.17 Sending Query Output to a File or to a Program 25 1.18 Selecting Tabular or Tab-Delimited Query Output Format 26 1.19 Specifying Arbitrary Output Column Delimiters 27 1.20 Producing HTML or XML Output 29 1.21 Suppressing Column Headings in Query Output 31 1.22 Making Long Output Lines More Readable 31 1.23 Controlling mysql’s Verbosity Level 33 1.24 Logging Interactive mysql Sessions 33 1.25 Creating mysql Scripts from Previously Executed Statements 34 1.26 Using User-Defined Variables in SQL Statements 35 1.27 Numbering Query Output Lines 38 1.28 Using mysql as a Calculator 39 v www.it-ebooks.info [...]... the first edition library files and make a copy of each one: % % % % cp cp cp cp Cookbook. php Cookbook1 .php Cookbook_ Utils.php Cookbook_ Utils1.php Cookbook_ Webutils.php Cookbook_ Webutils1.php Cookbook_ Session.php Cookbook_ Session1.php 2 Find all PHP scripts that include the first edition library files, and change them to include the *1.php files instead For example, a script that includes Cookbook_ Utils.php... the code from scratch The book also can be useful for people who aren’t even using MySQL You might suppose that because this is a MySQL cookbook and not a PostgreSQL cookbook or an InterBase cookbook that it won’t apply to database systems other than MySQL To some extent that’s true, because some of the SQL constructs are MySQL- specific But many of the queries use standard SQL that is portable to many... specific only to Windows use the C:\> prompt: C:\> "C:\Program Files \MySQL\ MySQL Server 5.0\bin \mysql" SQL statements that are issued from within the mysql client program are shown with a mysql> prompt and terminated with a semicolon: mysql> SELECT * FROM my_table; For examples that show a query result as you would see it when using mysql, I sometimes truncate the output, using an ellipsis ( ) to indicate... MySQL for Apache Logging 812 819 822 826 831 835 837 20 Using MySQL- Based Web Session Management 845 20.0 20.1 20.2 20.3 20.4 Introduction Using MySQL- Based Sessions in Perl Applications Using MySQL- Based Storage in Ruby Applications Using MySQL- Based Storage with the PHP Session Manager Using MySQL for Session-Backing Store with Tomcat 845 849 854 858 869 A Obtaining MySQL. .. Note for First Edition Readers If you have the first edition of MySQL Cookbook, be warned that the PHP library files in the second edition have the same names as in the first edition, but are incompatible with them If you are using any PHP scripts that depend on the first edition library files, they will break if you install the second edition library files on top of them To prevent this from happening,... receiving the statement results from it One such client is the mysql program that is included in MySQL distributions When used interactively, mysql prompts you for a statement, sends it to the MySQL server for execution, and then displays the results This capability makes mysql useful in its own right, but it’s also a valuable tool to help you with your MySQL programming activities It’s often convenient to... in this book took place under MySQL 5.0 and 5.1 Because new features are added to MySQL on a regular basis, some examples will not work under older versions For example, MySQL 5.0 introduces views, triggers, and stored procedures and functions, and the INFORMATION_SCHEMA metadata database MySQL 5.1 introduces events On occasion, I point out workarounds that you can use in MySQL 4.1 to compensate for... Perl DBI 1.52, DBD: :mysql 3.0, Ruby DBI 0.1.1, PEAR DB 1.7.6, MySQLdb 1.2.0, and MySQL Connector/J 3.1 Perl DBI requires Perl 5.6 or higher, although it’s slated to require Perl 5.8 or higher beginning with DBI 1.51 Ruby DBI requires Ruby 1.8.0 or higher Most PHP scripts shown here will run under either PHP 4.1 or PHP 5 (I recommend PHP 5) MySQLdb requires Python 2.3.4 or higher MySQL Connector/J requires... languages used here MySQL APIs Used in This Book MySQL programming interfaces exist for many languages, including C, C++, Eiffel, Java, Pascal, Perl, PHP, Python, Ruby, Smalltalk, and Tcl Given this fact, writing a MySQL cookbook presents an author with something of a challenge Clearly, the book Preface | xxi should provide recipes for doing many interesting and useful things with MySQL, but which API... xiii Preface The MySQL database management system has gained a large following in recent years This has been true especially in the Linux and open source communities, but MySQL has an increasing foothold in the commercial sector as well MySQL is well liked for several reasons: it’s fast, and it’s easy to set up, use, and administer MySQL runs under many varieties of Unix and Windows, and MySQL- based programs . 25–27, 2011 Portland, OR oscon.com/data www.it-ebooks.info MySQL Cookbook ™ www.it-ebooks.info www.it-ebooks.info SECOND EDITION MySQL Cookbook ™ Paul DuBois Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo www.it-ebooks.info www.it-ebooks.info MySQL. History: October 2002: First Edition. November 2006: Second Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. MySQL Cookbook, the. Using MySQL- Based Storage in Ruby Applications 854 20.3 Using MySQL- Based Storage with the PHP Session Manager 858 20.4 Using MySQL for Session-Backing Store with Tomcat 869 A. Obtaining MySQL
Ngày đăng: 05/05/2014, 15:21
Xem thêm: mysql cookbook 2nd edition, mysql cookbook 2nd edition, 4 Specifying Connection Parameters Using Option Files, 12 Telling mysql to Read Statements from a File, 26 Using User-Defined Variables in SQL Statements, 29 Using mysql in Shell Scripts, 1 Connecting, Selecting a Database, and Disconnecting, 2 Checking for Errors, 3 Writing Library Files, 4 Issuing Statements and Retrieving Results, 5 Handling Special Characters and NULL Values in Statements, 7 Identifying NULL Values in Result Sets, 8 Techniques for Obtaining Connection Parameters, 3 Giving Better Names to Query Result Columns, 16 Choosing Appropriate LIMIT Values, 2 Saving a Query Result in a Table, 5 Generating Unique Table Names, 2 Choosing a String Data Type, 4 Writing String Literals, 9 Controlling Case Sensitivity in String Comparisons, 10 Pattern Matching with SQL Patterns, 11 Pattern Matching with Regular Expressions, 13 Breaking Apart or Combining Strings, 15 Using FULLTEXT Searches, 2 Changing MySQL’s Date Format, 5 Using TIMESTAMP to Track Row Modification Times, 6 Extracting Parts of Dates or Times, 8 Converting Between Temporal Data Types and Basic Units, 9 Calculating the Interval Between Two Dates or Times, 10 Adding Date or Time Values, 16 Finding Dates for Any Weekday of a Given Week, 17 Performing Leap Year Calculations, 21 Selecting Rows Based on Their Temporal Characteristics, 1 Using ORDER BY to Sort Query Results, 3 Displaying One Set of Values While Sorting by Another, 4 Controlling Case Sensitivity of String Sorts, 11 Sorting by Variable-Length Substrings, 14 Floating Values to the Head or Tail of the Sort Order, 16 Sorting ENUM Values, 1 Summarizing with COUNT( ), 7 Dividing a Summary into Subgroups, 8 Summaries and NULL Values, 12 Categorizing Noncategorical Data, 14 Finding Smallest or Largest Summary Values, 16 Working with Per-Group and Overall Summary Values Simultaneously, 17 Generating a Report That Includes a Summary and a List, 2 Obtaining Result Set Metadata, 4 Using Metadata to Format Query Output, 6 Accessing Table Column Definitions, 8 Using Table Structure Information in Applications, Chapter 10. Importing and Exporting Data, 3 Specifying the Structure of the Datafile, 13 Exporting Query Results from MySQL, 17 Writing Your Own Export Programs, 19 Extracting and Rearranging Datafile Columns, 21 Validating and Transforming Data, 25 Using Patterns to Match Dates or Times, 27 Using Table Metadata to Validate Data, 28 Using a Lookup Table to Validate Data, 30 Performing Validity Checking on Date or Time Subparts, 31 Writing Date-Processing Utilities, 36 Guessing Table Structure from a Datafile, 38 Exchanging Data Between MySQL and Microsoft Excel, 39 Exporting Query Results as XML, 40 Importing XML into MySQL, 1 Creating a Sequence Column and Generating Sequence Values, 3 The Effect of Row Deletions on Sequence Generation, 4 Retrieving Sequence Values, 11 Using an AUTO_INCREMENT Column to Create Multiple Sequences, 13 Using AUTO_INCREMENT Values to Relate Tables, 14 Using Sequence Generators as Counters, 1 Finding Rows in One Table That Match Rows in Another, 2 Finding Rows with No Match in Another Table, 3 Comparing a Table to Itself, 4 Producing Master-Detail Lists and Summaries, 5 Enumerating a Many-to-Many Relationship, 6 Finding Rows Containing Per-Group Minimum or Maximum Values, 7 Computing Team Standings, 8 Using a Join to Fill or Identify Holes in a List, 10 Finding Cumulative Sums and Running Averages, 12 Combining Several Result Sets in a Single Query, 13 Identifying and Removing Mismatched or Unattached Rows, 15 Using Different MySQL Servers Simultaneously, 16 Referring to Join Output Column Names in Programs, 1 Calculating Descriptive Statistics, 3 Generating Frequency Distributions, 7 Randomizing a Set of Rows, 8 Selecting Random Items from a Set of Rows, 2 Dealing with Duplicates When Loading Rows into a Table, 3 Counting and Identifying Duplicates, 4 Eliminating Duplicates from a Table, 5 Eliminating Duplicates from a Self-Join Result, 3 Performing Transactions from Within Programs, 9 Using Alternatives to Transactions, 4 Using a Trigger to Define Dynamic Default Column Values, 6 Using a Trigger to Log Changes to a Table, 7 Using Events to Schedule Database Actions, 1 Basic Principles of Web Page Generation, 2 Using Apache to Run Web Scripts, 3 Using Tomcat to Run Web Scripts, 4 Encoding Special Characters in Web Output, 2 Displaying Query Results as Lists, 3 Displaying Query Results as Tables, 4 Displaying Query Results as Hyperlinks, 5 Creating a Navigation Index from Database Content, 6 Storing Images or Other Binary Data, 7 Retrieving Images or Other Binary Data, 9 Serving Query Results for Download, 10 Using a Template System to Generate Web Pages, Chapter 19. Processing Web Input with MySQL, 1 Writing Scripts That Generate Web Forms, 2 Creating Single-Pick Form Elements from Database Content, 3 Creating Multiple-Pick Form Elements from Database Content, 4 Loading a Database Record into a Form, 5 Collecting Web Input, 7 Storing Web Input in a Database, 8 Processing File Uploads, 9 Performing Searches and Presenting the Results, 10 Generating Previous-Page and Next-Page Links, 11 Generating “Click to Sort” Table Headings, 12 Web Page Access Counting, 14 Using MySQL for Apache Logging, Chapter 20. Using MySQL-Based Web Session Management, 1 Using MySQL-Based Sessions in Perl Applications, 2 Using MySQL-Based Storage in Ruby Applications, 3 Using MySQL-Based Storage with the PHP Session Manager, 4 Using MySQL for Session-Backing Store with Tomcat, Appendix B. Executing Programs from the Command Line, Appendix C. JSP and Tomcat Primer