Chapter 1. MySQL Pocket Reference
Introduction
When I fly across the country, I often pass the hours programming on my PowerBook. If that programming involves MySQL, I inevitably end up lugging around the book I co-wrote, Managing and Using MySQL (O'Reilly). I don't carry around the book to show it off; the problem is that no matter how experienced you are with MySQL, you never know when you will need to look up the exact syntax of an obscure function or SQL statement.
The MySQL Pocket Reference is a quick reference that you can take with you anywhere you go. Instead of racking your brain for the exact syntax of a variant of ALTER TABLE
that you generally never use, you can reach into your laptop case and grab this reference. As an experienced MySQL architect, administrator, or programmer, you can look to this reference.
This book does not, however, teach MySQL. I expect that you have learned or are in the process of learning MySQL from a book such as Managing and Using MySQL . Though I start with a reference on MySQL setup, it is designed to help you remember the full process of MySQL configurationnot to teach you the process.
Acknowledgments
I first would like to thank my editor Andy Oram, as always, for helping me along. I would also like to thank the book's strong technical reviewers, Paul Dubois, Judith Myerson, and Tim Allwine. Finally, I would like to thank my co-authors for Managing and Using MySQL , Tim King and Randy Jay Yarger, who helped set the foundation that made this pocket reference possible and necessary.
Conventions
The following conventions are used in this book:
Constant width
Used to indicate anything that might appear in a program, including keywords, function names, SQL commands, and variable names. This font is also used for code examples, output displayed by commands, and system configuration files.
Constant width bold
Used to indicate user input.
Constant width italic
Used to indicate an element (e.g., a filename or variable) that you supply.
ItalicUsed to indicate directory names, filenames, program names, Unix commands, and URLs. This font is also used to introduce new terms and for emphasis.
MySQL 5
If you have been using MySQL for a while, you really don't need to learn a thing about MySQL 5 to keep going. Everything you are used to using still works just as it always has. For the most part, MySQL 5 is about adding enterprise database features seen in other database engines without burdening MySQL with concepts that make it harder to learn and use.
Views
Views are denormalized, table-like structures that represent a snapshot of your data that match specific query parameters. You can thus represent as data from a single table the result of a complex join. New commands supporting views include CREATE VIEW
, DROP VIEW
, and ALTER VIEW
.
Triggers
A database trigger is functionality that you create that gets executed whenever a specific event occurs on a table. For example, you can trigger behavior for a table whenever a new row is inserted. New commands supporting triggers include CREATE TRIGGER
and DROP TRIGGER
.
Stored Procedures
Stored procedures are the big feature most people have been waiting for. A stored procedure is much like creating a function that is written entirely in SQL and stored in the database. Stored procedures are useful for encapsulating a number of SQL statements that always get executed together under a single logical name for use by clients. MySQL includes a number of new commands to support stored procedures:
CREATE PROCEDURE
ALTER PROCEDURE
DROP PROCEDURE
CALL
BEGIN/END
Cursors
A cursor is a tool that enables you to represent an entire data set within a MySQL stored procedure. MySQL cursors are limited in that they are asensitive (a quality affecting their response to changes in the table), nonscrolling (cursors must be used sequentially, moving forward), and read-only. New commands supporting cursors include OPEN
, FETCH
, and CLOSE
.
New Storage Engines
The most common storage engines (also known as table types) in MySQL are MyISAM and InnoDB. But a number of new ones were added in recent versions of MySQL:
ARCHIVEOffers fast stores and selects without indexes, but no updates or deletions.
BLACKHOLEDiscards data; used to support replication.
CSVStores data in a comma-separated values format commonly used for plain text data exchange.
FALCONA new general-purpose data storage engine that may one day replace InnoDB. It is currently somewhat experimental.
FEDERATEDOffers access to a database on a remote server.
MERGECombines multiple MyISAM tables.
NDB/NDBCLUSTERNetwork database, used with MySQL Cluster.
Database Events
Introduced with MySQL 5.1, database events allow you to arrange for SQL that runs at a specified time in the future either once, or on a recurring calendar.
Setup
You can install MySQL by compiling the source code with the options that best suit your needs, or by downloading and installing a prebuilt binary. In general, you'll want to use the package management system (such as the BSD ports system) appropriate to your operating system. You can also find both binary and source code at the MySQL web site, http://www.mysql.com.
Before installing using either approach, you need to prepare your operating system for MySQL. Specifically, you should create a mysql user and group under which MySQL will run.
Downloading MySQL
MySQL AB changes the download process somewhat frequently, so the exact process of downloading MySQL may vary from the details described here. MySQL comes in standard and debug packages. When in doubt, get the standard package. It is generally what you will want for a production server.
If you are having runtime problems with your MySQL environment, you can test your application against a Debug install to get detailed debug information on your MySQL operation. You do not want to use the Debug package for any production environment .
The MySQL download page also provides a variety of additional tools, including test suites, client utilities, libraries, and header files. These tools are not essential to getting MySQL up and running, though they may be necessary for programming on a machine without a MySQL server installation or just to make life easier.
Configuration
MySQL has three different kinds of configuration, both for the server process at server startup and for the client processes when a user executes them. In order of preference, these configuration options include:
Command-line options
Configuration file options