MySQL - Manual


565 10 4MB

English Pages 787

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
General Information About MySQL......Page 23
What Is MySQL......Page 24
What Is MySQL AB......Page 25
About This Manual......Page 26
Conventions Used in This Manual......Page 27
The Main Features of MySQL......Page 28
How Stable Is MySQL?......Page 30
Year 2000 Compliance......Page 33
Books About MySQL......Page 35
Useful MySQL-related Links......Page 41
MySQL Mailing Lists......Page 50
The MySQL Mailing Lists......Page 51
How to Report Bugs or Problems......Page 53
MySQL Licensing Policy......Page 58
Copyrights Used by MySQL......Page 59
Selling Products that use MySQL......Page 60
MySQL Licensing and Support Costs......Page 61
Payment information......Page 62
Contact Information......Page 63
Basic E-mail Support......Page 64
Login Support......Page 65
Telephone Support......Page 66
MySQL Extensions to ANSI SQL92......Page 67
MySQL Differences Compared to ANSI SQL92......Page 69
Sub-selects......Page 70
Transactions......Page 71
Foreign Keys......Page 73
Why We Did Not Implement Foreign Keys......Page 74
-- as the Start of a Comment......Page 75
How to Cope Without COMMIT/ROLLBACK......Page 76
Known errors and design deficiencies in MySQL......Page 77
How MySQL Compares to mSQL......Page 81
How to Convert mSQL Tools for MySQL......Page 83
How mSQL 2.0 SQL Syntax Differs from MySQL......Page 84
MySQL and PostgreSQL development strategies......Page 87
Featurewise Comparison of MySQL and PostgreSQL......Page 88
Benchmarking MySQL and PostgreSQL......Page 91
MySQL and the future (The TODO)......Page 94
Things that should be in 4.0......Page 95
Things that must be done in the real near future......Page 96
Things that have to be done sometime......Page 100
Some things we don't have any plans to do......Page 101
Installing MySQL on Linux......Page 102
Installing MySQL on Windows......Page 103
How to Get MySQL......Page 104
Operating Systems Supported by MySQL......Page 107
Which MySQL Version to Use......Page 109
Installation Layouts......Page 111
MySQL Binaries Compiled by MySQL AB......Page 112
Installing a MySQL Source Distribution......Page 114
Quick Installation Overview......Page 115
Typical configure Options......Page 117
Installing from the Development Source Tree......Page 120
Problems Compiling?......Page 121
MIT-pthreads Notes......Page 123
Post-installation Setup and Testing......Page 124
Problems Running mysql_install_db......Page 128
Problems Starting the MySQL Server......Page 129
Starting and Stopping MySQL Automatically......Page 131
Upgrading/Downgrading MySQL......Page 132
Upgrading From Version 3.22 to Version 3.23......Page 133
Upgrading from Version 3.20 to Version 3.21......Page 135
Upgrading to Another Architecture......Page 136
Linux Notes (All Linux Versions)......Page 137
Linux Notes for Binary Distributions......Page 141
Linux x86 Notes......Page 142
Linux PowerPC Notes......Page 143
Starting MySQL on Windows 95 or Windows 98......Page 144
Starting MySQL on Windows NT or Windows 2000......Page 145
Running MySQL on Windows......Page 146
Connecting to a Remote MySQL from Windows with SSH......Page 147
Compiling MySQL Clients on Windows......Page 148
MySQL-Windows Compared to Unix MySQL......Page 149
Solaris Notes......Page 151
Solaris 2.7/2.8 Notes......Page 153
BSD Notes......Page 154
FreeBSD Notes......Page 155
BSD/OS Version 2.x Notes......Page 156
Mac OS X Notes......Page 157
HP-UX Notes for Binary Distributions......Page 158
HP-UX Version 11.x Notes......Page 159
IBM-AIX notes......Page 161
SunOS 4 Notes......Page 162
Alpha-DEC-UNIX Notes (Tru64)......Page 163
Alpha-DEC-OSF1 Notes......Page 164
SGI Irix Notes......Page 165
SCO Notes......Page 166
OS/2 Notes......Page 168
Novell Netware Notes......Page 169
Connecting to and Disconnecting from the Server......Page 170
Entering Queries......Page 171
Creating and Using a Database......Page 174
Creating and Selecting a Database......Page 175
Creating a Table......Page 176
Loading Data into a Table......Page 177
Selecting All Data......Page 178
Selecting Particular Rows......Page 179
Selecting Particular Columns......Page 180
Sorting Rows......Page 181
Date Calculations......Page 183
Pattern Matching......Page 186
Counting Rows......Page 189
Using More Than one Table......Page 191
Getting Information About Databases and Tables......Page 192
Examples of Common Queries......Page 193
The Row Holding the Maximum of a Certain Column......Page 194
The Rows Holding the Group-wise Maximum of a Certain Field......Page 195
Using user variables......Page 196
Using Foreign Keys......Page 197
Searching on Two Keys......Page 198
Using mysql in Batch Mode......Page 199
Queries from Twin Project......Page 200
Find all Non-distributed Twins......Page 201
Using MySQL with Apache......Page 203
mysqld Command-line Options......Page 205
my.cnf Option Files......Page 209
Installing Many Servers on the Same Machine......Page 212
Running Multiple MySQL Servers on the Same Machine......Page 213
General Security Guidelines......Page 214
How to Make MySQL Secure Against Crackers......Page 217
Startup Options for mysqld Concerning Security......Page 218
How the Privilege System Works......Page 219
Privileges Provided by MySQL......Page 222
Connecting to the MySQL Server......Page 224
Access Control, Stage 1: Connection Verification......Page 225
Access Control, Stage 2: Request Verification......Page 228
Causes of Access denied Errors......Page 230
GRANT and REVOKE Syntax......Page 234
MySQL User Names and Passwords......Page 237
When Privilege Changes Take Effect......Page 238
Setting Up the Initial MySQL Privileges......Page 239
Adding New Users to MySQL......Page 240
Setting Up Passwords......Page 243
Keeping Your Password Secure......Page 244
Database Backups......Page 245
RESTORE TABLE Syntax......Page 246
CHECK TABLE Syntax......Page 247
REPAIR TABLE Syntax......Page 248
myisamchk Invocation Syntax......Page 249
General Options for myisamchk......Page 250
Check Options for myisamchk......Page 251
Repair Options for myisamchk......Page 252
Other Options for myisamchk......Page 253
myisamchk Memory Usage......Page 254
Using myisamchk for Crash Recovery......Page 255
How to Repair Tables......Page 256
Setting Up a Table Maintenance Regimen......Page 259
Getting Information About a Table......Page 260
ANALYZE TABLE Syntax......Page 266
FLUSH Syntax......Page 267
SHOW Syntax......Page 268
Retrieving information about Database, Tables, Columns, and Indexes......Page 269
SHOW STATUS......Page 270
SHOW VARIABLES......Page 274
SHOW PROCESSLIST......Page 283
The Character Set Used for Data and Sorting......Page 284
Non-English Error Messages......Page 285
Adding a New Character Set......Page 286
The character definition arrays......Page 287
Multi-byte Character Support......Page 288
Overview of the Server-Side Scripts and Utilities......Page 289
safe_mysqld, the wrapper around mysqld......Page 290
mysqld_multi, program for managing multiple MySQL servers......Page 292
myisampack, The MySQL Compressed Read-only Table Generator......Page 295
mysqld-max, An extended mysqld server......Page 301
Overview of the Client-Side Scripts and Utilities......Page 303
The Command-line Tool......Page 304
mysqladmin, Administrating a MySQL Server......Page 310
Using mysqlcheck for Table Maintenance and Crash Recovery......Page 311
mysqldump, Dumping Table Structure and Data......Page 314
mysqlhotcopy, Copying MySQL Databases and Tables......Page 317
mysqlimport, Importing Data from Text Files......Page 318
How to Run SQL Commands from a Text File......Page 321
The General Query Log......Page 322
The Binary Update Log......Page 323
Log File Maintenance......Page 325
Replication Implementation Overview......Page 326
How To Set Up Replication......Page 327
Replication Features and Known Problems......Page 328
Replication Options in my.cnf......Page 330
SQL Commands Related to Replication......Page 333
Replication FAQ......Page 335
Troubleshooting Replication......Page 338
MySQL Design Limitations/Tradeoffs......Page 341
Portability......Page 342
The MySQL Benchmark Suite......Page 343
Optimizing SELECTs and Other Queries......Page 345
EXPLAIN Syntax (Get Information About a SELECT)......Page 346
Speed of SELECT Queries......Page 351
How MySQL Optimizes WHERE Clauses......Page 352
How MySQL Optimizes DISTINCT......Page 353
How MySQL Optimizes LIMIT......Page 354
Speed of INSERT Queries......Page 355
Speed of UPDATE Queries......Page 356
Other Optimization Tips......Page 357
Table Locking Issues......Page 360
Get Your Data as Small as Possible......Page 362
How MySQL Uses Indexes......Page 363
Column Indexes......Page 365
Multiple-Column Indexes......Page 366
How MySQL Opens and Closes Tables......Page 367
System/Compile Time and Startup Parameter Tuning......Page 368
Tuning Server Parameters......Page 369
How Compiling and Linking Affects the Speed of MySQL......Page 371
How MySQL Uses Memory......Page 372
How MySQL uses DNS......Page 373
SET Syntax......Page 374
Disk Issues......Page 376
Using Symbolic Links......Page 377
Using Symbolic Links for Tables......Page 378
Strings......Page 380
NULL Values......Page 382
Database, Table, Index, Column, and Alias Names......Page 383
User Variables......Page 384
Comment Syntax......Page 385
Is MySQL Picky About Reserved Words?......Page 386
Column Types......Page 387
Numeric Types......Page 392
Date and Time Types......Page 394
The DATETIME, DATE, and TIMESTAMP Types......Page 395
The TIME Type......Page 398
The YEAR Type......Page 399
The CHAR and VARCHAR Types......Page 400
The BLOB and TEXT Types......Page 401
The ENUM Type......Page 402
The SET Type......Page 403
Using Column Types from Other Database Engines......Page 404
Column Type Storage Requirements......Page 405
Functions for Use in SELECT and WHERE Clauses......Page 406
Comparison Operators......Page 407
Logical Operators......Page 410
Control Flow Functions......Page 411
String Functions......Page 412
String Comparison Functions......Page 419
Case Sensitivity......Page 421
Arithmetic Operations......Page 422
Mathematical Functions......Page 423
Date and Time Functions......Page 427
Bit Functions......Page 435
Miscellaneous Functions......Page 436
SELECT Syntax......Page 439
JOIN Syntax......Page 443
UNION Syntax......Page 444
INSERT Syntax......Page 445
INSERT ... SELECT Syntax......Page 446
INSERT DELAYED syntax......Page 447
UPDATE Syntax......Page 448
DELETE Syntax......Page 449
TRUNCATE Syntax......Page 450
LOAD DATA INFILE Syntax......Page 451
DROP DATABASE Syntax......Page 457
CREATE TABLE Syntax......Page 458
ALTER TABLE Syntax......Page 465
CREATE INDEX Syntax......Page 469
USE Syntax......Page 470
BEGIN/COMMIT/ROLLBACK Syntax......Page 471
LOCK TABLES/UNLOCK TABLES Syntax......Page 472
HANDLER Syntax......Page 474
MySQL Full-text Search......Page 475
New Features of Full-text Search to Appear in MySQL 4.0......Page 477
Full-text Search TODO......Page 478
MyISAM Tables......Page 479
Static (Fixed-length) Table Characteristics......Page 482
Dynamic Table Characteristics......Page 483
MyISAM table problems.......Page 484
Clients is using or hasn't closed the table properly......Page 485
MERGE Tables......Page 486
ISAM Tables......Page 488
HEAP Tables......Page 489
Installing BDB......Page 490
Some characteristic of BDB tables:......Page 491
Some things we need to fix for BDB in the near future:......Page 492
Errors You May Get When Using BDB Tables......Page 493
InnoDB startup options......Page 494
Creating InnoDB table space......Page 497
Creating InnoDB tables......Page 498
Adding and removing InnoDB data and log files......Page 499
Backing up and recovering an InnoDB database......Page 500
Checkpoints......Page 501
Consistent read......Page 502
Next-key locking: avoiding the phantom problem......Page 503
Locks set by different SQL statements in InnoDB......Page 504
An example of how the consistent read works in InnoDB......Page 505
Performance tuning tips......Page 506
The InnoDB Monitor......Page 507
Implementation of multiversioning......Page 509
Physical structure of an index......Page 510
Physical record structure......Page 511
Disk i/o......Page 512
File space management......Page 513
Error handling......Page 514
InnoDB contact information......Page 515
DBI with DBD::mysql......Page 517
The DBI Interface......Page 518
More DBI/DBD Information......Page 523
How To Install MyODBC......Page 524
How to Fill in the Various Fields in the ODBC Administrator Program......Page 525
Connect parameters for MyODBC......Page 526
Programs Known to Work with MyODBC......Page 527
Reporting Problems with MyODBC......Page 532
MySQL C API......Page 533
C API Datatypes......Page 534
C API Function Overview......Page 536
mysql_affected_rows()......Page 540
mysql_close()......Page 541
mysql_change_user()......Page 542
mysql_character_set_name()......Page 543
mysql_data_seek()......Page 544
mysql_drop_db()......Page 545
mysql_eof()......Page 546
mysql_error()......Page 548
mysql_fetch_field()......Page 549
mysql_fetch_field_direct()......Page 550
mysql_fetch_lengths()......Page 551
mysql_fetch_row()......Page 552
mysql_field_count()......Page 553
mysql_field_seek()......Page 554
mysql_get_client_info()......Page 555
mysql_get_server_info()......Page 556
mysql_init()......Page 557
mysql_insert_id()......Page 558
mysql_list_dbs()......Page 559
mysql_list_processes()......Page 560
mysql_list_tables()......Page 561
mysql_num_fields()......Page 562
mysql_options()......Page 563
mysql_ping()......Page 565
mysql_real_connect()......Page 566
mysql_real_escape_string()......Page 569
mysql_real_query()......Page 570
mysql_row_seek()......Page 571
mysql_select_db()......Page 572
mysql_stat()......Page 573
mysql_store_result()......Page 574
mysql_use_result()......Page 575
my_init()......Page 576
C Embedded Server Function Descriptions......Page 577
mysql_server_init()......Page 578
What Results Can I Get From a Query?......Page 579
Problems Linking with the C API......Page 580
How to Make a Threaded Client......Page 581
Overview of the Embedded MySQL Server Library......Page 582
A Simple Embedded Server Example......Page 583
MySQL C++ APIs......Page 589
MySQL Eiffel wrapper......Page 590
CREATE FUNCTION/DROP FUNCTION Syntax......Page 591
Adding a New User-definable Function......Page 592
UDF Calling Sequences......Page 593
Argument Processing......Page 594
Return Values and Error Handling......Page 595
Compiling and Installing User-definable Functions......Page 596
Adding a New Native Function......Page 597
MySQL Internals......Page 599
Running the MySQL Test Suite......Page 600
Extending the MySQL Test Suite......Page 601
Reporting Bugs in the MySQL Test Suite......Page 602
How to Determine What Is Causing Problems......Page 604
MySQL server has gone away Error......Page 605
Can't connect to [local] MySQL server error......Page 606
Host '...' is blocked Error......Page 607
Some non-transactional changed tables couldn't be rolled back Error......Page 608
Communication Errors / Aborted Connection......Page 609
Can't create/write to file Error......Page 610
Ignoring user Error......Page 611
File Not Found......Page 612
Problems When Linking with the MySQL Client Library......Page 613
How to Run MySQL As a Normal User......Page 614
Administration Related Issues......Page 615
What To Do If MySQL Keeps Crashing......Page 616
How MySQL Handles a Full Disk......Page 618
How to Protect or change the MySQL socket file /tmp/mysql.sock......Page 619
Case Sensitivity in Searches......Page 620
Problems with NULL Values......Page 621
Problems with alias......Page 622
Solving Problems with No Matching Rows......Page 623
How To Change the Order of Columns in a Table......Page 624
TEMPORARY TABLE problems......Page 625
Some Information Search Engines Concentrated on Some Area......Page 626
Web Sites that Use MySQL as a Backend......Page 627
Web Sites that Use PHP and MySQL......Page 628
Uncategorized Pages......Page 629
MySQL customer usage......Page 632
APIs......Page 633
Clients......Page 636
Web Tools......Page 640
Authentication Tools......Page 641
Converters......Page 642
Useful Tools......Page 643
Useful Functions......Page 644
Uncategorized......Page 645
Developers at MySQL AB......Page 646
Contributors to MySQL......Page 648
Supporters to MySQL......Page 653
Changes in release 4.0.0......Page 655
Changes in release 3.23.x (Stable)......Page 656
Changes in release 3.23.42......Page 657
Changes in release 3.23.40......Page 658
Changes in release 3.23.38......Page 659
Changes in release 3.23.37......Page 660
Changes in release 3.23.36......Page 661
Changes in release 3.23.34......Page 662
Changes in release 3.23.33......Page 663
Changes in release 3.23.32......Page 664
Changes in release 3.23.30......Page 665
Changes in release 3.23.29......Page 666
Changes in release 3.23.28......Page 668
Changes in release 3.23.27......Page 669
Changes in release 3.23.26......Page 670
Changes in release 3.23.25......Page 671
Changes in release 3.23.23......Page 672
Changes in release 3.23.22......Page 673
Changes in release 3.23.21......Page 674
Changes in release 3.23.18......Page 675
Changes in release 3.23.16......Page 676
Changes in release 3.23.15......Page 677
Changes in release 3.23.13......Page 678
Changes in release 3.23.11......Page 679
Changes in release 3.23.9......Page 680
Changes in release 3.23.8......Page 681
Changes in release 3.23.6......Page 682
Changes in release 3.23.5......Page 683
Changes in release 3.23.3......Page 684
Changes in release 3.23.2......Page 685
Changes in release 3.23.0......Page 686
Changes in release 3.22.32......Page 688
Changes in release 3.22.28......Page 689
Changes in release 3.22.24......Page 690
Changes in release 3.22.21......Page 691
Changes in release 3.22.16......Page 692
Changes in release 3.22.13......Page 693
Changes in release 3.22.11......Page 694
Changes in release 3.22.9......Page 695
Changes in release 3.22.7......Page 696
Changes in release 3.22.5......Page 697
Changes in release 3.22.4......Page 699
Changes in release 3.22.1......Page 700
Changes in release 3.22.0......Page 701
Changes in release 3.21.33......Page 702
Changes in release 3.21.30......Page 703
Changes in release 3.21.27......Page 704
Changes in release 3.21.24......Page 705
Changes in release 3.21.22......Page 706
Changes in release 3.21.20......Page 707
Changes in release 3.21.17......Page 708
Changes in release 3.21.15......Page 709
Changes in release 3.21.13......Page 710
Changes in release 3.21.12......Page 711
Changes in release 3.21.9......Page 712
Changes in release 3.21.7......Page 713
Changes in release 3.21.3......Page 714
Changes in release 3.21.2......Page 715
Changes in release 3.21.0......Page 716
Changes in release 3.20.18......Page 717
Changes in release 3.20.16......Page 718
Changes in release 3.20.14......Page 719
Changes in release 3.20.10......Page 720
Changes in release 3.20.7......Page 721
Changes in release 3.20.6......Page 722
Changes in release 3.20.0......Page 723
Changes in release 3.19.4......Page 724
Changes in release 3.19.3......Page 725
Comments on porting to other systems......Page 726
Compiling MYSQL for debugging.......Page 727
Creating trace files......Page 728
Debugging mysqld under gdb......Page 729
Using a stack trace......Page 730
Making a test case when you experience table corruption......Page 731
The DBUG package.......Page 732
Locking methods......Page 734
Comments about RTS threads......Page 735
Differences between different thread packages......Page 737
Environment Variables......Page 738
Description of MySQL regular expression syntax......Page 739
What is Unireg?......Page 742
Preamble......Page 743
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION......Page 744
How to Apply These Terms to Your New Programs......Page 748
Preamble......Page 749
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION......Page 750
How to Apply These Terms to Your New Libraries......Page 757
Installing a MySQL Binary Distribution......Page 758
Installing Perl on Unix......Page 760
Installing ActiveState Perl on Windows......Page 761
Problems Using the Perl DBI/DBD Interface......Page 762
Functions for Use with GROUP BY Clauses......Page 763
SQL command, type and function index......Page 766
Concept Index......Page 775
Recommend Papers

MySQL - Manual

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

MySQL Reference Manual

c 1997-2001 MySQL AB Copyright

i

Table of Contents 1

General Information About MySQL. . . . . . . . . 1 1.1

1.2

1.3

1.4

MySQL, MySQL AB, and Open Source . . . . . . . . . . . . . . . . . . . 2 1.1.1 What Is MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.1.2 What Is MySQL AB . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.1.3 About This Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.1.4 Conventions Used in This Manual . . . . . . . . . . . . . . . . 5 1.1.5 History of MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.1.6 The Main Features of MySQL . . . . . . . . . . . . . . . . . . . 6 1.1.7 How Stable Is MySQL? . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.1.8 How Big Can MySQL Tables Be? . . . . . . . . . . . . . . . 11 1.1.9 Year 2000 Compliance . . . . . . . . . . . . . . . . . . . . . . . . . 11 MySQL Information Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2.1 Books About MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2.2 General SQL Information and Tutorials . . . . . . . . . 19 1.2.3 Useful MySQL-related Links . . . . . . . . . . . . . . . . . . . . 19 1.2.4 MySQL Mailing Lists . . . . . . . . . . . . . . . . . . . . . . . . . . 28 1.2.4.1 The MySQL Mailing Lists . . . . . . . . . . . . . 29 1.2.4.2 Asking Questions or Reporting Bugs. . . . 31 1.2.4.3 How to Report Bugs or Problems . . . . . . 31 1.2.4.4 Guidelines for Answering Question on the Mailing List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 MySQL Licensing and Support . . . . . . . . . . . . . . . . . . . . . . . . . . 36 1.3.1 MySQL Licensing Policy . . . . . . . . . . . . . . . . . . . . . . . 36 1.3.2 Copyrights Used by MySQL . . . . . . . . . . . . . . . . . . . . 37 1.3.2.1 Copyright Changes . . . . . . . . . . . . . . . . . . . . 38 1.3.3 Example Licensing Situations . . . . . . . . . . . . . . . . . . . 38 1.3.3.1 Selling Products that use MySQL . . . . . . 38 1.3.3.2 ISP MySQL Services . . . . . . . . . . . . . . . . . . 39 1.3.3.3 Running a Web Server Using MySQL . . . 39 1.3.4 MySQL Licensing and Support Costs. . . . . . . . . . . . 39 1.3.4.1 Payment information . . . . . . . . . . . . . . . . . . 40 1.3.4.2 Contact Information. . . . . . . . . . . . . . . . . . . 41 1.3.5 Types of Commercial Support . . . . . . . . . . . . . . . . . . 42 1.3.5.1 Basic E-mail Support. . . . . . . . . . . . . . . . . . 42 1.3.5.2 Extended E-mail Support . . . . . . . . . . . . . . 43 1.3.5.3 Login Support . . . . . . . . . . . . . . . . . . . . . . . . 43 1.3.5.4 Extended Login Support . . . . . . . . . . . . . . . 44 1.3.5.5 Telephone Support . . . . . . . . . . . . . . . . . . . . 44 1.3.5.6 Support for other table handlers. . . . . . . . 45 How Standards-compatible Is MySQL?. . . . . . . . . . . . . . . . . . . 45 1.4.1 MySQL Extensions to ANSI SQL92 . . . . . . . . . . . . . 45 1.4.2 MySQL Differences Compared to ANSI SQL92. . . 47 1.4.3 Running MySQL in ANSI Mode . . . . . . . . . . . . . . . . 48

ii

1.5

1.6

2

1.4.4 Functionality Missing from MySQL . . . . . . . . . . . . . 48 1.4.4.1 Sub-selects . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 1.4.4.2 SELECT INTO TABLE . . . . . . . . . . . . . . . . . . . 49 1.4.4.3 Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . 49 1.4.4.4 Stored Procedures and Triggers . . . . . . . . 51 1.4.4.5 Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . 51 1.4.4.6 Why We Did Not Implement Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 1.4.4.7 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 1.4.4.8 ‘--’ as the Start of a Comment . . . . . . . . . 53 1.4.5 What Standards Does MySQL Follow? . . . . . . . . . . 54 1.4.6 How to Cope Without COMMIT/ROLLBACK . . . . . . . . 54 1.4.7 Known errors and design deficiencies in MySQL . . 55 How MySQL Compares to Other Databases . . . . . . . . . . . . . . 59 1.5.1 How MySQL Compares to mSQL . . . . . . . . . . . . . . . . 59 1.5.1.1 How to Convert mSQL Tools for MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 1.5.1.2 How mSQL and MySQL Client/Server Communications Protocols Differ . . . . . . . . . . . 62 1.5.1.3 How mSQL 2.0 SQL Syntax Differs from MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 1.5.2 How MySQL Compares to PostgreSQL . . . . . . . . . . 65 1.5.2.1 MySQL and PostgreSQL development strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 1.5.2.2 Featurewise Comparison of MySQL and PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 1.5.2.3 Benchmarking MySQL and PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 MySQL and the future (The TODO) . . . . . . . . . . . . . . . . . . . . 72 1.6.1 Things that should be in 4.0 . . . . . . . . . . . . . . . . . . . . 73 1.6.2 Things that must be done in the real near future . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 1.6.3 Things that have to be done sometime . . . . . . . . . . 78 1.6.4 Some things we don’t have any plans to do . . . . . . 79

MySQL Installation . . . . . . . . . . . . . . . . . . . . . . . 80 2.1

2.2

2.3

Quick Standard Installation of MySQL. . . . . . . . . . . . . . . . . . . 2.1.1 Installing MySQL on Linux. . . . . . . . . . . . . . . . . . . . . 2.1.2 Installing MySQL on Windows . . . . . . . . . . . . . . . . . General Installation Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.1 How to Get MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.2 Operating Systems Supported by MySQL . . . . . . . 2.2.3 Which MySQL Version to Use . . . . . . . . . . . . . . . . . . 2.2.4 Installation Layouts. . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2.5 How and When Updates Are Released. . . . . . . . . . . 2.2.6 MySQL Binaries Compiled by MySQL AB. . . . . . . Installing a MySQL Source Distribution. . . . . . . . . . . . . . . . . . 2.3.1 Quick Installation Overview . . . . . . . . . . . . . . . . . . . .

80 80 81 82 82 85 87 89 90 90 92 93

iii

2.4

2.5

2.6

2.3.2 Applying Patches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 2.3.3 Typical configure Options . . . . . . . . . . . . . . . . . . . . 95 2.3.4 Installing from the Development Source Tree. . . . . 98 2.3.5 Problems Compiling? . . . . . . . . . . . . . . . . . . . . . . . . . . 99 2.3.6 MIT-pthreads Notes . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Post-installation Setup and Testing . . . . . . . . . . . . . . . . . . . . . 102 2.4.1 Problems Running mysql_install_db . . . . . . . . . 106 2.4.2 Problems Starting the MySQL Server . . . . . . . . . . 107 2.4.3 Starting and Stopping MySQL Automatically . . . 109 Upgrading/Downgrading MySQL . . . . . . . . . . . . . . . . . . . . . . . 110 2.5.1 Upgrading From Version 3.23 to Version 4.0 . . . . 111 2.5.2 Upgrading From Version 3.22 to Version 3.23 . . . 111 2.5.3 Upgrading from Version 3.21 to Version 3.22 . . . . 113 2.5.4 Upgrading from Version 3.20 to Version 3.21 . . . . 113 2.5.5 Upgrading to Another Architecture . . . . . . . . . . . . 114 Operating System Specific Notes . . . . . . . . . . . . . . . . . . . . . . . 115 2.6.1 Linux Notes (All Linux Versions) . . . . . . . . . . . . . . 115 2.6.1.1 Linux Notes for Binary Distributions . . 119 2.6.1.2 Linux x86 Notes . . . . . . . . . . . . . . . . . . . . . 120 2.6.1.3 Linux SPARC Notes . . . . . . . . . . . . . . . . . 121 2.6.1.4 Linux Alpha Notes . . . . . . . . . . . . . . . . . . . 121 2.6.1.5 Linux PowerPC Notes . . . . . . . . . . . . . . . . 121 2.6.1.6 Linux MIPS Notes . . . . . . . . . . . . . . . . . . . 122 2.6.1.7 Linux IA64 Notes . . . . . . . . . . . . . . . . . . . . 122 2.6.2 Windows Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 2.6.2.1 Starting MySQL on Windows 95 or Windows 98 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 2.6.2.2 Starting MySQL on Windows NT or Windows 2000 . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 2.6.2.3 Running MySQL on Windows. . . . . . . . . 124 2.6.2.4 Connecting to a Remote MySQL from Windows with SSH . . . . . . . . . . . . . . . . . . . . . . 125 2.6.2.5 Splitting Data Across Different Disks on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 2.6.2.6 Compiling MySQL Clients on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 2.6.2.7 MySQL-Windows Compared to Unix MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 2.6.3 Solaris Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 2.6.3.1 Solaris 2.7/2.8 Notes . . . . . . . . . . . . . . . . . 131 2.6.3.2 Solaris x86 Notes . . . . . . . . . . . . . . . . . . . . 132 2.6.4 BSD Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 2.6.4.1 FreeBSD Notes . . . . . . . . . . . . . . . . . . . . . . 133 2.6.4.2 NetBSD notes . . . . . . . . . . . . . . . . . . . . . . . 134 2.6.4.3 OpenBSD Notes . . . . . . . . . . . . . . . . . . . . . 134 2.6.4.4 OpenBSD 2.5 Notes . . . . . . . . . . . . . . . . . . 134 2.6.4.5 OpenBSD 2.8 Notes . . . . . . . . . . . . . . . . . . 134 2.6.4.6 BSD/OS Notes . . . . . . . . . . . . . . . . . . . . . . 134

iv

2.6.5

2.6.6

2.6.7 2.6.8 2.6.9

3

2.6.4.7 BSD/OS Version 2.x Notes . . . . . . . . . . . 2.6.4.8 BSD/OS Version 3.x Notes . . . . . . . . . . . 2.6.4.9 BSD/OS Version 4.x Notes . . . . . . . . . . . Mac OS X Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.6.5.1 Mac OS X Public Beta . . . . . . . . . . . . . . . 2.6.5.2 Mac OS X Server . . . . . . . . . . . . . . . . . . . . Other Unix Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.6.6.1 HP-UX Notes for Binary Distributions ........................................ 2.6.6.2 HP-UX Version 10.20 Notes. . . . . . . . . . . 2.6.6.3 HP-UX Version 11.x Notes. . . . . . . . . . . . 2.6.6.4 IBM-AIX notes . . . . . . . . . . . . . . . . . . . . . . 2.6.6.5 SunOS 4 Notes . . . . . . . . . . . . . . . . . . . . . . 2.6.6.6 Alpha-DEC-UNIX Notes (Tru64) . . . . . . 2.6.6.7 Alpha-DEC-OSF1 Notes . . . . . . . . . . . . . . 2.6.6.8 SGI Irix Notes . . . . . . . . . . . . . . . . . . . . . . . 2.6.6.9 SCO Notes . . . . . . . . . . . . . . . . . . . . . . . . . . 2.6.6.10 SCO Unixware Version 7.0 Notes . . . . . OS/2 Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . BeOS Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Novell Netware Notes . . . . . . . . . . . . . . . . . . . . . . . . .

134 135 135 135 136 136 136 136 137 137 139 140 141 142 143 144 146 146 147 147

Introduction to MySQL: A MySQL Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 3.1 3.2 3.3

3.4 3.5

Connecting to and Disconnecting from the Server . . . . . . . . 148 Entering Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Creating and Using a Database. . . . . . . . . . . . . . . . . . . . . . . . . 152 3.3.1 Creating and Selecting a Database . . . . . . . . . . . . . 153 3.3.2 Creating a Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 3.3.3 Loading Data into a Table . . . . . . . . . . . . . . . . . . . . 155 3.3.4 Retrieving Information from a Table . . . . . . . . . . . 156 3.3.4.1 Selecting All Data . . . . . . . . . . . . . . . . . . . 156 3.3.4.2 Selecting Particular Rows . . . . . . . . . . . . . 157 3.3.4.3 Selecting Particular Columns . . . . . . . . . 158 3.3.4.4 Sorting Rows . . . . . . . . . . . . . . . . . . . . . . . . 159 3.3.4.5 Date Calculations . . . . . . . . . . . . . . . . . . . . 161 3.3.4.6 Working with NULL Values . . . . . . . . . . . . 164 3.3.4.7 Pattern Matching . . . . . . . . . . . . . . . . . . . . 164 3.3.4.8 Counting Rows . . . . . . . . . . . . . . . . . . . . . . 167 3.3.4.9 Using More Than one Table . . . . . . . . . . 169 Getting Information About Databases and Tables . . . . . . . 170 Examples of Common Queries . . . . . . . . . . . . . . . . . . . . . . . . . . 171 3.5.1 The Maximum Value for a Column . . . . . . . . . . . . 172 3.5.2 The Row Holding the Maximum of a Certain Column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 3.5.3 Maximum of Column per Group . . . . . . . . . . . . . . . 173 3.5.4 The Rows Holding the Group-wise Maximum of a Certain Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

v

3.6 3.7

3.8

4

3.5.5 Using user variables. . . . . . . . . . . . . . . . . . . . . . . . . . . 3.5.6 Using Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.5.7 Searching on Two Keys . . . . . . . . . . . . . . . . . . . . . . . 3.5.8 Calculating visits per day . . . . . . . . . . . . . . . . . . . . . Using mysql in Batch Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . Queries from Twin Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.7.1 Find all Non-distributed Twins . . . . . . . . . . . . . . . . 3.7.2 Show a Table on Twin Pair Status . . . . . . . . . . . . . Using MySQL with Apache . . . . . . . . . . . . . . . . . . . . . . . . . . . .

174 175 176 177 177 178 179 181 181

MySQL Database Administration . . . . . . . . . 183 4.1

Configuring MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 4.1.1 mysqld Command-line Options . . . . . . . . . . . . . . . . 183 4.1.2 my.cnf Option Files . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 4.1.3 Installing Many Servers on the Same Machine . . 190 4.1.4 Running Multiple MySQL Servers on the Same Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 4.2 General Security Issues and the MySQL Access Privilege System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 4.2.1 General Security Guidelines . . . . . . . . . . . . . . . . . . . 192 4.2.2 How to Make MySQL Secure Against Crackers . . 195 4.2.3 Startup Options for mysqld Concerning Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 4.2.4 What the Privilege System Does . . . . . . . . . . . . . . . 197 4.2.5 How the Privilege System Works . . . . . . . . . . . . . . . 197 4.2.6 Privileges Provided by MySQL . . . . . . . . . . . . . . . . 200 4.2.7 Connecting to the MySQL Server . . . . . . . . . . . . . . 202 4.2.8 Access Control, Stage 1: Connection Verification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 4.2.9 Access Control, Stage 2: Request Verification . . . 206 4.2.10 Causes of Access denied Errors . . . . . . . . . . . . . . 208 4.3 MySQL User Account Management . . . . . . . . . . . . . . . . . . . . . 212 4.3.1 GRANT and REVOKE Syntax . . . . . . . . . . . . . . . . . . . . . 212 4.3.2 MySQL User Names and Passwords . . . . . . . . . . . . 215 4.3.3 When Privilege Changes Take Effect . . . . . . . . . . . 216 4.3.4 Setting Up the Initial MySQL Privileges. . . . . . . . 217 4.3.5 Adding New Users to MySQL . . . . . . . . . . . . . . . . . 218 4.3.6 Setting Up Passwords . . . . . . . . . . . . . . . . . . . . . . . . . 221 4.3.7 Keeping Your Password Secure . . . . . . . . . . . . . . . . 222 4.4 Disaster Prevention and Recovery . . . . . . . . . . . . . . . . . . . . . . 223 4.4.1 Database Backups . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 4.4.2 BACKUP TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . 224 4.4.3 RESTORE TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . 224 4.4.4 CHECK TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . 225 4.4.5 REPAIR TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . 226 4.4.6 Using myisamchk for Table Maintenance and Crash Recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 4.4.6.1 myisamchk Invocation Syntax . . . . . . . . . 227

vi

4.5

4.6

4.7

4.8

4.4.6.2 General Options for myisamchk . . . . . . . 228 4.4.6.3 Check Options for myisamchk . . . . . . . . . 229 4.4.6.4 Repair Options for myisamchk . . . . . . . . 230 4.4.6.5 Other Options for myisamchk . . . . . . . . . 231 4.4.6.6 myisamchk Memory Usage . . . . . . . . . . . . 232 4.4.6.7 Using myisamchk for Crash Recovery . . 233 4.4.6.8 How to Check Tables for Errors . . . . . . . 234 4.4.6.9 How to Repair Tables . . . . . . . . . . . . . . . . 234 4.4.6.10 Table Optimization . . . . . . . . . . . . . . . . . 237 4.4.7 Setting Up a Table Maintenance Regimen . . . . . . 237 4.4.8 Getting Information About a Table . . . . . . . . . . . . 238 Database Administration Language Reference . . . . . . . . . . . 244 4.5.1 OPTIMIZE TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . 244 4.5.2 ANALYZE TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . 244 4.5.3 FLUSH Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 4.5.4 KILL Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 4.5.5 SHOW Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 4.5.5.1 Retrieving information about Database, Tables, Columns, and Indexes . . . . . . . . . . . . . 247 4.5.5.2 SHOW TABLE STATUS . . . . . . . . . . . . . . . . . . 248 4.5.5.3 SHOW STATUS . . . . . . . . . . . . . . . . . . . . . . . . . 248 4.5.5.4 SHOW VARIABLES . . . . . . . . . . . . . . . . . . . . . 252 4.5.5.5 SHOW LOGS . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 4.5.5.6 SHOW PROCESSLIST . . . . . . . . . . . . . . . . . . . 261 4.5.5.7 SHOW GRANTS . . . . . . . . . . . . . . . . . . . . . . . . . 262 4.5.5.8 SHOW CREATE TABLE . . . . . . . . . . . . . . . . . . 262 MySQL Localization and International Usage . . . . . . . . . . . 262 4.6.1 The Character Set Used for Data and Sorting . . . 262 4.6.1.1 German character set . . . . . . . . . . . . . . . . 263 4.6.2 Non-English Error Messages . . . . . . . . . . . . . . . . . . . 263 4.6.3 Adding a New Character Set . . . . . . . . . . . . . . . . . . 264 4.6.4 The character definition arrays . . . . . . . . . . . . . . . . 265 4.6.5 String Collating Support . . . . . . . . . . . . . . . . . . . . . . 266 4.6.6 Multi-byte Character Support . . . . . . . . . . . . . . . . . 266 MySQL Server-Side Scripts and Utilities . . . . . . . . . . . . . . . . 267 4.7.1 Overview of the Server-Side Scripts and Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 4.7.2 safe mysqld, the wrapper around mysqld . . . . . . . 268 4.7.3 mysqld multi, program for managing multiple MySQL servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 4.7.4 myisampack, The MySQL Compressed Read-only Table Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 4.7.5 mysqld-max, An extended mysqld server . . . . . . . 279 MySQL Client-Side Scripts and Utilities . . . . . . . . . . . . . . . . 281 4.8.1 Overview of the Client-Side Scripts and Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 4.8.2 The Command-line Tool . . . . . . . . . . . . . . . . . . . . . . 282 4.8.3 mysqladmin, Administrating a MySQL Server . . 288

vii 4.8.4 Using mysqlcheck for Table Maintenance and Crash Recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 4.8.5 mysqldump, Dumping Table Structure and Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 4.8.6 mysqlhotcopy, Copying MySQL Databases and Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 4.8.7 mysqlimport, Importing Data from Text Files . . . 296 4.8.8 Showing Databases, Tables, and Columns . . . . . . 299 4.8.9 perror, Explaining Error Codes . . . . . . . . . . . . . . . . 299 4.8.10 How to Run SQL Commands from a Text File . . 299 4.9 The MySQL Log Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 4.9.1 The Error Log. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 4.9.2 The General Query Log . . . . . . . . . . . . . . . . . . . . . . . 300 4.9.3 The Update Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 4.9.4 The Binary Update Log . . . . . . . . . . . . . . . . . . . . . . . 301 4.9.5 The Slow Query Log . . . . . . . . . . . . . . . . . . . . . . . . . . 303 4.9.6 Log File Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . 303 4.10 Replication in MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 4.10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 4.10.2 Replication Implementation Overview . . . . . . . . . 304 4.10.3 How To Set Up Replication . . . . . . . . . . . . . . . . . . 305 4.10.4 Replication Features and Known Problems . . . . 306 4.10.5 Replication Options in my.cnf . . . . . . . . . . . . . . . . 308 4.10.6 SQL Commands Related to Replication . . . . . . . 311 4.10.7 Replication FAQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313 4.10.8 Troubleshooting Replication . . . . . . . . . . . . . . . . . . 316

5

MySQL Optimization . . . . . . . . . . . . . . . . . . . . 319 5.1

5.2

Optimization Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 5.1.1 MySQL Design Limitations/Tradeoffs . . . . . . . . . . 319 5.1.2 Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320 5.1.3 What Have We Used MySQL For? . . . . . . . . . . . . . 321 5.1.4 The MySQL Benchmark Suite . . . . . . . . . . . . . . . . . 321 5.1.5 Using Your Own Benchmarks . . . . . . . . . . . . . . . . . . 323 Optimizing SELECTs and Other Queries . . . . . . . . . . . . . . . . . 323 5.2.1 EXPLAIN Syntax (Get Information About a SELECT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 5.2.2 Estimating Query Performance . . . . . . . . . . . . . . . . 329 5.2.3 Speed of SELECT Queries . . . . . . . . . . . . . . . . . . . . . . 329 5.2.4 How MySQL Optimizes WHERE Clauses . . . . . . . . . 330 5.2.5 How MySQL Optimizes DISTINCT . . . . . . . . . . . . . 331 5.2.6 How MySQL Optimizes LEFT JOIN and RIGHT JOIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 5.2.7 How MySQL Optimizes LIMIT . . . . . . . . . . . . . . . . . 332 5.2.8 Speed of INSERT Queries . . . . . . . . . . . . . . . . . . . . . . 333 5.2.9 Speed of UPDATE Queries . . . . . . . . . . . . . . . . . . . . . . 334 5.2.10 Speed of DELETE Queries . . . . . . . . . . . . . . . . . . . . . 335 5.2.11 Other Optimization Tips . . . . . . . . . . . . . . . . . . . . . 335

viii 5.3

5.4

5.5

5.6

6

Locking Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 5.3.1 How MySQL Locks Tables . . . . . . . . . . . . . . . . . . . . 338 5.3.2 Table Locking Issues . . . . . . . . . . . . . . . . . . . . . . . . . . 338 Optimizing Database Structure . . . . . . . . . . . . . . . . . . . . . . . . . 340 5.4.1 Design Choices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 5.4.2 Get Your Data as Small as Possible . . . . . . . . . . . . 340 5.4.3 How MySQL Uses Indexes . . . . . . . . . . . . . . . . . . . . 341 5.4.4 Column Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 5.4.5 Multiple-Column Indexes. . . . . . . . . . . . . . . . . . . . . . 344 5.4.6 How MySQL Opens and Closes Tables . . . . . . . . . 345 5.4.7 Drawbacks to Creating Large Numbers of Tables in the Same Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 5.4.8 Why So Many Open tables? . . . . . . . . . . . . . . . . . . . 346 Optimizing the MySQL Server . . . . . . . . . . . . . . . . . . . . . . . . . 346 5.5.1 System/Compile Time and Startup Parameter Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 5.5.2 Tuning Server Parameters . . . . . . . . . . . . . . . . . . . . . 347 5.5.3 How Compiling and Linking Affects the Speed of MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 5.5.4 How MySQL Uses Memory . . . . . . . . . . . . . . . . . . . . 350 5.5.5 How MySQL uses DNS . . . . . . . . . . . . . . . . . . . . . . . 351 5.5.6 SET Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 Disk Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 5.6.1 Using Symbolic Links . . . . . . . . . . . . . . . . . . . . . . . . . 355 5.6.1.1 Using Symbolic Links for Databases . . . 356 5.6.1.2 Using Symbolic Links for Tables . . . . . . 356

MySQL Language Reference . . . . . . . . . . . . . 358 6.1

6.2

Language Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 6.1.1 Literals: How to Write Strings and Numbers . . . . 358 6.1.1.1 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 6.1.1.2 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 6.1.1.3 Hexadecimal Values . . . . . . . . . . . . . . . . . . 360 6.1.1.4 NULL Values . . . . . . . . . . . . . . . . . . . . . . . . . 360 6.1.2 Database, Table, Index, Column, and Alias Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 6.1.3 Case Sensitivity in Names . . . . . . . . . . . . . . . . . . . . . 362 6.1.4 User Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362 6.1.5 Comment Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 6.1.6 Is MySQL Picky About Reserved Words?. . . . . . . 364 Column Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 6.2.1 Numeric Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 6.2.2 Date and Time Types . . . . . . . . . . . . . . . . . . . . . . . . . 372 6.2.2.1 Y2K Issues and Date Types. . . . . . . . . . . 373 6.2.2.2 The DATETIME, DATE, and TIMESTAMP Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 6.2.2.3 The TIME Type . . . . . . . . . . . . . . . . . . . . . . 376 6.2.2.4 The YEAR Type . . . . . . . . . . . . . . . . . . . . . . 377

ix 6.2.3 String Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 6.2.3.1 The CHAR and VARCHAR Types . . . . . . . . . 378 6.2.3.2 The BLOB and TEXT Types . . . . . . . . . . . . 379 6.2.3.3 The ENUM Type . . . . . . . . . . . . . . . . . . . . . . 380 6.2.3.4 The SET Type . . . . . . . . . . . . . . . . . . . . . . . 381 6.2.4 Choosing the Right Type for a Column. . . . . . . . . 382 6.2.5 Using Column Types from Other Database Engines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 6.2.6 Column Type Storage Requirements . . . . . . . . . . . 383 6.3 Functions for Use in SELECT and WHERE Clauses . . . . . . . . . 384 6.3.1 Non-Type-Specific Operators and Functions . . . . 385 6.3.1.1 Parenthesis . . . . . . . . . . . . . . . . . . . . . . . . . . 385 6.3.1.2 Comparison Operators . . . . . . . . . . . . . . . 385 6.3.1.3 Logical Operators . . . . . . . . . . . . . . . . . . . . 388 6.3.1.4 Control Flow Functions. . . . . . . . . . . . . . . 389 6.3.2 String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390 6.3.2.1 String Comparison Functions . . . . . . . . . 397 6.3.2.2 Case Sensitivity . . . . . . . . . . . . . . . . . . . . . . 399 6.3.3 Numeric Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 6.3.3.1 Arithmetic Operations . . . . . . . . . . . . . . . 400 6.3.3.2 Mathematical Functions . . . . . . . . . . . . . . 401 6.3.4 Date and Time Functions . . . . . . . . . . . . . . . . . . . . . 405 6.3.5 Other Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413 6.3.5.1 Bit Functions . . . . . . . . . . . . . . . . . . . . . . . . 413 6.3.5.2 Miscellaneous Functions . . . . . . . . . . . . . . 414 6.4 Data Manipulation: SELECT, INSERT, UPDATE, DELETE . . . . 417 6.4.1 SELECT Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417 6.4.1.1 JOIN Syntax . . . . . . . . . . . . . . . . . . . . . . . . . 421 6.4.2 UNION Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422 6.4.3 INSERT Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423 6.4.3.1 INSERT ... SELECT Syntax . . . . . . . . . . . 424 6.4.4 INSERT DELAYED syntax . . . . . . . . . . . . . . . . . . . . . . . 425 6.4.5 UPDATE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426 6.4.6 DELETE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 6.4.7 TRUNCATE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428 6.4.8 REPLACE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 6.4.9 LOAD DATA INFILE Syntax . . . . . . . . . . . . . . . . . . . . . 429 6.5 Data Definition: CREATE, DROP, ALTER . . . . . . . . . . . . . . . . . . 435 6.5.1 CREATE DATABASE Syntax . . . . . . . . . . . . . . . . . . . . . . 435 6.5.2 DROP DATABASE Syntax . . . . . . . . . . . . . . . . . . . . . . . . 435 6.5.3 CREATE TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . 436 6.5.3.1 Silent Column Specification Changes . . 443 6.5.4 ALTER TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . 443 6.5.5 RENAME TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . 447 6.5.6 DROP TABLE Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . 447 6.5.7 CREATE INDEX Syntax . . . . . . . . . . . . . . . . . . . . . . . . . 447 6.5.8 DROP INDEX Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . 448 6.6 Basic MySQL User Utility Commands . . . . . . . . . . . . . . . . . . 448

x 6.6.1 USE Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448 6.6.2 DESCRIBE Syntax (Get Information About Columns) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 6.7 MySQL Transactional and Locking Commands . . . . . . . . . . 449 6.7.1 BEGIN/COMMIT/ROLLBACK Syntax . . . . . . . . . . . . . . . 449 6.7.2 LOCK TABLES/UNLOCK TABLES Syntax . . . . . . . . . . . 450 6.7.3 SET TRANSACTION Syntax . . . . . . . . . . . . . . . . . . . . . . 452 6.8 HANDLER Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452 6.9 MySQL Full-text Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453 6.9.1 Fulltext restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 6.9.2 Fine-tuning MySQL Full-text Search . . . . . . . . . . . 455 6.9.3 New Features of Full-text Search to Appear in MySQL 4.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455 6.9.4 Full-text Search TODO . . . . . . . . . . . . . . . . . . . . . . . 456

7

MySQL Table Types . . . . . . . . . . . . . . . . . . . . . 457 7.1

7.2 7.3 7.4 7.5

7.6

MyISAM Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 7.1.1 Space Needed for Keys . . . . . . . . . . . . . . . . . . . . . . . . 460 7.1.2 MyISAM Table Formats . . . . . . . . . . . . . . . . . . . . . . 460 7.1.2.1 Static (Fixed-length) Table Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460 7.1.2.2 Dynamic Table Characteristics . . . . . . . . 461 7.1.2.3 Compressed Table Characteristics . . . . . 462 7.1.3 MyISAM table problems.. . . . . . . . . . . . . . . . . . . . . . 462 7.1.3.1 Corrupted MyISAM tables. . . . . . . . . . . . 463 7.1.3.2 Clients is using or hasn’t closed the table properly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 MERGE Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464 ISAM Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466 HEAP Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467 BDB or Berkeley DB Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . 468 7.5.1 Overview of BDB Tables . . . . . . . . . . . . . . . . . . . . . . 468 7.5.2 Installing BDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468 7.5.3 BDB startup options. . . . . . . . . . . . . . . . . . . . . . . . . . 469 7.5.4 Some characteristic of BDB tables: . . . . . . . . . . . . . . 469 7.5.5 Some things we need to fix for BDB in the near future: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470 7.5.6 Operating systems supported by BDB . . . . . . . . . . 471 7.5.7 Errors You May Get When Using BDB Tables . . 471 InnoDB Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 7.6.1 InnoDB tables overview . . . . . . . . . . . . . . . . . . . . . . . 472 7.6.2 InnoDB startup options . . . . . . . . . . . . . . . . . . . . . . . 472 7.6.3 Creating InnoDB table space . . . . . . . . . . . . . . . . . . 475 7.6.3.1 If something goes wrong in database creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476 7.6.4 Creating InnoDB tables . . . . . . . . . . . . . . . . . . . . . . . 476 7.6.4.1 Converting MyISAM tables to InnoDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477

xi 7.6.5 Adding and removing InnoDB data and log files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477 7.6.6 Backing up and recovering an InnoDB database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478 7.6.6.1 Checkpoints . . . . . . . . . . . . . . . . . . . . . . . . . 479 7.6.7 Moving an InnoDB database to another machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 7.6.8 InnoDB transaction model . . . . . . . . . . . . . . . . . . . . 480 7.6.8.1 Consistent read . . . . . . . . . . . . . . . . . . . . . . 480 7.6.8.2 Locking reads . . . . . . . . . . . . . . . . . . . . . . . . 481 7.6.8.3 Next-key locking: avoiding the phantom problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481 7.6.8.4 Locks set by different SQL statements in InnoDB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482 7.6.8.5 Deadlock detection and rollback . . . . . . . 483 7.6.8.6 An example of how the consistent read works in InnoDB. . . . . . . . . . . . . . . . . . . . . . . . . 483 7.6.9 Performance tuning tips . . . . . . . . . . . . . . . . . . . . . . . 484 7.6.9.1 The InnoDB Monitor . . . . . . . . . . . . . . . . . 485 7.6.1