1. Trang chủ
  2. » Công Nghệ Thông Tin

mysql cookbook 2nd edition

990 1.9K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Cấu trúc

  • MySQL Cookbook

  • MySQL Cookbook

  • Table of Contents

  • Preface

    • Who This Book Is For

    • What’s in This Book

    • MySQL APIs Used in This Book

    • Conventions Used in This Book

    • The MySQL Cookbook Companion Web Site

    • Version and Platform Notes

    • Upgrade Note for First Edition Readers

    • Additional Resources

    • Using Code Examples

    • Safari® Enabled

    • How to Contact Us

    • Acknowledgments

      • Second Edition

      • First Edition

  • Chapter 1. Using the mysql Client Program

    • 1.0  Introduction

    • 1.1  Setting Up a MySQL User Account

      • Problem

      • Solution

      • Discussion

    • 1.2  Creating a Database and a Sample Table

      • Problem

      • Solution

      • Discussion

    • 1.3  Starting and Stopping mysql

      • Problem

      • Solution

      • Discussion

    • 1.4  Specifying Connection Parameters Using Option Files

      • Problem

      • Solution

      • Discussion

    • 1.5  Protecting Option Files from Other Users

      • Problem

      • Solution

      • Discussion

    • 1.6  Mixing Command-Line and Option File Parameters

      • Problem

      • Solution

      • Discussion

    • 1.7  What to Do if mysql Cannot Be Found

      • Problem

      • Solution

      • Discussion

    • 1.8  Issuing SQL Statements

      • Problem

      • Solution

      • Discussion

      • See Also

    • 1.9  Canceling a Partially Entered Statement

      • Problem

      • Solution

      • Discussion

    • 1.10  Repeating and Editing SQL Statements

      • Problem

      • Solution

      • Discussion

    • 1.11  Using Auto-Completion for Database and Table Names

      • Problem

      • Solution

      • Discussion

    • 1.12  Telling mysql to Read Statements from a File

      • Problem

      • Solution

      • Discussion

    • 1.13  Telling mysql to Read Statements from Other Programs

      • Problem

      • Solution

      • Discussion

      • See Also

    • 1.14  Entering an SQL One-Liner

      • Problem

      • Solution

      • Discussion

    • 1.15  Using Copy and Paste as a mysql Input Source

      • Problem

      • Solution

      • Discussion

    • 1.16  Preventing Query Output from Scrolling off the Screen

      • Problem

      • Solution

      • Discussion

    • 1.17  Sending Query Output to a File or to a Program

      • Problem

      • Solution

      • Discussion

    • 1.18  Selecting Tabular or Tab-Delimited Query Output Format

      • Problem

      • Solution

      • Discussion

    • 1.19  Specifying Arbitrary Output Column Delimiters

      • Problem

      • Solution

      • Discussion

      • See Also

    • 1.20  Producing HTML or XML Output

      • Problem

      • Solution

      • Discussion

      • See Also

    • 1.21  Suppressing Column Headings in Query Output

      • Problem

      • Solution

      • Discussion

    • 1.22  Making Long Output Lines More Readable

      • Problem

      • Solution

      • Discussion

    • 1.23  Controlling mysql’s Verbosity Level

      • Problem

      • Solution

      • Discussion

    • 1.24  Logging Interactive mysql Sessions

      • Problem

      • Solution

      • Discussion

    • 1.25  Creating mysql Scripts from Previously Executed Statements

      • Problem

      • Solution

      • Discussion

    • 1.26  Using User-Defined Variables in SQL Statements

      • Problem

      • Solution

      • Discussion

    • 1.27  Numbering Query Output Lines

      • Problem

      • Solution

      • Discussion

    • 1.28  Using mysql as a Calculator

      • Problem

      • Solution

      • Discussion

    • 1.29  Using mysql in Shell Scripts

      • Problem

      • Solution

      • Discussion

        • Writing shell scripts under Unix

        • Writing shell scripts under Windows

  • Chapter 2. Writing MySQL-Based Programs

    • 2.0  Introduction

      • Assumptions

      • MySQL Client API Architecture

    • 2.1  Connecting, Selecting a Database, and Disconnecting

      • Problem

      • Solution

      • Discussion

        • Perl

        • Ruby

        • PHP

        • Python

        • Java

    • 2.2  Checking for Errors

      • Problem

      • Solution

      • Discussion

        • Perl

        • Ruby

        • PHP

        • Python

        • Java

    • 2.3  Writing Library Files

      • Problem

      • Solution

      • Discussion

        • Choosing a library file installation location

        • Setting library file access privileges

        • Perl

        • Ruby

        • PHP

        • Python

        • Java

    • 2.4  Issuing Statements and Retrieving Results

      • Problem

      • Solution

      • Discussion

        • SQL statement categories

        • Perl

        • Ruby

        • PHP

        • Python

        • Java

    • 2.5  Handling Special Characters and NULL Values in Statements

      • Problem

      • Solution

      • Discussion

        • Using placeholders

        • Using a quoting function

        • Perl

        • Ruby

        • PHP

        • Python

        • Java

    • 2.6  Handling Special Characters in Identifiers

      • Problem

      • Solution

      • Discussion

    • 2.7  Identifying NULL Values in Result Sets

      • Problem

      • Solution

      • Discussion

        • Perl

        • Ruby

        • PHP

        • Python

        • Java

    • 2.8  Techniques for Obtaining Connection Parameters

      • Problem

      • Solution

      • Discussion

        • Getting parameters from the command line

        • Getting parameters from option files

    • 2.9  Conclusion and Words of Advice

  • Chapter 3. Selecting Data from Tables

    • 3.0  Introduction

    • 3.1  Specifying Which Columns to Select

      • Problem

      • Solution

      • Discussion

    • 3.2  Specifying Which Rows to Select

      • Problem

      • Solution

      • Discussion

    • 3.3  Giving Better Names to Query Result Columns

      • Problem

      • Solution

      • Discussion

    • 3.4  Using Column Aliases to Make Programs Easier to Write

      • Problem

      • Solution

      • Discussion

      • See Also

    • 3.5  Combining Columns to Construct Composite Values

      • Problem

      • Solution

      • Discussion

    • 3.6  WHERE Clauses and Column Aliases

      • Problem

      • Solution

      • Discussion

    • 3.7  Debugging Comparison Expressions

      • Problem

      • Solution

      • Discussion

    • 3.8  Removing Duplicate Rows

      • Problem

      • Solution

      • Discussion

      • See Also

    • 3.9  Working with NULL Values

      • Problem

      • Solution

      • Discussion

      • See Also

    • 3.10  Writing Comparisons Involving NULL in Programs

      • Problem

      • Solution

      • Discussion

    • 3.11  Sorting a Result Set

      • Problem

      • Solution

      • Discussion

    • 3.12  Using Views to Simplify Table Access

      • Problem

      • Solution

      • Discussion

    • 3.13  Selecting Data from More Than One Table

      • Problem

      • Solution

      • Discussion

    • 3.14  Selecting Rows from the Beginning or End of a Result Set

      • Problem

      • Solution

      • Discussion

      • See Also

    • 3.15  Selecting Rows from the Middle of a Result Set

      • Problem

      • Solution

      • Discussion

    • 3.16  Choosing Appropriate LIMIT Values

      • Problem

      • Solution

      • Discussion

    • 3.17  What to Do When LIMIT Requires the “Wrong” Sort Order

      • Problem

      • Solution

      • Discussion

    • 3.18  Calculating LIMIT Values from Expressions

      • Problem

      • Solution

      • Discussion

  • Chapter 4. Table Management

    • 4.0  Introduction

    • 4.1  Cloning a Table

      • Problem

      • Solution

      • Discussion

    • 4.2  Saving a Query Result in a Table

      • Problem

      • Solution

      • Discussion

    • 4.3  Creating Temporary Tables

      • Problem

      • Solution

      • Discussion

    • 4.4  Checking or Changing a Table’s Storage Engine

      • Problem

      • Solution

      • Discussion

      • See Also

    • 4.5  Generating Unique Table Names

      • Problem

      • Solution

      • Discussion

  • Chapter 5. Working with Strings

    • 5.0  Introduction

    • 5.1  String Properties

    • 5.2  Choosing a String Data Type

      • Problem

      • Solution

      • Discussion

    • 5.3  Setting the Client Connection Character Set Properly

      • Problem

      • Solution

      • Discussion

    • 5.4  Writing String Literals

      • Problem

      • Solution

      • Discussion

      • See Also

    • 5.5  Checking a String’s Character Set or Collation

      • Problem

      • Solution

      • Discussion

    • 5.6  Changing a String’s Character Set or Collation

      • Problem

      • Solution

      • Discussion

    • 5.7  Converting the Lettercase of a String

      • Problem

      • Solution

      • Discussion

    • 5.8  Converting the Lettercase of a “Stubborn” String

      • Problem

      • Solution

      • Discussion

    • 5.9  Controlling Case Sensitivity in String Comparisons

      • Problem

      • Solution

      • Discussion

    • 5.10  Pattern Matching with SQL Patterns

      • Problem

      • Solution

      • Discussion

    • 5.11  Pattern Matching with Regular Expressions

      • Problem

      • Solution

      • Discussion

    • 5.12  Controlling Case Sensitivity in Pattern Matching

      • Problem

      • Solution

      • Discussion

    • 5.13  Breaking Apart or Combining Strings

      • Problem

      • Solution

      • Discussion

    • 5.14  Searching for Substrings

      • Problem

      • Solution

      • Discussion

    • 5.15  Using FULLTEXT Searches

      • Problem

      • Solution

      • Discussion

      • See Also

    • 5.16  Using a FULLTEXT Search with Short Words

      • Problem

      • Solution

      • Discussion

    • 5.17  Requiring or Excluding FULLTEXT Search Words

      • Problem

      • Solution

      • Discussion

    • 5.18  Performing Phrase Searches with a FULLTEXT Index

      • Problem

      • Solution

      • Discussion

  • Chapter 6. Working with Dates and Times

    • 6.0  Introduction

    • 6.1  Choosing a Temporal Data Type

      • Problem

      • Solution

      • Discussion

    • 6.2  Changing MySQL’s Date Format

      • Problem

      • Solution

      • Discussion

    • 6.3  Setting the Client Time Zone

      • Problem

      • Solution

      • Discussion

      • See Also

    • 6.4  Determining the Current Date or Time

      • Problem

      • Solution

      • Discussion

    • 6.5  Using TIMESTAMP to Track Row Modification Times

      • Problem

      • Solution

      • Discussion

      • See Also

    • 6.6  Extracting Parts of Dates or Times

      • Problem

      • Solution

      • Discussion

        • Decomposing dates or times using component-extraction functions

        • Decomposing dates or times using formatting functions

        • Decomposing dates or times using string functions

    • 6.7  Synthesizing Dates or Times from Component Values

      • Problem

      • Solution

      • Discussion

    • 6.8  Converting Between Temporal Data Types and Basic Units

      • Problem

      • Solution

      • Discussion

        • Converting between times and seconds

        • Converting between dates and days

        • Converting between date-and-time values and seconds

    • 6.9  Calculating the Interval Between Two Dates or Times

      • Problem

      • Solution

      • Discussion

        • Calculating intervals with temporal-difference functions

        • Calculating intervals using basic units

        • Time interval calculation using basic units

        • Date or date-and-time interval calculation using basic units

    • 6.10  Adding Date or Time Values

      • Problem

      • Solution

      • Discussion

        • Adding temporal values using temporal-addition functions or operators

        • Adding temporal values using basic units

    • 6.11  Calculating Ages

      • Problem

      • Solution

      • Discussion

    • 6.12  Shifting a Date-and-Time Value to a Different Time Zone

      • Problem

      • Solution

      • Discussion

    • 6.13  Finding the First Day, Last Day, or Length of a Month

      • Problem

      • Solution

      • Discussion

      • See Also

    • 6.14  Calculating Dates by Substring Replacement

      • Problem

      • Solution

      • Discussion

    • 6.15  Finding the Day of the Week for a Date

      • Problem

      • Solution

      • Discussion

    • 6.16  Finding Dates for Any Weekday of a Given Week

      • Problem

      • Solution

      • Discussion

    • 6.17  Performing Leap Year Calculations

      • Problem

      • Solution

      • Discussion

        • Determining whether a date occurs in a leap year

        • Using leap year tests for year-length calculations

        • Using leap year tests for month-length calculations

    • 6.18  Canonizing Not-Quite-ISO Date Strings

      • Problem

      • Solution

      • Discussion

      • See Also

    • 6.19  Treating Dates or Times as Numbers

      • Problem

      • Solution

      • Discussion

    • 6.20  Forcing MySQL to Treat Strings as Temporal Values

      • Problem

      • Solution

      • Discussion

    • 6.21  Selecting Rows Based on Their Temporal Characteristics

      • Problem

      • Solution

      • Discussion

        • Comparing dates to one another

        • Comparing times to one another

        • Comparing dates to calendar days

  • Chapter 7. Sorting Query Results

    • 7.0  Introduction

    • 7.1  Using ORDER BY to Sort Query Results

      • Problem

      • Solution

      • Discussion

    • 7.2  Using Expressions for Sorting

      • Problem

      • Solution

      • Discussion

    • 7.3  Displaying One Set of Values While Sorting by Another

      • Problem

      • Solution

      • Discussion

    • 7.4  Controlling Case Sensitivity of String Sorts

      • Problem

      • Solution

      • Discussion

    • 7.5  Date-Based Sorting

      • Problem

      • Solution

      • Discussion

    • 7.6  Sorting by Calendar Day

      • Problem

      • Solution

      • Discussion

    • 7.7  Sorting by Day of Week

      • Problem

      • Solution

      • Discussion

    • 7.8  Sorting by Time of Day

      • Problem

      • Solution

      • Discussion

    • 7.9  Sorting Using Substrings of Column Values

      • Problem

      • Solution

      • Discussion

    • 7.10  Sorting by Fixed-Length Substrings

      • Problem

      • Solution

      • Discussion

    • 7.11  Sorting by Variable-Length Substrings

      • Problem

      • Solution

      • Discussion

    • 7.12  Sorting Hostnames in Domain Order

      • Problem

      • Solution

      • Discussion

    • 7.13  Sorting Dotted-Quad IP Values in Numeric Order

      • Problem

      • Solution

      • Discussion

    • 7.14  Floating Values to the Head or Tail of the Sort Order

      • Problem

      • Solution

      • Discussion

      • See Also

    • 7.15  Sorting in User-Defined Orders

      • Problem

      • Solution

      • Discussion

    • 7.16  Sorting ENUM Values

      • Problem

      • Solution

      • Discussion

  • Chapter 8. Generating Summaries

    • 8.0  Introduction

    • 8.1  Summarizing with COUNT⁠(⁠ ⁠ ⁠)

      • Problem

      • Solution

      • Discussion

      • See Also

    • 8.2  Summarizing with MIN⁠(⁠ ⁠ ⁠) and MAX⁠(⁠ ⁠ ⁠)

      • Problem

      • Solution

      • Discussion

    • 8.3  Summarizing with SUM⁠(⁠ ⁠ ⁠) and AVG⁠(⁠ ⁠ ⁠)

      • Problem

      • Solution

      • Discussion

      • See Also

    • 8.4  Using DISTINCT to Eliminate Duplicates

      • Problem

      • Solution

      • Discussion

    • 8.5  Finding Values Associated with Minimum and Maximum Values

      • Problem

      • Solution

      • Discussion

      • See Also

    • 8.6  Controlling String Case Sensitivity for MIN⁠(⁠ ⁠ ⁠) and MAX⁠(⁠ ⁠ ⁠)

      • Problem

      • Solution

      • Discussion

    • 8.7  Dividing a Summary into Subgroups

      • Problem

      • Solution

      • Discussion

    • 8.8  Summaries and NULL Values

      • Problem

      • Solution

      • Discussion

    • 8.9  Selecting Only Groups with Certain Characteristics

      • Problem

      • Solution

      • Discussion

    • 8.10  Using Counts to Determine Whether Values Are Unique

      • Problem

      • Solution

      • Discussion

    • 8.11  Grouping by Expression Results

      • Problem

      • Solution

      • Discussion

    • 8.12  Categorizing Noncategorical Data

      • Problem

      • Solution

      • Discussion

    • 8.13  Controlling Summary Display Order

      • Problem

      • Solution

      • Discussion

    • 8.14  Finding Smallest or Largest Summary Values

      • Problem

      • Solution

      • Discussion

    • 8.15  Date-Based Summaries

      • Problem

      • Solution

      • Discussion

    • 8.16  Working with Per-Group and Overall Summary Values Simultaneously

      • Problem

      • Solution

      • Discussion

    • 8.17  Generating a Report That Includes a Summary and a List

      • Problem

      • Solution

      • Discussion

  • Chapter 9. Obtaining and Using Metadata

    • 9.0  Introduction

    • 9.1  Obtaining the Number of Rows Affected by a Statement

      • Problem

      • Solution

      • Discussion

        • Perl

        • Ruby

        • PHP

        • Python

        • Java

    • 9.2  Obtaining Result Set Metadata

      • Problem

      • Solution

      • Discussion

        • Perl

        • Ruby

        • PHP

        • Python

        • Java

    • 9.3  Determining Whether a Statement Produced a Result Set

      • Problem

      • Solution

      • Discussion

    • 9.4  Using Metadata to Format Query Output

      • Problem

      • Solution

      • Discussion

      • See Also

    • 9.5  Listing or Checking Existence of Databases or Tables

      • Problem

      • Solution

      • Discussion

    • 9.6  Accessing Table Column Definitions

      • Problem

      • Solution

      • Discussion

        • Using INFORMATION_SCHEMA to get table structure

        • Using SHOW COLUMNS to get table structure

        • Using CREATE TABLE to get table structure

    • 9.7  Getting ENUM and SET Column Information

      • Problem

      • Solution

      • Discussion

    • 9.8  Using Table Structure Information in Applications

      • Problem

      • Solution

      • Discussion

        • Displaying column lists

        • Interactive record editing

        • Mapping column definitions onto web page elements

        • Adding elements to ENUM or SET column definitions

        • Selecting all except certain columns

    • 9.9  Getting Server Metadata

      • Problem

      • Solution

      • Discussion

    • 9.10  Writing Applications That Adapt to the MySQL Server Version

      • Problem

      • Solution

      • Discussion

    • 9.11  Determining the Default Database

      • Problem

      • Solution

      • Discussion

    • 9.12  Monitoring the MySQL Server

      • Problem

      • Solution

      • Discussion

    • 9.13  Determining Which Storage Engines the Server Supports

      • Problem

      • Solution

      • Discussion

  • Chapter 10. Importing and Exporting Data

    • 10.0  Introduction

      • General Import and Export Issues

      • File Formats

      • Notes on Invoking Shell Commands

    • 10.1  Importing Data with LOAD DATA and mysqlimport

      • Problem

      • Solution

      • Discussion

    • 10.2  Specifying the Datafile Location

      • Problem

      • Solution

      • Discussion

    • 10.3  Specifying the Structure of the Datafile

      • Problem

      • Solution

      • Discussion

    • 10.4  Dealing with Quotes and Special Characters

      • Problem

      • Solution

      • Discussion

    • 10.5  Importing CSV Files

      • Problem

      • Solution

      • Discussion

    • 10.6  Reading Files from Different Operating Systems

      • Problem

      • Solution

      • Discussion

    • 10.7  Handling Duplicate Key Values

      • Problem

      • Solution

      • Discussion

    • 10.8  Obtaining Diagnostics About Bad Input Data

      • Problem

      • Solution

      • Discussion

    • 10.9  Skipping Datafile Lines

      • Problem

      • Solution

      • Discussion

    • 10.10  Specifying Input Column Order

      • Problem

      • Solution

      • Discussion

    • 10.11  Preprocessing Input Values Before Inserting Them

      • Problem

      • Solution

      • Discussion

    • 10.12  Ignoring Datafile Columns

      • Problem

      • Solution

      • Discussion

      • See Also

    • 10.13  Exporting Query Results from MySQL

      • Problem

      • Solution

      • Discussion

        • Exporting with the SELECT ... INTO OUTFILE statement

        • Using the mysql client to export data

      • See Also

    • 10.14  Exporting Tables as Text Files

      • Problem

      • Solution

      • Discussion

    • 10.15  Exporting Table Contents or Definitions in SQL Format

      • Problem

      • Solution

      • Discussion

    • 10.16  Copying Tables or Databases to Another Server

      • Problem

      • Solution

      • Discussion

    • 10.17  Writing Your Own Export Programs

      • Problem

      • Solution

      • Discussion

    • 10.18  Converting Datafiles from One Format to Another

      • Problem

      • Solution

      • Discussion

    • 10.19  Extracting and Rearranging Datafile Columns

      • Problem

      • Solution

      • Discussion

    • 10.20  Using the SQL Mode to Control Bad Input Data Handling

      • Problem

      • Solution

      • Discussion

    • 10.21  Validating and Transforming Data

      • Problem

      • Solution

      • Discussion

        • Writing an input-processing loop

        • Putting common tests in libraries

    • 10.22  Using Pattern Matching to Validate Data

      • Problem

      • Solution

      • Discussion

    • 10.23  Using Patterns to Match Broad Content Types

      • Problem

      • Solution

      • Discussion

    • 10.24  Using Patterns to Match Numeric Values

      • Problem

      • Solution

      • Discussion

    • 10.25  Using Patterns to Match Dates or Times

      • Problem

      • Solution

      • Discussion

      • See Also

    • 10.26  Using Patterns to Match Email Addresses or URLs

      • Problem

      • Solution

      • Discussion

    • 10.27  Using Table Metadata to Validate Data

      • Problem

      • Solution

      • Discussion

    • 10.28  Using a Lookup Table to Validate Data

      • Problem

      • Solution

      • Discussion

        • Issue individual statements

        • Construct a hash from the entire lookup table

        • Use a hash as a cache of already-seen lookup values

    • 10.29  Converting Two-Digit Year Values to Four-Digit Form

      • Problem

      • Solution

      • Discussion

    • 10.30  Performing Validity Checking on Date or Time Subparts

      • Problem

      • Solution

      • Discussion

    • 10.31  Writing Date-Processing Utilities

      • Problem

      • Solution

      • Discussion

    • 10.32  Using Dates with Missing Components

      • Problem

      • Solution

      • Discussion

    • 10.33  Importing Non-ISO Date Values

      • Problem

      • Solution

      • Discussion

    • 10.34  Exporting Dates Using Non-ISO Formats

      • Problem

      • Solution

      • Discussion

    • 10.35  Importing and Exporting NULL Values

      • Problem

      • Solution

      • Discussion

    • 10.36  Guessing Table Structure from a Datafile

      • Problem

      • Solution

      • Discussion

    • 10.37  Exchanging Data Between MySQL and Microsoft Access

      • Problem

      • Solution

      • Discussion

    • 10.38  Exchanging Data Between MySQL and Microsoft Excel

      • Problem

      • Solution

      • Discussion

    • 10.39  Exporting Query Results as XML

      • Problem

      • Solution

      • Discussion

    • 10.40  Importing XML into MySQL

      • Problem

      • Solution

      • Discussion

    • 10.41  Epilogue

  • Chapter 11. Generating and Using Sequences

    • 11.0  Introduction

    • 11.1  Creating a Sequence Column and Generating Sequence Values

      • Problem

      • Solution

      • Discussion

    • 11.2  Choosing the Data Type for a Sequence Column

      • Problem

      • Solution

      • Discussion

    • 11.3  The Effect of Row Deletions on Sequence Generation

      • Problem

      • Solution

      • Discussion

    • 11.4  Retrieving Sequence Values

      • Problem

      • Solution

      • Discussion

        • Using LAST_INSERT_ID⁠(⁠ ⁠ ⁠) to obtain AUTO_INCREMENT values

        • Using API-specific methods to obtain AUTO_INCREMENT values

        • Server-side and client-side sequence value retrieval compared

    • 11.5  Renumbering an Existing Sequence

      • Problem

      • Solution

      • Discussion

    • 11.6  Extending the Range of a Sequence Column

      • Problem

      • Solution

      • Discussion

    • 11.7  Reusing Values at the Top of a Sequence

      • Problem

      • Solution

      • Discussion

    • 11.8  Ensuring That Rows Are Renumbered in a Particular Order

      • Problem

      • Solution

      • Discussion

    • 11.9  Starting a Sequence at a Particular Value

      • Problem

      • Solution

      • Discussion

    • 11.10  Sequencing an Unsequenced Table

      • Problem

      • Solution

      • Discussion

    • 11.11  Using an AUTO_INCREMENT Column to Create Multiple Sequences

      • Problem

      • Solution

      • Discussion

    • 11.12  Managing Multiple Simultaneous AUTO_INCREMENT Values

      • Problem

      • Solution

      • Discussion

    • 11.13  Using AUTO_INCREMENT Values to Relate Tables

      • Problem

      • Solution

      • Discussion

    • 11.14  Using Sequence Generators as Counters

      • Problem

      • Solution

      • Discussion

      • See Also

    • 11.15  Generating Repeating Sequences

      • Problem

      • Solution

      • Discussion

    • 11.16  Numbering Query Output Rows Sequentially

      • Problem

      • Solution

      • Discussion

      • See Also

  • Chapter 12. Using Multiple Tables

    • 12.0  Introduction

    • 12.1  Finding Rows in One Table That Match Rows in Another

      • Problem

      • Solution

      • Discussion

    • 12.2  Finding Rows with No Match in Another Table

      • Problem

      • Solution

      • Discussion

      • See Also

    • 12.3  Comparing a Table to Itself

      • Problem

      • Solution

      • Discussion

    • 12.4  Producing Master-Detail Lists and Summaries

      • Problem

      • Solution

      • Discussion

    • 12.5  Enumerating a Many-to-Many Relationship

      • Problem

      • Solution

      • Discussion

    • 12.6  Finding Rows Containing Per-Group Minimum or Maximum Values

      • Problem

      • Solution

      • Discussion

      • See Also

    • 12.7  Computing Team Standings

      • Problem

      • Solution

      • Discussion

    • 12.8  Using a Join to Fill or Identify Holes in a List

      • Problem

      • Solution

      • Discussion

    • 12.9  Calculating Successive-Row Differences

      • Problem

      • Solution

      • Discussion

    • 12.10  Finding Cumulative Sums and Running Averages

      • Problem

      • Solution

      • Discussion

    • 12.11  Using a Join to Control Query Output Order

      • Problem

      • Solution

      • Discussion

    • 12.12  Combining Several Result Sets in a Single Query

      • Problem

      • Solution

      • Discussion

    • 12.13  Identifying and Removing Mismatched or Unattached Rows

      • Problem

      • Solution

      • Discussion

    • 12.14  Performing a Join Between Tables in Different Databases

      • Problem

      • Solution

      • Discussion

    • 12.15  Using Different MySQL Servers Simultaneously

      • Problem

      • Solution

      • Discussion

    • 12.16  Referring to Join Output Column Names in Programs

      • Problem

      • Solution

      • Discussion

  • Chapter 13. Statistical Techniques

    • 13.0  Introduction

    • 13.1  Calculating Descriptive Statistics

      • Problem

      • Solution

      • Discussion

    • 13.2  Per-Group Descriptive Statistics

      • Problem

      • Solution

      • Discussion

    • 13.3  Generating Frequency Distributions

      • Problem

      • Solution

      • Discussion

    • 13.4  Counting Missing Values

      • Problem

      • Solution

      • Discussion

    • 13.5  Calculating Linear Regressions or Correlation Coefficients

      • Problem

      • Solution

      • Discussion

    • 13.6  Generating Random Numbers

      • Problem

      • Solution

      • Discussion

    • 13.7  Randomizing a Set of Rows

      • Problem

      • Solution

      • Discussion

    • 13.8  Selecting Random Items from a Set of Rows

      • Problem

      • Solution

      • Discussion

    • 13.9  Assigning Ranks

      • Problem

      • Solution

      • Discussion

  • Chapter 14. Handling Duplicates

    • 14.0  Introduction

    • 14.1  Preventing Duplicates from Occurring in a Table

      • Problem

      • Solution

      • Discussion

      • See Also

    • 14.2  Dealing with Duplicates When Loading Rows into a Table

      • Problem

      • Solution

      • Discussion

      • See Also

    • 14.3  Counting and Identifying Duplicates

      • Problem

      • Solution

      • Discussion

    • 14.4  Eliminating Duplicates from a Table

      • Problem

      • Solution

      • Discussion

        • Removing duplicates using table replacement

        • Removing duplicates by adding an index

        • Removing duplicates of a particular row

    • 14.5  Eliminating Duplicates from a Self-Join Result

      • Problem

      • Solution

      • Discussion

  • Chapter 15. Performing Transactions

    • 15.0  Introduction

    • 15.1  Choosing a Transactional Storage Engine

      • Problem

      • Solution

      • Discussion

    • 15.2  Performing Transactions Using SQL

      • Problem

      • Solution

      • Discussion

    • 15.3  Performing Transactions from Within Programs

      • Problem

      • Solution

      • Discussion

    • 15.4  Using Transactions in Perl Programs

      • Problem

      • Solution

      • Discussion

    • 15.5  Using Transactions in Ruby Programs

      • Problem

      • Solution

      • Discussion

    • 15.6  Using Transactions in PHP Programs

      • Problem

      • Solution

      • Discussion

    • 15.7  Using Transactions in Python Programs

      • Problem

      • Solution

      • Discussion

    • 15.8  Using Transactions in Java Programs

      • Problem

      • Solution

      • Discussion

    • 15.9  Using Alternatives to Transactions

      • Problem

      • Solution

      • Discussion

        • Grouping statements using locks

        • Rewriting statements to avoid transactions

  • Chapter 16. Using Stored Routines, Triggers, and Events

    • 16.0  Introduction

    • 16.1  Creating Compound-Statement Objects

      • Problem

      • Solution

      • Discussion

    • 16.2  Using a Stored Function to Encapsulate a Calculation

      • Problem

      • Solution

      • Discussion

    • 16.3  Using a Stored Procedure to “Return” Multiple Values

      • Problem

      • Solution

      • Discussion

    • 16.4  Using a Trigger to Define Dynamic Default Column Values

      • Problem

      • Solution

      • Discussion

    • 16.5  Simulating TIMESTAMP Properties for Other Date and Time Types

      • Problem

      • Solution

      • Discussion

    • 16.6  Using a Trigger to Log Changes to a Table

      • Problem

      • Solution

      • Discussion

    • 16.7  Using Events to Schedule Database Actions

      • Problem

      • Solution

      • Discussion

  • Chapter 17. Introduction to MySQL on the Web

    • 17.0  Introduction

    • 17.1  Basic Principles of Web Page Generation

      • Problem

      • Solution

      • Discussion

    • 17.2  Using Apache to Run Web Scripts

      • Problem

      • Solution

      • Discussion

        • Perl

        • Ruby

        • PHP

        • Python

    • 17.3  Using Tomcat to Run Web Scripts

      • Problem

      • Solution

      • Discussion

        • Installing the mcb application

        • Installing the JDBC driver

        • Installing the JSTL distribution

        • Writing JSP pages with JSTL

        • Writing a MySQL script using JSP and JSTL

    • 17.4  Encoding Special Characters in Web Output

      • Problem

      • Solution

      • Discussion

        • General encoding principles

        • Encoding special characters using web APIs

  • Chapter 18. Incorporating Query Results into Web Pages

    • 18.0  Introduction

    • 18.1  Displaying Query Results as Paragraph Text

      • Problem

      • Solution

      • Discussion

      • See Also

    • 18.2  Displaying Query Results as Lists

      • Problem

      • Solution

      • Discussion

        • Ordered lists

        • Unordered lists

        • Definition lists

        • Unmarked lists

        • Nested lists

      • See Also

    • 18.3  Displaying Query Results as Tables

      • Problem

      • Solution

      • Discussion

      • See Also

    • 18.4  Displaying Query Results as Hyperlinks

      • Problem

      • Solution

      • Discussion

    • 18.5  Creating a Navigation Index from Database Content

      • Problem

      • Solution

      • Discussion

        • Creating a single-page navigation index

        • Creating a multiple-page navigation index

      • See Also

    • 18.6  Storing Images or Other Binary Data

      • Problem

      • Solution

      • Discussion

        • Storing images with LOAD_FILE⁠(⁠ ⁠ ⁠)

        • Storing images using a script

      • See Also

    • 18.7  Retrieving Images or Other Binary Data

      • Problem

      • Solution

      • Discussion

    • 18.8  Serving Banner Ads

      • Problem

      • Solution

      • Discussion

    • 18.9  Serving Query Results for Download

      • Problem

      • Solution

      • Discussion

    • 18.10  Using a Template System to Generate Web Pages

      • Problem

      • Solution

      • Discussion

        • Using page template for web page generation in Ruby

        • Using Smarty for web page generation in PHP

  • Chapter 19. Processing Web Input with MySQL

    • 19.0  Introduction

    • 19.1  Writing Scripts That Generate Web Forms

      • Problem

      • Solution

      • Discussion

        • Perl

        • Ruby

        • PHP

        • Python

        • Java

      • See Also

    • 19.2  Creating Single-Pick Form Elements from Database Content

      • Problem

      • Solution

      • Discussion

    • 19.3  Creating Multiple-Pick Form Elements from Database Content

      • Problem

      • Solution

      • Discussion

    • 19.4  Loading a Database Record into a Form

      • Problem

      • Solution

      • Discussion

    • 19.5  Collecting Web Input

      • Problem

      • Solution

      • Discussion

        • Web input extraction conventions

    • 19.6  Validating Web Input

      • Problem

      • Solution

      • Discussion

    • 19.7  Storing Web Input in a Database

      • Problem

      • Solution

      • Discussion

      • See Also

    • 19.8  Processing File Uploads

      • Problem

      • Solution

      • Discussion

        • Uploads in Perl

        • Uploads in PHP

        • Uploads in Python

    • 19.9  Performing Searches and Presenting the Results

      • Problem

      • Solution

      • Discussion

    • 19.10  Generating Previous-Page and Next-Page Links

      • Problem

      • Solution

      • Discussion

        • Paged displays with previous-page and next-page links

        • Paged displays with links to each page

    • 19.11  Generating “Click to Sort” Table Headings

      • Problem

      • Solution

      • Discussion

    • 19.12  Web Page Access Counting

      • Problem

      • Solution

      • Discussion

    • 19.13  Web Page Access Logging

      • Problem

      • Solution

      • Discussion

    • 19.14  Using MySQL for Apache Logging

      • Problem

      • Solution

      • Discussion

        • Setting up database logging

        • Analyzing the logfile

        • Other logging issues

  • Chapter 20. Using MySQL-Based Web Session Management

    • 20.0  Introduction

      • Session Management Issues

    • 20.1  Using MySQL-Based Sessions in Perl Applications

      • Problem

      • Solution

      • Discussion

        • Installing Apache::Session

        • The Apache::Session interface

        • A sample application

        • Session expiration

    • 20.2  Using MySQL-Based Storage in Ruby Applications

      • Problem

      • Solution

      • Discussion

    • 20.3  Using MySQL-Based Storage with the PHP Session Manager

      • Problem

      • Solution

      • Discussion

        • The PHP session management interface

        • Specifying a user-defined storage module

    • 20.4  Using MySQL for Session-Backing Store with Tomcat

      • Problem

      • Solution

      • Discussion

        • The Servlet and JSP Session Interface

        • A sample JSP session application

        • Telling Tomcat to save session records in MySQL

        • Session expiration in Tomcat

        • Session tracking in Tomcat

  • Appendix A. Obtaining MySQL Software

    • Obtaining Sample Source Code and Data

    • Obtaining MySQL and Related Software

      • MySQL

      • Perl Support

      • Ruby Support

      • PHP Support

      • Python Support

      • Java Support

      • Web Servers

  • Appendix B. Executing Programs from the Command Line

    • Setting Environment Variables

      • Setting the PATH Variable on Unix

      • Setting the PATH Variable on Windows

    • Executing Programs

      • Executing Perl, Ruby, PHP, or Python Scripts

      • Compiling and Executing Java Programs

  • Appendix C. JSP and Tomcat Primer

    • Servlet and JavaServer Pages Overview

      • JSP Pages: An Alternative to Servlets

      • Custom Actions and Tag Libraries

    • Setting Up a Tomcat Server

      • Installing a Tomcat Distribution

      • Starting and Stopping Tomcat

    • Tomcat’s Directory Structure

      • Application Directories

      • Configuration and Control Directories

      • Class Directories

      • Operational Directories

    • Restarting Applications Without Restarting Tomcat

    • Web Application Structure

    • Elements of JSP Pages

      • Scripting Elements

      • JSP Directives

      • Action Elements

      • Using a Tag Library

      • Implicit JSP Objects

      • Levels of Scope in JSP Pages

  • Appendix D. References

    • MySQL Resources

    • Perl Resources

    • Ruby Resources

    • PHP Resources

    • Python Resources

    • Java Resources

    • Other Resources

  • Index

Nội dung

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

TỪ KHÓA LIÊN QUAN

w