Introduction

Welcome to MySQL Tutorial. This book is a fast-paced tutorial to get you up to speed with MySQL quickly. It covers MySQL both from an end user's perspective and from an administrator's perspective.

In this introduction, we will review the following:

• Why use MySQL?

• Why MySQL instead of another database?

• What's different in MySQL 4.0 and 4.1?

• What features are still to come in future versions?

• Who should read this book?

• How this book works

• A note on MySQL licensing

Why Use MySQL?

MySQL is an excellent database server product. There are many reasons you should consider using MySQL for your database server.

MySQL is fast and stable. This is the key to its success. In February 2002, eWeek (www.eweek.com) performed a benchmarking study of the major database systems including Oracle, Microsoft SQL Server, DB2, and MySQL. The study rated MySQL and Oracle 9 as the best overall performers. This study was done using only the alpha of MySQL 4.0, but MySQL 4.1 is even faster.

MySQL is available as free software and as commercial software. It is available under a dual licensing scheme. All MySQL software is available under the GNU General Public License (GPL), but in situations in which you need or want a commercial license, you can buy one.

MySQL supports the vast majority of features considered important by the database community, such as transactions, row-level locking, foreign keys, subqueries, and full text searching. Version 5.0 will add stored procedures to the list.

MySQL scales well. It is used by some very demanding customers including Yahoo! Finance, Slashdot, and the U.S. Census Bureau.

MySQL is a great tool for learning about databases in general due to the ease of installation and use and the small hard disk and memory footprint.

Overall, MySQL is an excellent choice for your database application.

Why MySQL Instead of Another Database?

Too many databases are available to compare MySQL to each one in detail, but MySQL offers a combination of performance, price, and features that others will find hard to match.

Performance

MySQL is undeniably fast. Oracle, Microsoft, and IBM all claim to sell the fastest database in the world, which, depending on your level of credulity, demonstrates either that a benchmark can be made to prove anything its sponsor requires or that different products excel under different conditions.

On the MySQL Web site, you can read benchmarks comparing MySQL and various other databases. These benchmarks generally show MySQL solidly outperforming competitors. Although you are probably wise to take all benchmarks, especially vendor-designed benchmarks, with a grain or two of salt, all available evidence including independent tests indicates that MySQL is among the fastest products available.

The benchmark results can be found at

www.mysql.com/information/benchmarks.html

and the tests are included in the source download in case you want to make comparisons in your own environment.

The benchmarking exercise undertaken by eWeek magazine in 2002 showed MySQL performing as well as Oracle in backing a Web-based Java application running on a quad processor Windows machine. These two products outperformed IBM's DB2, Microsoft's SQL Server, and Sybase's ASE. We will come back to price in a moment, but it is worth noting that in this benchmark, one of the leading performers was free, whereas the other had a price of $160,000 ($40,000 per processor). See www.eweek.com/article2/0,3959,293,00.asp for details.

Speed has always been a key MySQL design consideration. New features are added to MySQL only when this can be done without harming performance. Sometimes this means that features are added more slowly than users would like, but it has ensured that MySQL has always been fast. Your own testing or experience will probably confirm this assertion.

Price

Price is perhaps the easiest point to compare. For many purposes, MySQL is a free application. The GPL allows you to use the software, alter the source code, and redistribute MySQL to other people who will also be bound by the GPL. Under some circumstances, such as if you want to redistribute MySQL as part of a commercial product, you will need to purchase a commercial license. A single server license costs $220 or $440 (at the time of writing), depending on whether you want to use the InnoDB table type. In other words, MySQL uses a dual licensing scheme where the free use is channeled through the GPL and the commercial use is channeled through industry-standard EULAs (End-User License Agreements) and OEM (Original Equipment Manufacturer) agreements. MySQL AB's general rule is, “if you are free, so are we; if you are commercial, so are we.”

The major competitors are commercial, with complex pricing schemes that depend on the intended use, number of processors in each server, and number of users that will connect. The Oracle Database, Microsoft SQL Server, and IBM DB2 Sybase can all cost tens of thousands of dollars in moderate scenarios and hundreds of thousands of dollars on a server with many processors and many connected clients.

MySQL is sometimes compared to other Open Source databases, such as PostgreSQL and Firebird. Of these Open Source databases, MySQL is the only product with one company behind it, owning all intellectual property rights and offering full commercial licenses including liability and indemnifications required by large user organizations.

Another category of software that MySQL is at times compared with are the inexpensive non-client-server databases, targeted at home or small business users (such as Microsoft Access and Filemaker Pro). While often having an easy-to-use graphical user interface (GUI), the programs in this category lack important functionality, as well as the stability, scalability, and speed needed for mission critical applications.

Stability

The developers at MySQL have always regarded stability to be of prime importance. All MySQL versions released in binary form—even alpha releases—must pass the MySQL Test Suite. This process tests functions and other features, as well as the results of operations where a bug has been fixed in the past; thereby, ensuring that bugs can never be accidentally re-introduced.

Developers must also give the fixing of a bug higher priority than their other development tasks. Basically, their other work stops until any bugs related to their field of expertise are fixed. The rule is that MySQL releases should be free of all known and reproducible bugs. Naturally, some things cannot be resolved without causing problems elsewhere. This is particularly the case with production versions that should not be subject to major changes that can influence their stability. In these instances, the issue is documented and fixed in all later versions.

Finally, quality is ensured through the MySQL customers and community. With over four million users around the world working in a wide variety of environments, this provides unparalleled opportunity for finding bugs even in early stages of development. The bug reporting and handling system at MySQL is public, so people can see what others have reported and add their own comments.

Ease of Use

Another key feature of MySQL is its ease of use. No complicated configuration procedure is required in order to get started. MySQL Server works adequately straight out of the box. The defaults are set for minimal use of disk and memory resources. For optimal performance and for specific production requirements such as logging, tuning of this default setup will naturally be required. Sample configuration files are included to help with this.

Features

Feature comparison depends greatly on which features you regard as important. MySQL has some features, such as full text search, replication, and support for massive tables, that are missing or immature in other low-cost offerings. It is, however, missing features such as stored procedures and views that are standard in the high-cost options and available in some of the low-cost products. Some of these omissions are planned for upcoming versions (such as stored procedures); others (such as views) will take longer. Some MySQL features (such as row level locking) are missing even from most of the top-priced systems.

The feature comparison page on MySQL's Web site at http://www.mysql.com/information/features.html allows you to do a very fine-grained comparison between MySQL and around 20 competitors to see what is supported in different offerings. What follows, though, is a brief list of features MySQL has that are not offered by all competitors, as well as a list of features MySQL lacks that exist elsewhere.

MySQL 4.1 offers

ACID-compliant transactions

• Cross-platform support

• Replication

• Support for huge tables and databases

• Full text search

• Subqueries

• Support for most SQL 92 syntax

MySQL does not currently include

• Views

• Stored procedures

• Triggers

What's Different in MySQL 4.0 and 4.1?

MySQL 3.23 was the production version for a long time. MySQL 4.0.13 was released as the production version of 4.0 in March 2003. The alpha of 4.1 was released in April.

If you have used 3.23, these are the major changes you'll note in 4.0:

• The InnoDB storage engine is included in the standard binary. This was included in some later versions of 3.23. The InnoDB engine is ACID compliant, supporting transactions, foreign keys, and row-level locking.

• MySQL now uses a query cache, storing the results returned by queries for later reuse and, hence, greatly improving performance for common queries.

• Full-text indexing and searching, which were added in 3.23.23, have been improved with the addition of Boolean mode.

• MERGE tables now support INSERT and AUTO_INCREMENT.

The result sets from SELECT queries can now be merged with UNION.

• You can now delete rows from multiple tables with a single DELETE statement.

• User privilege management has been refined. More privileges have been added to give you a finer grain of control, and you can also now limit a user's resource use.

• You can now make changes to server configuration without having to restart the server.

• A new C language library, libmysqld, is now available to allow you to embed MySQL servers in your programs.

• Replication, which has been available since 3.23.15, has been improved in various ways, mostly bug fixes. For example, you can now set up a slave using LOAD DATA FROM MASTER, rather than having to use mysqldump or a tool like mysqlsnapshot.

A lot of other small changes and improvements have been made.

In 4.1, there are some additional improvements. The most important change is the addition of subqueries and derived tables. You also get Unicode support, support for OpenGIS geographical data storage, and a host of other minor improvements.

What Features Are Still to Come in Future Versions?

Version 5.0 is currently in the pre-alpha stage, but you can download the source code and experiment with it if you want. The biggest change in version 5.0 is the addition of stored procedures, which have already been implemented in this pre-alpha. Other changes are also planned, including support for cursors, RTREE indexes, true VARCHARs, and a host of other minor features.

Version 5.1 should support foreign keys for all table types (among a host of other features). Views should be supported in one of the 5.x versions.

Who Should Read This Book?

This book is unique because it consists of a series of short, concise chapters, each on a tightly targeted topic, with a task-oriented focus. Each chapter ends with a series of review questions and exercises so that you can test your understanding of the concepts in that chapter.

In short, we are aiming this book to be a tutorial for smart people. This book is not a reference manual. For that, we refer you to the excellent online MySQL manual. There is no point in reinventing the wheel.

We will focus on five key areas: installing and configuring MySQL, designing and creating databases with MySQL, using MySQL, administering MySQL, and optimizing MySQL. We will cover all the core skills necessary to use MySQL on a professional basis.

You can use this book when you need to learn how to perform MySQL-related tasks for a new project, a new job, or a course when you do not have time to plow through a thousand-page book. This book has a task-oriented focus to help you get the job done.

How This Book Works

This book is divided into five parts:

Part I, “MySQL Basics,” will teach you how to install and configure MySQL on your system and will give you a tour of your installation.

Part II, “Designing and Creating Databases with MySQL,” will take you through the process of database design and creation. Readers who have done database design before can skim through this part of the book, but if databases are new to you, we suggest that you read it in detail.

Part III, “Using MySQL,” takes you through how to query MySQL on a day-to-day basis.

Part IV, “MySQL Table Types and Transactions,” explains how to use the different storage engine types in MySQL, with a special focus on the InnoDB engine and how it can be used for transactions.

Part V, “Administering MySQL,” explains the basic tasks needed to perform DBA duties with MySQL, including user management, configuration, database maintenance, backup and recovery, database security, and replication.

Part VI, “Optimizing MySQL,” will help you to get the most out of your MySQL database by optimizing your server setup, database, and queries for your specific situation.

At the end of each chapter, you'll find a set of review questions and practical exercises for you to practice the skills from that particular chapter.

A Note on MySQL Licensing

The company MySQL AB, run by MySQL's developers and founders, owns the MySQL code and documentation.

Most parts of the MySQL source code are available under the GNU General Public License. What this means in practice is that you can freely use, copy, distribute, and modify the source code for MySQL. If you copy or distribute the code (or modify and then copy and distribute the code), you must do so again under the GPL. If you distribute binaries, you must also include the source code.

You can read the full text of the GNU GPL in your MySQL distribution, online in the MySQL manual, or online at the Free Software Foundation:

http://www.gnu.org/licenses/

More information on the GPL is provided at

http://www.gnu.org/licenses/gpl-faq.html

If this does not suit your purposes—if, for example, you want to modify MySQL and sell the modified binary without access to the source code—you must purchase a commercial license from MySQL AB. You can also choose to purchase a commercial license from MySQL AB if you want to support the development of MySQL.

One important note is that the MySQL documentation is not available under the GPL, but it can be printed for personal use.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset