You are on page 1of 6

Table of Contents

Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi

Part I. Introduction
1.

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Why Is MySQL so Popular?
Elements of MySQL and Its Environment
MySQL Software Covered in This Book
The Books Web Site

2.

Installing MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Installation Choices and Platforms
Using the Command-Line Interface
Using a Text Editor
Following the Instructions in This Book
Downloading and Verifying Files from the MySQL AB Web Site
Installing Under Linux
Installing Under Windows
Installing Under Mac OS X
Using a MySQL Installation Provided by an ISP
Upgrading an Existing MySQL Server
Configuring Access to the MySQL Server
What If Things Dont Work?
The Contents of the MySQL Directory
Configuring and Controlling the Apache Web Server
Setting up Perl
Resources
Exercises

3.

4
5
7
8

9
12
18
19
21
25
51
61
69
69
73
74
80
83
87
92
93

Using the MySQL Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95


Starting the Monitor

95
v

Style, Case, and Semicolons


The Monitor Help
Running the Monitor in Batch Mode
Loading the Sample Databases
MySQL Monitor Program Options
Graphical Clients
Exercises

97
98
101
102
102
104
105

Part II. Using MySQL


4.

Modeling and Designing Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109


How Not to Develop a Database
The Database Design Process
The Entity Relationship Model
Entity Relationship Modeling Examples
Using the Entity Relationship Model
Using Tools for Database Design
Resources
Exercises

5.

Basic SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135


Using the Music Database
The SELECT Statement and Basic Querying Techniques
The INSERT Statement
The DELETE Statement
The UPDATE Statement
Exploring Databases and Tables with SHOW and mysqlshow
Exercises

6.

135
139
162
168
171
173
176

Working with Database Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179


Creating and Using Databases
Creating Tables
The Sample Music Database
Altering Structures
Deleting Structures
Exercises

7.

109
111
112
120
128
129
131
132

179
181
212
214
220
221

Advanced Querying . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223


Aliases
Aggregating Data
Advanced Joins
Nested Queries

vi | Table of Contents

223
228
236
250

User Variables
Transactions and Locking
Table Types
Exercises

8.

Doing More with MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277


Inserting Data Using Queries
Loading Data from Comma-Delimited Files
Writing Data into Comma-Delimited Files
Creating Tables with Queries
Updates and Deletes with Multiple Tables
Replacing Data
The EXPLAIN Statement
Exercises

9.

263
266
267
275

277
281
284
285
288
292
294
295

Managing Users and Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297


Understanding Users and Privileges
Creating and Using New Users
Privileges
The GRANT OPTION Privilege
How Privileges Interact
Users and Hosts
Checking Privileges
Revoking Privileges
Removing Users
Understanding and Changing Passwords
The Default Users
Devising a User Security Policy
Managing Privileges with SQL
Privileges and Performance
Resetting Forgotten MySQL Passwords
Exercises

298
299
303
306
307
308
317
322
323
324
328
333
339
346
347
349

Part III. Advanced Topics


10. Backups and Recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Dumping a Database as SQL Statements
Loading Data from an SQL Dump File
mysqlhotcopy
Scheduling Backups
The Binary Log
Checking and Repairing Corrupted Tables

353
360
361
361
365
366

Table of Contents | vii

Re-Creating Damaged Grant Tables


Resources
Exercises

369
369
369

11. Using an Options File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371


Configuring Options for the MySQL Monitor
Structure of the Options File
Scope of Options
Search Order for Options Files
Determining the Options in Effect
Exercises

371
373
374
375
376
377

12. Configuring and Tuning the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379


The MySQL Server Daemon
Server Variables
Checking Server Settings
Other Things to Consider
Resources
Exercises

379
383
388
391
392
392

Part IV. Web Database Applications with PHP


13. Web Database Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Building a Web Database Application
The Apache Web Server
Introducing PHP
Using a PHP-Enabled Web Hosting Site
Resources
Exercises

395
400
402
410
411
412

14. PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415


Language Basics
Accessing MySQL Using PHP
Modularizing Code
Processing and Using User Data
The PHP Predefined Superglobal Variables
Untainting User Data
Sessions
The Reload Problem
Using PHP for Command-Line Scripts
Resources
Exercises

viii | Table of Contents

415
427
448
452
455
456
461
462
466
470
471

15. A PHP Application: The Wedding Gift Registry . . . . . . . . . . . . . . . . . . . . . . . . . . . 473


Designing and Creating the Wedding Database
The Login Form
Passing a Message to a Script
Logging Users In and Out
The db.php Include File
Editing the List of Gifts
Loading Sample Gifts
Listing Gifts for Selection
Selecting and Deselecting Gifts
Resources
Exercises

474
476
478
480
486
487
497
497
501
507
507

Part V. Interacting with MySQL Using Perl


16. Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511
Writing Your First Perl Program
Scripting With Perl
Resources
Exercises

512
513
537
538

17. Using Perl with MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539


Connecting to the MySQL Server and Database
Handling Errors When Interacting with the Database
Using Queries That Return Answer Sets
Using Queries That Dont Return Answer Sets
Binding Queries and Variables
Importing and Exporting Data
Handling NULL Values
Resources
Exercises

539
540
542
545
546
552
554
554
554

18. Serving Perl Pages to the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 555


The Perl CGI Module
Processing User Input
A Note on mod_perl
Perl Security
Resources
Exercises

558
560
564
564
566
566

Table of Contents | ix

Part VI. Appendix


A.

The Wedding Registry Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 569

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583

x | Table of Contents

You might also like