You are on page 1of 121

MySQL

Wikibooks.org

October 26, 2012

On the 28th of April 2012 the contents of the English as well as German Wikibooks and Wikipedia projects were licensed under Creative Commons Attribution-ShareAlike 3.0 Unported license. An URI to this license is given in the list of gures on page 111. If this document is a derived work from the contents of one of these projects and the content was still licensed by the project under this license at the time of derivation this document has to be licensed under the same, a similar or a compatible license, as stated in section 4b of the license. The list of contributors is included in chapter Contributors on page 109. The licenses GPL, LGPL and GFDL are included in chapter Licenses on page 115, since this book and/or parts of it may or may not be licensed under one or more of these licenses, and thus require inclusion of these licenses. The licenses of the gures are given A A in the list of gures on page 111. This PDF was generated by the LTEX typesetting software. The LTEX source code is included as an attachment (source.7z.txt) in this PDF le. To extract the source from the PDF le, we recommend the use of http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/ utility or clicking the paper clip attachment symbol on the lower left of your PDF Viewer, selecting Save Attachment. After extracting it from the PDF le you have to rename it to source.7z. To uncompress A the resulting archive we recommend the use of http://www.7-zip.org/. The LTEX source itself was generated by a program written by Dirk Hnniger, which is freely available under an open source license from http://de.wikibooks.org/wiki/Benutzer:Dirk_Huenniger/wb2pdf. This distribution also contains a congured version of the pdflatex compiler with all necessary packages and A fonts needed to compile the LTEX source included in this PDF le.

Contents
1 Introduction . . . . . . . . . . . . . . . 1.1 What is SQL? . . . . . . . . . . 1.2 Why MySQL? . . . . . . . . . . 1.3 MySQL and its forks . . . . . . . 1.4 Notes . . . . . . . . . . . . . . . Table types . . . . . . . . . . . . . . . 2.1 Storage Engines . . . . . . . . . 2.2 Metadata about Storage Engines . 2.3 Changing the Storage Engine . . 2.4 List databases . . . . . . . . . . 2.5 List tables and views . . . . . . . 2.6 List elds . . . . . . . . . . . . . 2.7 List indexes . . . . . . . . . . . 2.8 INFORMATION_SCHEMA . . Specifying table names . . . . . . . . . Denitions . . . . . . . . . . . . . . . . User Variables . . . . . . . . . . . . . . 5.1 Session Variables . . . . . . . . 5.2 Global Variables . . . . . . . . . Alias . . . . . . . . . . . . . . . . . . . Queries . . . . . . . . . . . . . . . . . 7.1 SELECT . . . . . . . . . . . . . 7.2 Joins . . . . . . . . . . . . . . . 7.3 Subqueries . . . . . . . . . . . . 7.4 References . . . . . . . . . . . . 7.5 Resources . . . . . . . . . . . . Data manipulation . . . . . . . . . . . . 8.1 INSERT . . . . . . . . . . . . . 8.2 UPDATE . . . . . . . . . . . . . 8.3 REPLACE . . . . . . . . . . . . 8.4 DELETE and TRUNCATE . . . Table manipulation . . . . . . . . . . . 9.1 CREATE TABLE . . . . . . . . 9.2 ALTER TABLE . . . . . . . . . 9.3 Renaming a table . . . . . . . . . 9.4 DROP TABLE . . . . . . . . . . 9.5 Dealing with NULL . . . . . . . Reserved Words . . . . . . . . . . . . . Data Types . . . . . . . . . . . . . . . 11.1 TEXT and BLOB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 3 3 4 5 7 7 10 11 12 13 14 15 16 17 19 21 21 21 23 25 25 32 35 36 36 37 37 38 39 39 41 41 41 42 42 44 45 47 47

3 4 5

6 7

10 11

III

Contents 11.2 integer . . . . . . . . . . . . . 11.3 decimal . . . . . . . . . . . . . 11.4 Dates . . . . . . . . . . . . . . 11.5 set and enum . . . . . . . . . . Operators . . . . . . . . . . . . . . . 12.1 Precedence . . . . . . . . . . . 12.2 Assignment operators . . . . . 12.3 Comparison operators . . . . . 12.4 Logical operators . . . . . . . 12.5 Arithmetic operators . . . . . . 12.6 Text operators . . . . . . . . . 12.7 Bitwise operators . . . . . . . Import/export . . . . . . . . . . . . . 13.1 Export data . . . . . . . . . . . 13.2 Import data . . . . . . . . . . . Functions . . . . . . . . . . . . . . . 14.1 Syntax . . . . . . . . . . . . . 14.2 General functions . . . . . . . 14.3 Date and time . . . . . . . . . 14.4 Aggregate functions . . . . . . Exercises . . . . . . . . . . . . . . . 15.1 Practicing SELECT . . . . . . 15.2 Examples . . . . . . . . . . . . Administration . . . . . . . . . . . . 16.1 Installation . . . . . . . . . . . 16.2 Start the service . . . . . . . . 16.3 Client connection . . . . . . . 16.4 Conguration . . . . . . . . . 16.5 Privileges . . . . . . . . . . . . 16.6 Processes . . . . . . . . . . . . 16.7 Security . . . . . . . . . . . . 16.8 Backup . . . . . . . . . . . . . 16.9 Binary logs . . . . . . . . . . . 16.10 Logs . . . . . . . . . . . . . . 16.11 Admin Tools . . . . . . . . . . Replication . . . . . . . . . . . . . . 17.1 What is replication . . . . . . . 17.2 Asynchronous replication . . . Databases manipulation . . . . . . . . 18.1 Creation . . . . . . . . . . . . 18.2 Deletion . . . . . . . . . . . . 18.3 Rename . . . . . . . . . . . . 18.4 Copy . . . . . . . . . . . . . . 18.5 Migration from other databases 18.6 Tools for data modeling . . . . Appendixes . . . . . . . . . . . . . . 19.1 Cheat Sheet . . . . . . . . . . 19.2 About this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 . 47 . 48 . 49 . 51 . 51 . 52 . 52 . 55 . 56 . 58 . 60 . 61 . 61 . 61 . 63 . 63 . 63 . 65 . 66 . 69 . 69 . 71 . 73 . 73 . 76 . 77 . 78 . 79 . 82 . 84 . 84 . 87 . 87 . 87 . 89 . 89 . 89 . 95 . 95 . 95 . 95 . 96 . 96 . 96 . 101 . 101 . 104

12

13

14

15

16

17

18

19

IV

Contents 20 GNU Free Documentation License . . . . . . 21 Contributors . . . . . . . . . . . . . . . . . . List of Figures . . . . . . . . . . . . . . . . . . . 22 Licenses . . . . . . . . . . . . . . . . . . . . 22.1 GNU GENERAL PUBLIC LICENSE . 22.2 GNU Free Documentation License . . 22.3 GNU Lesser General Public License . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 109 111 115 115 116 116

1 Introduction
1.1 What is SQL?
For a more general introduction see the SQL Wikibook1 . Structured Query Language is a third generation language to query Databases. Being a 3G language it is closer to human language than machine language and therefore easier to understand and work with. Dr. E. F. Ted Codd who worked for IBM described a relational model for database in 1970. In 1992, ANSI (American National Standards Institute), the apex body, standardized most of the basic syntax. Its called SQL 92 and most databases (like Oracle, MySQL, Sybase, etc.) implement a subset of the standard (and proprietary extensions that makes them often incompatible).

1.2 Why MySQL?


Free as in Freedom - Released with GPL version 2 license (though a different license can be bought from Oracle, see below) Cost - Free! Support - Online tutorials, forums, mailing list (lists.mysql.com), paid support contracts. Speed - One of the fastest databases available. (http://www.mysql.com/why-mysql/ benchmarks/) Functionality - supports most of ANSI SQL commands. Ease of use - less need of training / retraining. Portability - easily import / export from Excel and other databases Scalable - Useful for both small as well as large databases containing billions of records and terabytes of data in hundreds of thousands of tables. Permission Control - selectively grant or revoke permissions to users.

1.2.1 The MySQL license


MySQL is available under a dual-licensing scheme: Under the GNU General Public License, version 2, ("or later" allowed in versions released before 2007): this is a Free (as in freedom), copyleft software license that allows you to use MySQL for commercial and non-commercial purposes in your application, as long as your application

http://en.wikibooks.org/wiki/SQL

Introduction is released under the GNU GPL. There is also a "FLOSS Exception" which essentially allows non-GPL'd but Free applications (such as the PHP programming language, under the PHP license) to connect to a MySQL server. The exception lists a set of free and open-source software license that can be used in addition to the GNU GPL for your MySQL-dependent Free application. A so-called "commercial" 2 , paid license, that is, a license where MySQL grants you the right to integrate MySQL with a non-FLOSS application that you are redistributing outside your own organization. 3

1.3 MySQL and its forks


MySQL is Free Software, so some forks and unofcial builds delivering contributions from the community exist.

1.3.1 MariaDB
In 2008 Sun Microsystems bought MySQL, Sun being itself later acquired by Oracle, in 2010. After the acquisition, the development process has changed. The team has started to release new MySQL versions less frequently, so the new code is less tested. There were also less contributions from the community. In 2009 Monty Widenius, the founder of MySQL, left the company and created a new one, called The Monty Program4 . He started a new fork called MariaDB. The scopes of MariaDB are: import all the new code that will be added to the main MySQL branch, but enhancing it to make it more stable; clean the MySQL code; add contributions from the community (new plugins, new features); develop the Aria storage engine, formerly named Maria; improving the performance; adding new features to the server. The license is the GNU GPLv2 (inherited from MySQL). The primary platform for MariaDB is GNU/Linux, but also works on one proprietary system. The following Storage Engine have been added:
2 3

Aria (also used for internal tables) PBXT XtraDB FederatedX SphinxSE OQGRAPH
Calling it "commercial" is misleading, because the GNU GPL can be used in commercial (but non-proprietary) projects. Proprietary projects still can connect to a MySQL server without purchasing this license by using old versions of the MySQL client connection libraries (under the GNU Lesser General Public License). However, these libraries cannot connect to the newest versions of the MySQL server. http://www.askmonty.org/

Notes Others may be added in the future.

1.3.2 Drizzle
In 2008 Brian Aker, chief architect of MySQL, left the project to start a new fork called Drizzle5 . While Oracle initially funded the project, Drizzle is now funded by Rackspace. Its characteristics are: only a small part of the MySQL code has survived in this fork, the rest being removed: only essential features are implemented in the Drizzle server; the survived code has been cleaned; Drizzle is modular: many features are or can be implemented as plugins; the software is optimized for multiCPU and multicore 64 bit machines; only GNU/Linux and UNIX systems are supported. There are no public releases of this fork, still. Its main license will be the GNU GPLv2 (inherited from MySQL), but where possible the BSD license is applied.

1.3.3 OurDelta
OurDelta6 is another fork, maintained by Open Query. The rst branch, which has number 5.0, is based on MySQL 5.0. The 5.1 branch is based on MariaDB. OurDelta includes some patches developed by the community or by third parties. OurDelta provides packages for some GNU/Linux distributions: Debian, Ubuntu, Red Hat/CentOS. It is not available for other systems, but the source code is freely available.

1.3.4 Percona Server


Percona Server is a MySQL fork maintained by Percona. It provides the ExtraDB Storage Engine, which is a fork of InnoDB, and some patches which mainly improve the performance.

1.4 Notes
7

5 6 7

http://www.drizzle.org/ http://ourdelta.org/ http://en.wikibooks.org/wiki/Category%3A

2 Table types
Every table is a logical object in a database; but it also needs to physically store its data (records) on the disk and/or in memory. Tables use a Storage Engine to do this. SE are plugins which can be installed or uninstalled into the server (if they're not builtin). Many operations are requested by the server but physically done by the SE. So, from the SE we choose for a table affects performance, stability, LOCKs type, use of the query cache, disk space required and special features. In some future versions of MySQL, partitioned tables will be able to use different SE for different partitions. Let's see which Storage Engine is good for which uses. Note: Table Type is an old term deprecated in recent versions of MySQL. It is still accepted by some SQL commands for backward compatibility, but ENGINE[s] or STORAGE ENGINE[s] should be preferred.

2.1 Storage Engines


2.1.1 MyISAM and InnoDB
MyISAM does table level locking, while InnoDB does row level locking. In addition to foreign keys, InnoDB offers transaction support, which is absolutely critical when dealing with larger applications. Speed may suffer, particularly for inserts with full transaction guarantees, because all this Foreign Key / Transaction stuff adds overhead. The default table type for MySQL on Linux is MyISAM, on Windows, normally InnoDB. MyISAM uses table level locking, which means during an UPDATE, nobody can access any other record of the same table. InnoDB however, uses Row level locking. Row level locking ensures that during an UPDATE, nobody can access that particular row, until the locking transaction issues a COMMIT. Many people use MyISAM if they need speed and InnoDB for data integrity. MyISAM Pros Fulltext search is currently only available with MyISAM tables Geometric datatypes Sometimes faster reads

Table types All numeric key values are stored with the high byte rst to allow better index compression Internal handling of one AUTO_INCREMENT column per table is supported. MyISAM automatically updates this column for INSERT and UPDATE operations. This makes AUTO_INCREMENT columns faster (at least 10%) Cons Table (not row) level locking only No foreign keys contraints (but planned for MySQL 6.x) Slower table checking and restarts after power loss, an issue for those who need high availability InnoDB Pros Provides MySQL with a transaction-safe (ACID compliant) storage engine that has commit, rollback, and crash recovery capabilities XA transactions Foreign keys Row level locking Maintains its own buffer pool for caching data and indexes in main memory Faster for some workloads, particularly those where physical ordering by primary key helps or where the automatically built hash indexes speed up record lookups Tables can be of any size even on operating systems where le size is limited to 2GB. Fast and reliable recovery from power loss. Cons Data takes more space to store ACID guarantee requires full sync to disk at transaction commit, can be turned off where speed is more important than full ACID guarantees. Data Versioning and transactions add overhead to table management. They can lead to high memory requirements to manage large numbers of locks used in row locking. Indexes are slow to build when they're added after a table has been created.Indexes should therefore be created when data is bulk-loaded. Overall, InnoDB should be used for with applications that rely highly on data integrity or need transactions, while MyISAM can be used where that is not required or where fulltext indexing is needed. Where speed is more important, both should be tried because which is faster depends on the application. Drizzle, a MySQL's fork supported by Sun Microsystems, uses InnoDB as its default engine and doesn't support MyISAM.

2.1.2 Merge Table


Synonyms: Merge, MRG_MYISAM A MERGE table is a collection of identical MyISAM tables that can be used as one. Identical means that all tables have identical column and index information, no deviation of any sort is permitted.

Storage Engines

CREATE TABLE mumbai (first_name VARCHAR(30), amount INT(10)) TYPE=MyISAM CREATE TABLE delhi (first_name VARCHAR(30), amount INT(10)) TYPE=MyISAM CREATE TABLE total (first_name VARCHAR(30), amount INT(10)) TYPE=MERGE UNION=(mumbai,delhi)

Merges can be used to work around MySQL's or system's lesize limits. In fact those limits affect single MyISAM datales, but don't affect the whole Merge table, which doesn't have a datale. In the past, in some cases Merge and MyISAM could be used to replace views, which were not supported by MySQL. Merge could be used as a base table and MyISAM tables could be used as views containing part of the base table data. A SELECT on the Merge table returned all the effective data. View support was added in MySQL 5.0, so this use of Merge tables is obsolete.

2.1.3 MEMORY / HEAP


HEAP is the name of this table type before MySQL 4.1. MEMORY is the new, preferred name. This engine is introduced in version 3.23.

2.1.4 BDB
Synonyms: BDB, BerkleyDB BDB has been removed from version 5.1 and later due to lack of use. BerkeleyDB is a family of free software embeddable DBMS's developer by SleepyCat, a company which has been acquired by Oracle. SleepyCat provided a Storage Engine for MySQL called BDB. BDB supports transactions and page-level locking, but it also has many limitations within MySQL.

2.1.5 BLACKHOLE
Discards all data stored in it but does still write to the binary log, so it is useful in replication scale-out or secure binlog-do ltering situations where slaves aren't trustworthy and for benchmarking the higher layers of the server.

2.1.6 Miscellaneous
For completeness, other storage engines include: CSV: simple Comma-Separated Values engine, that uses the CSV format to store data. Used to share database with other CSV-aware applications maybe? Due to the simple nature of its format, indexing is not available. EXAMPLE (a stub for developers) ISAM (for pre-3.23 backward compatibility, removed in 5.1)

Table types

2.2 Metadata about Storage Engines


You can get metadata about ofcial MySQL Storage Engines and other Storage Engines which are present on your server, via SQL. SHOW STORAGE ENGINES Starting from MySQL 5.0, you can get information about the Storage Engine which you can use using the SHOW STORAGE ENGINES statement.
SHOW STORAGE ENGINES

The STORAGE word is optional. This command returns a dataset with the following columns: Engine - Name of the Storage Engine. Support - Wether the Storage Engine is supported or not. Possible values: 'DEFAULT' - it's supported and it's the default engine; 'YES' - supported; 'DISABLED' - it has been compiled, but MySQL has been started with that engine disabled (possibly with options like --skip-engine-name); 'NO' - not supported. Comment - Brief description of the engine. Transactions - Wether the engine supports SQL transactions. Added in MySQL 5.1. XA - Wether the engine supports XA transactions. Added in MySQL 5.1. Savepoints - Wether the engine supports savepoints and rollbacks. Added in MySQL 5.1. INFORMATION_SCHEMA ENGINES table ENGINES is a virtual table within the INFORMATION_SCHEMA database. It can be used to get information about Storage Engines. Its columns are the came which are returned by the SHOW ENGINES statement (see above). ENGINES has been added in MySQL 5.1.5. HELP statement If you want more info about an ofcial MySQL Storage Engine, you can use the HELP command:
HELP 'myisam'

If you are using the command line client, you can omit the quotes:
help myisam \g

10

Changing the Storage Engine

2.3 Changing the Storage Engine


2.3.1 SQL
When you want to create a table using a given Storage Engine, you can use the ENGINE clause in the CREATE TABLE command:
CREATE TABLE ... ENGINE=InnoDB

If the ENGINE clause is not specied, the value of the storage_engine variable will be used. By default it's MyISAM, but you can change it:
SET storage_engine=InnoDB

Or you can modify the value of default-storage-engine in the my.cnf before starting the MySQL server. You can also change the Storage Engine of an existing table:
ALTER TABLE stats ENGINE=MyISAM

2.3.2 mysql_convert_table_format
mysql_convert_table_format is a tool provided with MySQL, written in Perl. It converts all the tables contained in the specied database to another Storage Engine. The syntax is:
mysql_convert_table_format [options] database

database is the name of the database in which the program will operate. It's mandatory. Options are: --help Print a help and exit. --version Print version number and exit. --host=host The host on which MySQL is running. Default: localhost. --port=port TCP port. --user=user Specify the username. --password=password Specify the password. As it is insecure (it's visible with the coomand top, for example), you can use an option le, instead. --type=storage_engine The storage engine that the tables will use after conversion.

11

Table types --force Don't stop the execution if an error occurs. --verbose Print detailed information about the conversions. Example:
mysql_convert_table_format --host=localhost --user=root --password=xyz970 --force --type=InnoDB test

This command species access data (localhost, username, password) and converts all tables within database test into InnoDB. If some tables can't be converted, the script skips them and converts the others (--force).
1

Italic text

=Browsing the databases The following SQL commands provide information about the databases located on the current server. The INFORMATION_SCHEMA table containing this information is SCHEMATA. The mysqlshow command line tool can be used instead. You can't show databases if the server has been started with the --skip-all-databases option. If you don't have the 'SHOW DATABASES' privilege, you'll only see databases on which you have some permissions.

2.4 List databases


Show all databases:
SHOW DATABASES;

The SCHEMA keywords can be used in place of DATABASES. MySQL doesn't support standard SQL SCHEMAs, so SCHEMA is a synonym of database. It has been added for compatibility with other DBMSs.

2.4.1 Add a lter on the databases names


SHOW DATABASES LIKE 'pattern';

The LIKE operator here works as in normal SELECTs or DML statements. So you an list all databases whose name starts with 'my':
SHOW DATABASES LIKE 'my%';

http://en.wikibooks.org/wiki/Category%3A

12

List tables and views

2.4.2 Add complex lters


You can add more complex lters using the WHERE clause:
SHOW DATABASES WHERE conditions;

WHERE clause allows you to use regular expressions, '<' and '>' operators, string functions or other useful expressions to lter the records returned by SHOW DATABASES.

2.5 List tables and views


The following SQL commands provide information about the tables and views contained in a database. The INFORMATION_SCHEMA tables containing this information are TABLES and VIEWS. Since the following statements provide very little information about views, if you need to get metadata about them you'll probably prefer to query the VIEWS table. The mysqlshow command line tool can be used instead.

2.5.1 Show all tables


USE database; SHOW TABLES;

SHOW TABLES FROM database;

The 2 forms shown above are equivalent.

2.5.2 Apply a lter


You can apply a lter to the tables names, to show only tables whose name match a pattern. You can use the LIKE operators, as you do in SELECTs or in the DML statements:
SHOW TABLES LIKE pattern;

Also, you can apply a more complex lter to any column returned by the SHOW TABLES command using the WHERE clause:
SHOW TABLES WHERE condition;

(see below)

13

Table types

2.5.3 Extra info


By default, SHOW TABLES returns only one column containing the name of the table. You can get extra information by using the FULL keyword:
SHOW FULL TABLES;

This will add a column called Table_type. This can have 3 values: 'BASE TABLE' for tables, 'VIEW' for views ans 'SYSTEM VIEW' for special tables created by the server (normally used only INFORMATION_SCHEMA tables). So you can only list tables:
SHOW FULL TABLES WHERE Table_type='BASE TABLE';

Or, you can only list views:


SHOW FULL TABLES WHERE Table_type='VIEW';

2.5.4 Show only open tables


You can get a list of the non-temporary tables (not views) which are open in the cache:
SHOW OPEN TABLES;

This command has the same parameters as SHOW TABLES, except for FULL (useless in this case). You can't get this information from the INFORMATION_SCHEMA.

2.6 List elds


The following SQL commands provide information about the columns in a table or in a view. The INFORMATION_SCHEMA table containing this information is COLUMNS. The mysqlshow command line tool can be used instead.

2.6.1 DESCRIBE
DESCRIBE table; DESCRIBE database.table; DESCRIBE table 'filter';

DESC can be used as a shortcut for DESCRIBE.

14

List indexes 'lter' can be a column name. If a column name is specied, only that column will be shown. If 'lter' contains the '%' or the '_' characters, it will be evaluated as a LIKE condition. For example, you can list all elds which start with 'my':
DESC table 'my%';

2.6.2 SHOW COLUMNS


EXPLAIN table; --synonym SHOW [FULL] FIELDS FROM table; -- synonym SHOW COLUMNS FROM table; --synonym SHOW COLUMNS FROM table FROM database; SHOW COLUMNS FROM table LIKE 'pattern'; SHOW COLUMNS FROM table WHERE condition;

FIELDS and COLUMNS are synonyms. EXPLAIN is a synonym of SHOW COLUMNS / FIELDS too, but it doesn't support all of its clauses. A databases name can be specied both in the form
SHOW COLUMNS FROM table FROM database;

both:
SHOW COLUMNS FROM database.table;

2.6.3 Extra info


Using the FULL keyword, extra info can be retried: the columns' collation, privileges you have on the column and the comment.

2.7 List indexes


The following SQL commands provide information about the indexes in a table. Information about keys is contained in the COLUMNS table in the INFORMATION_SCHEMA. The mysqlshow -k command line tool can be used instead.
SHOW INDEX FROM TABLE; SHOW INDEX FROM TABLE FROM databases;

The KEYS reserved word can be used as a synonym of INDEX. No other clauses are provided.

15

Table types

2.8 INFORMATION_SCHEMA
information_schema is a virtual database provided by MySQL 5 and later, that contains metadata about the server and the databases. You can't modify structure and data of information_schema. You can only query the tables. Many information_schema tables provide the same data you can retrieve with a SHOW statement. While using SHOW commands is faster (the server responds much faster and you type less characters), the information_schema provides a more exible way to obtain and organize the metadata.

16

3 Specifying table names


In this book, we will quote the MySQL identiers (tables names, elds, databases, etc.) using backquotes (). Backquote is ASCII 96. It can be type on Linux systems by pressing: ALT+'. Most often, this is optional. However, this allows better error messages from MySQL. For example, this error is not very helpful:
mysql> SELECT user_id, group_id FROM user,group LIMIT 1; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group LIMIT 1' at line 1

But this one is better:


mysql> SELECT user_id, group_id FROM user,group LIMIT 1; ERROR 1146 (42S02): Table 'savannah.group' doesn't exist

Ok, it was just a missing s:


mysql> SELECT user_id, group_id FROM user,groups LIMIT 1; +---------+----------+ | user_id | group_id | +---------+----------+ | 100 | 2 | +---------+----------+ 1 row in set (0.02 sec)

This syntax allows the user to use reserved words and some illegal characters in objects' names. It is even possible to use backquotes by typing it twice:
RENAME TABLE user TO

However, this is not a portable syntax. The SQL standard recommends the use of a double quote ("). If you want to write portable SQL quote, do not quote the identiers. But is there something like portable SQL, even remotely?

17

4 Denitions
DDL (Data Denition Language) refers to the CREATE, ALTER and DROP statements DDL allows to add / modify / delete the logical structures which contain the data or which allow users to access / mantain the data (databases, tables, keys, views...). DDL is about "metadata". DML (Data Manipulation Language) refers to the INSERT, UPDATE and DELETE statements DML allows to add / modify / delete data itself. DQL (Data Query Language) refers to the SELECT, SHOW and HELP statements (queries) SELECT is the main DQL instruction. It retrieves data you need. SHOW retrieves infos about the metadata. HELP... is for people who need help. DCL (Data Control Language) refers to the GRANT and REVOKE statements DCL is used to grant / revoke permissions on databases and their contents. DCL is simple, but MySQL's permissions are rather complex. DCL is about security.

19

5 User Variables
5.1 Session Variables
The ability to set variables in a statement with the := assignment operator: For e.g. (@total) to calculate the total in an example, you have to have the total column rst because it must be calculated before the individual percentage calculations Session variables are set for the duration of the thread. In the vast majority of cases you'd use a programming language to do this sort of thing. Mysql variables can be useful when working on the Mysql command line. If no records are returned, the user variable will not be set for that statement. A user variable set in the eld list cannot be used as a condition. The value of a variable is set with the SET statement or in a SELECT statement with :=
select @test := 2; select @test + 1 set @startdate='some_start_date', @enddate='some_end_date' SELECT @toremember:=count(*) FROM membros; select @numzero := count(*) from table1 where field=0; select @numdistinct := count(distinct field) from table1 where field <> 0 ; select @numzero @numdistinct;

You can copy values retrieved by a SELECT into one or more variables:
SELECT INTO

5.2 Global Variables


A global variable is visible to all users, it allows to modify the conguration les settings during the session or denitely. So when changing them, it's necessary to precise this permanent or ephemera criteria, with respectively set global and set session. Example:
mysql> set @@global.max_connections = 1000; mysql> show global variables like 'wait_timeout'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | wait_timeout | 60 | +---------------+-------+ 1 row in set (0.00 sec) mysql> set @@session.wait_timeout=120;

21

6 Alias
An expression and a column may be given aliases using AS. The alias is used as the expression's column name and can be used with order by or having clauses. For e.g.
SELECT CONCAT(last_name,' ', first_name) AS full_name, nickname AS nick FROM mytable ORDER BY full_name

These aliases can be used in ORDER BY, GROUP BY and HAVING clauses. They should not be used in WHERE clause. A table name can have a shorter name for reference using AS. You can omit the AS word and still use aliasing. For e.g.
SELECT COUNT(B.Booking_ID), U.User_Location FROM Users U LEFT OUTER JOIN Bookings AS B ON U.User_ID = B.Rep_ID AND B.Project_ID = '10' GROUP BY (U.User_Location)

Aliasing plays a crucial role while you are using self joins. For e.g. people table has been referred to as p and c aliases!
SELECT p.name AS parent, c.name AS child, MIN((TO_DAYS(NOW())-TO_DAYS(c.dob))/365) AS minage FROM people AS p LEFT JOIN people AS c ON p.name=c.parent WHERE c.name IS NOT NULL GROUP BY parent HAVING minage > 50 ORDER BY p.dob;

23

7 Queries
7.1 SELECT
select syntax is as follows:
SELECT * FROM table WHERE condition GROUP BY grouping field HAVING group condition ORDER BY order LIMIT limit, offset

7.1.1 List of elds


You must specify what data you're going to retrieve in the SELECT clause:
SELECT DATABASE() -- returns the current db's name SELECT CURRENT_USER() -- returns your username SELECT 1+1 -- returns 2

Any SQL expression is allowed here. You can also retrieve all elds from a table:
SELECT * FROM stats

If you SELECT only the necessary elds, the query will be faster.

7.1.2 The table's name


If you are retrieving results from a table or a view, usually you specify the table's name in the FROM clause:
SELECT id FROM stats -- retrieve a field called id from a table called stats

Or:

25

Queries

SELECT MAX(id) FROM stats SELECT id*2 FROM stats

You can also use the db_name.table_name syntax:


SELECT id FROM sitedb.stats

But you can also specify the table's name in the SELECT clause:
SELECT stats.id -- retrieve a field called id from a table SELECT sitedb.stats.id

7.1.3 WHERE
You can set a lter to decide what records must be retrieved. For example, you can retrieve only the record which has an id of 42:
SELECT * FROM stats WHERE id=42

Or you can read more than one record:


SELECT * FROM antiques WHERE buyerid IS NOT NULL

7.1.4 GROUP BY
You can group all records by one or more elds. The record which have the same value for that eld will be grouped in one computed record. You can only select the grouped record and the result of some aggregate functions, which will be computed on all records of each group. For example, the following will group all records in the table users by the eld city. For each group of users living in the same city, the maximum age, the minimum age and the average age will be returned:
SELECT city, MAX(age), MIN(age), AVG(age) GROUP BY city

In the following example, the users are grouped by city and sex, so that we'll know the max, min and avg age of male/female users in each city:
SELECT city, sex, MAX(age), MIN(age), AVG(age) GROUP BY city, sex

26

SELECT

7.1.5 HAVING
The HAVING clause declares a lter for the records which are computed by the GROUP BY clause. It's different from the WHERE clause, that operates before the GROUP BY. Here's what happens: 1. The records which match to the WHERE clause are retrieved 2. Those records are used to compute new records as dened in the GROUP BY clause 3. The new records that match to the HAVING conditions are returned This means which WHERE decides what record are used to compose the new computed records. HAVING decides what computed records are returned, so it can operate on the results of aggregate functions. HAVING is not optimized and can't use indexes. Incorrect use of HAVING:
SELECT city, sex, MAX(age), MIN(age), AVG(age) GROUP BY city HAVING sex='m'

This probably gives a wrong results. MAX(age) and other aggregate calculations are made using all values, even if the record's sex value is 'f'. This is hardly the expected result. Incorrect use of HAVING:
SELECT city, sex, MAX(age), MIN(age), AVG(age) GROUP BY city, sex HAVING sex='m'

This is correct and returns the expected results, but the execution of this query is not optimized. The WHERE clause can and should be used, because, so that MySQL doesn't computes records which are excluded later. Correct use of HAVING:
SELECT city, sex, MAX(age), MIN(age), AVG(age) GROUP BY city HAVING MAX(age) > 80

It must group all records, because can't decide the max age of each city before the GROUP BY clause is execute. Later, it returns only the record with a MAX(age)>80.

7.1.6 ORDER BY
You can set an arbitrary order for the records you retrieve. The order may be alphabetical or numeric.
SELECT * FROM stats ORDER BY id

By default, the order is ASCENDING. You can also specify that the order must be DESCENDING:

27

Queries

SELECT * FROM stats ORDER BY id ASC -- default SELECT * FROM stats ORDER BY id DESC -- inverted

NULLs values are considered as minor than any other value. You can also specify the eld position, in place of the eld name:
SELECT name, buyerid FROM antiques ORDER BY 1 -- name SELECT name, buyerid FROM antiques ORDER BY 2 -- buyerid SELECT name, buyerid FROM antiques ORDER BY 1 DESC

SQL expressions are allowed:


SELECT name FROM antiques ORDER BY REVERSE(name)

You can retrieve records in a random order:


SELECT name FROM antiques ORDER BY RAND()

If a GROUP BY clause is specied, the results are ordered by the elds named in GROUP BY, unless an ORDER BY clause is present. You can even specify in the GROUP BY clause if the order must be ascending or descending:
SELECT city, sex, MAX(age) GROUP BY city ASC, sex DESC

If you have a GROUP BY but you don't want the records to be ordered, you can use ORDER BY NULL:
SELECT city, sex, MAX(age) GROUP BY city, sex ORDER BY NULL

7.1.7 LIMIT
You can specify the maximum of rows that you want to read:
SELECT * FROM antiques ORDER BY id LIMIT 10

This statement returns a maximum of 10 rows. If there are less than 10 rows, it returns the number of rows found. The limit clause is usually used with ORDER BY. You can get a given number of random records:

28

SELECT

SELECT * FROM antiques ORDER BY rand() LIMIT 1 -- one random record SELECT * FROM antiques ORDER BY rand() LIMIT 3

You can specify how many rows should be skipped before starting to return the records found. The rst record is 0, not one:
SELECT * FROM antiques ORDER BY id LIMIT 10 SELECT * FROM antiques ORDER BY id LIMIT 0, 10 -- synonym

You can use the LIMIT clause to get the pagination of results:
SELECT * FROM antiques ORDER BY id LIMIT 0, 10 -- first page SELECT * FROM antiques ORDER BY id LIMIT 10, 10 -- second page SELECT * FROM antiques ORDER BY id LIMIT 20, 10 -- third page

Also, the following syntax is acceptable:


SELECT * FROM antiques ORDER BY id LIMIT 10 OFFSET 10

You can use the LIMIT clause to check the syntax of a query without waiting for it to return the results:
SELECT ... LIMIT 0

Optimization tips: SQL_CALC_FOUND_ROWS may speed up a query 12 LIMIT is particularly useful for SELECTs which use ORDER BY, DISTINCT and GROUP BY, because their calculations don't have to involve all the rows. If the query is resolved by the server copying internally the results into a temporary table, LIMIT helps MySQL to calculate how much memory is required by the table.

7.1.8 DISTINCT
The DISTINCT keyword can be used to remove all duplicate rows from the resultset:
SELECT DISTINCT * FROM stats -- no duplicate rows SELECT DISTINCTROW * FROM stats -- synonym SELECT ALL * FROM stats -- duplicate rows returned (default)

You can use it to get the list of all values contained in one eld:

1 2

http://www.mysqlperformanceblog.com/2007/08/28/to-sql_calc_found_ rows-or-not-to-sql_calc_found_rows/ http://dev.mysql.com/doc/refman/5.0/en/information-functions.html

29

Queries

SELECT DISTINCT type FROM antiques ORDER BY type

Or you can use it to get the existing combinations of some values:


SELECT DISTINCT type, age FROM antiques ORDER BY type

If one of the elds you are SELECTing is the PRIMARY KEY or has a UNIQUE index, DISTINCT is useless. Also, it's useless to use DISTINCT in conjunction with the GROUP BY clause.

7.1.9 IN and NOT IN


SELECT id FROM stats WHERE position IN ('Manager', 'Staff')

SELECT ownerid, 'is in both orders & antiques' FROM orders, antiques WHERE ownerid = buyerid UNION SELECT buyerid, 'is in antiques only' FROM antiques WHERE buyerid NOT IN (SELECT ownerid FROM orders)

7.1.10 EXISTS and ALL


(Compatible: Mysql 4+)
SELECT ownerfirstname, ownerlastname FROM owner WHERE EXISTS (SELECT * FROM antiques WHERE item = 'chair')

SELECT buyerid, item FROM antiques WHERE price = ALL (SELECT price FROM antiques)

7.1.11 Optimization hints


There are some hints you may want to give to the server to better optimize the SELECTs. If you give more than one hints, the order of the keywords is important:
SELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [STRAIGHT_JOIN] [SQL_SMALL_RESULT | SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] ...

HIGH_PRIORITY

30

SELECT Usually, DML commands (INSERT, DELETE, UPDATE) have higher priority than SELECTs. If you specify HIGH_PRIORITY though, the SELECT will have higher priority than DML statements. STRAIGHT_JOIN Force MySQL to evaluate the tables of a JOIN in the same order they are named, from the leftmost. SQL_SMALL_RESULT It's useful only while using DISTINCT or GROUP BY. Tells the optimizer that the query will return few rows. SQL_BIG_RESULT It's useful only while using DISTINCT or GROUP BY. Tells the optimizer that the query will return a many rows. SQL_BUFFER_RESULT Force MySQL to copy the result into a temporary table. This is useful to remove LOCKs as soon as possible. SQL_CACHE Forces MySQL to copy the result into the query cache. Only works if the value of query_cache_type is DEMAND or 2. SQL_NO_CACHE Tells MySQL not to cache the result. Useful if the query occurs very seldom or if the result often change. SQL_CALC_FOUND_ROWS Useful if you are using the LIMIT clause. Tells the server to calculate how many rows would have been returned if there were no LIMIT. You can retrieve that number with another query:
SELECT SQL_CALC_FOUND_ROWS * FROM stats LIMIT 10 OFFSET 100; SELECT FOUND_ROWS();

7.1.12 UNION and UNION All


(Compatible: Mysql 4+) Following query will return all the records from both tables.
SELECT * FROM english UNION ALL SELECT * FROM hindi

UNION is the same as UNION DISTINCT. If you type only UNION, then it is considered that you are asking for distinct records. If you want all records, you have to use UNION ALL.
SELECT word FROM word_table WHERE id = 1 UNION SELECT word FROM word_table WHERE id = 2

(SELECT magazine FROM pages) UNION DISTINCT

31

Queries

(SELECT magazine FROM pdflog) ORDER BY magazine

(SELECT ID_ENTRY FROM table WHERE ID_AGE = 1) UNION DISTINCT (SELECT ID_ENTRY FROM table WHERE ID_AGE=2)

7.2 Joins
The Most important aspect of SQL is its relational features. You can query, compare and calculate two different tables having entirely different structure. Joins and subselects are the two methods to join tables. Both methods of joining tables should give the same results. The natural join is faster on most SQL platforms. In the following example a student is trying to learn what the numbers are called in hindi.
CREATE TABLE english (Tag int, Inenglish varchar(255)); CREATE TABLE hindi (Tag int, Inhindi varchar(255));

INSERT INTO english (Tag, Inenglish) VALUES (1, 'One'); INSERT INTO english (Tag, Inenglish) VALUES (2, 'Two'); INSERT INTO english (Tag, Inenglish) VALUES (3, 'Three');

INSERT INTO hindi (Tag, Inhindi) VALUES (2, 'Do'); INSERT INTO hindi (Tag, Inhindi) VALUES (3, 'Teen'); INSERT INTO hindi (Tag, Inhindi) VALUES (4, 'Char');

Tag 1 2 3

select * from english Inenglish One Two Three

Tag 2 3 4

select * from hindi Inhindi Do Teen Char

7.2.1 Cartesian join (CROSS JOIN)


A Cartesian join is when you join every row of one table to every row of another table.
SELECT * FROM english, hindi

It is also called Cross Join and may be written in this way:


SELECT * FROM english CROSS JOIN hindi

Tag

Inenglish

Tag

Inhindi

32

Joins 1 2 3 1 2 3 1 2 3 One Two Three One Two Three One Two Three 2 2 2 3 3 3 4 4 4 Do Do Do Teen Teen Teen Char Char Char

7.2.2 Inner Join


SELECT hindi.Tag, english.Inenglish, hindi.Inhindi FROM english, hindi WHERE english.Tag = hindi.Tag

Tag 2 3

Inenglish Two Three

Inhindi Do Teen

You can also write the same query as


SELECT hindi.Tag, english.Inenglish, hindi.Inhindi FROM english INNER JOIN hindi ON english.Tag = hindi.Tag

Natural Joins using "using" (Compatible: MySQL 4+; but changed in MySQL 5) The following statement using "USING" method will display the same results.
SELECT hindi.tag, hindi.Inhindi, english.Inenglish FROM hindi NATURAL JOIN english USING (Tag)

7.2.3 Outer Joins


Tag 1 2 3 Inenglish One Two Three Tag 2 3 4 Inhindi Do Teen Char

33

Queries

7.2.4 LEFT JOIN / LEFT OUTER JOIN


The syntax is as follows:
SELECT field1, field2 FROM table1 LEFT JOIN table2 ON field1=field2

SELECT e.Inenglish as English, e.Tag, '--no row--' as Hindi FROM english AS e LEFT JOIN hindi AS h ON e.Tag=h.Tag WHERE h.Inhindi IS NULL

English One

tag 1

Hindi --no row-

7.2.5 Right Outer Join


SELECT '--no row--' AS English, h.tag, h.Inhindi AS Hindi FROM english AS e RIGHT JOIN hindi AS h ON e.Tag=h.Tag WHERE e.Inenglish IS NULL

English tag Hindi --no row-- 4 Char Make sure that you have the same name and same data type in both tables. The keywords LEFT and RIGHT are not absolute, they only operate within the context of the given statement: we can reverse the order of the tables and reverse the keywords, and the result would be the same. If the type of join is not specied as inner or outer then it will be executed as an INNER JOIN.

7.2.6 Full Outer Join


As for v5.1, MySQL does not provide FULL OUTER JOIN. You may emulate it this way:
(SELECT a.*, b* FROM tab1 a LEFT JOIN tab2 b ON a.id = b.id) UNION (SELECT a.*, b* FROM tab1 a RIGHT JOIN tab2 b ON a.id = b.id)

7.2.7 Multiple joins


It is possible to join more than just two tables:
SELECT ... FROM a JOIN (b JOIN c on b.id=c.id) ON a.id=b.id

34

Subqueries Here is an example from Savane:


mysql> SELECT group_type.type_id, group_type.name, COUNT(people_job.job_id) AS count FROM group_type JOIN (groups JOIN people_job ON groups.group_id = people_job.group_id) ON group_type.type_id = groups.type GROUP BY type_id ORDER BY type_id +---------+--------------------------------------+-------+ | type_id | name | count | +---------+--------------------------------------+-------+ | 1 | Official GNU software | 148 | | 2 | non-GNU software and documentation | 268 | | 3 | www.gnu.org portion | 4 | | 6 | www.gnu.org translation team | 5 | +---------+--------------------------------------+-------+ 4 rows in set (0.02 sec)

7.3 Subqueries
(Compatible: Mysql 4.1 and later...Bold text) SQL subqueries let you use the results of one query as part of another query. Subqueries are often natural ways of writing a statement. Let you break a query into pieces and assemble it. Allow some queries that otherwise can't be constructed. Without using a subquery, you have to do it in two steps. Subqueries always appear as part of the WHERE (or HAVING) clause. Only one eld can be in the subquery SELECT. It means Subquery can only produce a single column of data as its result. ORDER BY is not allowed; it would not make sense. Usually refer to name of a main table column in the subquery. This denes the current row of the main table for which the subquery is being run. This is called an outer reference.

For e.g. If RepOfce= OfceNbr from Ofces table, list the ofces where the sales quota for the ofce exceeds the sum of individual salespersons' quotas
SELECT City FROM Offices WHERE Target > ???

??? is the sum of the quotas of the salespeople, i.e.


SELECT SUM(Quota) FROM SalesReps WHERE RepOffice = OfficeNbr

We combine these to get

35

Queries

SELECT City FROM Offices WHERE Target > (SELECT SUM(Quota) FROM SalesReps WHERE RepOffice = OfficeNbr)

Display all customers with orders or credit limits > $50,000. Use the DISTINCT word to list the customer just once.
SELECT DISTINCT CustNbr FROM Customers, Orders WHERE CustNbr = Cust AND (CreditLimit>50000 OR Amt>50000);

7.4 References 7.5 Resources


Ofcial MySQL documentation3

http://dev.mysql.com/doc

36

8 Data manipulation
8.1 INSERT
The syntax is as follows: Insert value1 into Column1, value2 into Column2, and value3 into Column3:
INSERT INTO TableName (Column1, Column2, Column3) VALUES (value1, value2, value3)

Insert one record (values are inserted in the order that the columns appear in the database):
INSERT INTO TableName VALUES (value1, value2, value3)

Insert two records:


INSERT INTO TableName VALUES (value1, value2, value3), (value4, value5, value6)

INSERT INTO antiques VALUES (21, 01, 'Ottoman', 200.00); INSERT INTO antiques (buyerid, sellerid, item) VALUES (01, 21, 'Ottoman');

You can also insert records 'selected' from other table.


INSERT INTO table1(field1, field2) SELECT field1, field2 FROM table2

INSERT INTO World_Events SELECT * FROM National_Events

Performance tips: To insert many rows, consider using LOAD DATA INFILE instead. If bulk INSERTs are too slow and they operate on indexed non-empty tables, maybe you should increase the value of bulk_insert_buffer_size. Before performing bulk inserts, you may want to disable thekeys. LOCKing a table also speeds up the INSERT.

37

Data manipulation

8.2 UPDATE
The syntax is:
UPDATE table SET field = newvalue WHERE criteria ORDER BY field LIMIT n

Examples are:
UPDATE owner SET ownerfirstname = 'John' WHERE ownerid = (SELECT buyerid FROM antiques WHERE item = 'Bookcase'); UPDATE antiques SET price = 500.00 WHERE item = 'Chair'; UPDATE order SET discount=discount * 1.05 UPDATE tbl1 JOIN tbl2 ON tbl1.ID = tbl2.ID SET tbl1.col1 = tbl1.col1 + 1 WHERE tbl2.status='Active' UPDATE tbl SET names = REPLACE(names, 'aaa', 'zzz') UPDATE products_categories AS pc INNER JOIN products AS p ON pc.prod_id = p.id SET pc.prod_sequential_id = p.sequential_id UPDATE table_name SET col_name = REPLACE(col_name, 'host.domain.com', 'host2.domain.com')

UPDATE posts SET deleted=True ORDER BY date LIMIT 1

With ORDER BY you can order the rows before updating them, and only update a given number of rows (LIMIT). It is currently not possible to update a table while performing a subquery on the same table. For example, if I want to reset a password I forgot in SPIP:
mysql> UPDATE spip_auteurs SET pass = (SELECT pass FROM spip_auteurs WHERE login='paul') where login='admin'; ERROR 1093 (HY000): You can't specify target table 'spip_auteurs' for update in FROM clause

TODO: http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-targetdescribes a work-around that I couldn't make to work with MySQL 4.1. Currently the work-around is not use 2 subqueries, possibly with transactions. Performance tips UPDATEs speed depends of how many indexes are updated.

38

REPLACE If you UPDATE a MyISAM table which uses dynamic format, if you make rows larger they could be splitted in more than one part. This causes reading overhead. So, if your applications often do this, you may want to regularly run an OPTIMIZE TABLE statement. Performing many UPDATEs all together on a LOCKed table is faster than performing them individually.

8.3 REPLACE
REPLACE works exactly like INSERT, except that if an old record in the table has the same value as a new record for a PRIMARY KEY or a UNIQUE index, the old record is deleted before the new record is inserted. With IGNORE, invalid values are adjusted to the closest values and inserted; warnings are produced but the statement does not abort. Prior to MySQL 4.0.1, INSERT ... SELECT implicitly operates in IGNORE mode. As of MySQL 4.0.1, specify IGNORE explicitly to ignore records that would cause duplicate-key violations.

8.4 DELETE and TRUNCATE


DELETE [QUICK] FROM table1 TRUNCATE [TABLE] table1

If you don't use a WHERE clause with DELETE, all records will be deleted. It can be very slow in a large table, especially if the table has many indexes. If the table has many indexes, you can make the cache larger to try making the DELETE faster (key_buffer_size variable). For indexed MyISAM tables, in some cases DELETEs are faster if you specify the QUICK keyword (DELETE QUICK FROM ...). This is only useful for tables where DELETEd index values will be reused. TRUNCATE will delete all rows quickly by DROPping and reCREATE-ing the table (not all Storage Engines support this operation). TRUNCATE is not transaction-safe nor lock-safe. DELETE informs you how many rows have been removed, but TRUNCATE doesn't. After DELETing many rows (about 30%), an OPTIMIZE TABLE command should make next statements faster. For a InnoDB table with FOREIGN KEYs constraints, TRUNCATE behaves like DELETE.
DELETE FROM antiques WHERE item = 'Ottoman' ORDER BY id LIMIT 1

You can order the rows before deleting them, and then delete only a given number of rows.

39

9 Table manipulation
9.1 CREATE TABLE
Create table syntax is: Create table tablename (FieldName1 DataType, FieldName2 DataType) The rows returned by the "select" query can be saved as a new table. The datatype will be the same as the old table. For e.g. CREATE TABLE LearnHindi select english.tag, english.Inenglish as english, hindi.Inhindi as hindi FROM english, hindi WHERE english.tag = hindi.tag

9.2 ALTER TABLE


ALTER TABLE command can be used when you want to add / delete /modify the columns and / or the indexes; or, it can be used to change other table properties. Add a column:
ALTER TABLE awards ADD COLUMN AwardCode int(2)

Modify a column:
ALTER TABLE awards CHANGE COLUMN AwardCode VARCHAR(2) NOT NULL

ALTER TABLE awards MODIFY COLUMN AwardCode VARCHAR(2) NOT NULL

Drop a column:

41

Table manipulation

ALTER TABLE awards DROP COLUMN AwardCode

Re-order the record in a table:


ALTER TABLE awards ORDER BY id

(this operation is only supported by some Storage Engines; it could make some query faster)

9.3 Renaming a table


In order to rename a table, you must have ALTER and DROP privileges on the old table name (or on all the tables), and CREATE and INSERT privileges on the new table name (or on all the tables). You can use ALTER TABLE to rename a table:
RENAME TABLE old_name TO new_name

You can rename more than one table with a single command:
RENAME TABLE old1 TO new1, old2 TO new2, ...

RENAME is a shortcut. You can also use the ALTER TABLE statement:
ALTER TABLE old RENAME new

Using ALTER TABLE you can only rename one table per statement, but it's the only way to rename temporary tables.

9.4 DROP TABLE


DROP TABLE awards

Will completely delete the table and all the records it contains. You can also drop more than one table with a single statement:
DROP TABLE table1, table2, ...

There are come optional keywords:

42

DROP TABLE

DROP TEMPORARY TABLE table; DROP TABLE table IF EXISTS;

TEMPORARY must be specied, to drop a temporary table. IF EXISTS tells the server that it must not raise an error if the table doesn't exist. =Using/Dealing with NULL Null is a special logical value in SQL. Most programming languages have 2 values of logic: True and False. SQL also has NULL which means "Unknown". A NULL value can be set. NULL is a non-value, so it can be assigned to TEXT columns, INTEGER columns or any other datatype. A column can not contain NULLs only if it has been declared as NOT NULL (see ALTER TABLE).
INSERT into Singer (F_Name, L_Name, Birth_place, Language) values ("", "Homer", NULL, "Greek"), ("", "Sting", NULL, "English"), ("Jonny", "Five", NULL, "Binary");

Do not quote the NULL. If you quote a Null then you name the person NULL. For some strange reason, NULLs do not show visually on windows XP in Varchar elds but they do in Fedora's version, so versions of mysql can give different outputs. Here we set the value of Sting and Homer's rst name to a zero length string "", because we KNOW they have NO rst name, but we KNOW we do not know the place they were born. To check for a NULLs use

SELECT * from Singer WHERE Birth_place IS NULL; or SELECT * from Singer WHERE Birth_place IS NOT NULL; or SELECT * from Singer WHERE isNull(Birth_place)

Remember, COUNT never counts NULLS.


select count(Birth_place) from Singer; 0 and sum(NULL) gives a NULL answer.

Normal operations (comparisons, expressions...) return NULL if at least one of the compared items is NULL:
SELECT (NULL=NULL) OR (NULL<>NULL) OR (NOT NULL) OR (1<NULL) OR (1>NULL) OR (1 + NULL) OR (1 LIKE NULL)

because all the expressions between in parenthesis return NULL. It's denitely logical: if you don't know the value represented by NULL, you don't know is it's =1 or <>1. Be aware that even (NULL=NULL and (NOT NULL) return NULL.

43

Table manipulation

9.5 Dealing with NULL


The function 'COALESCE' can simplify working with null values. for example, to avoid showing null values by treating null as zero, you can type:
SELECT COALESCE(colname,0) from table where COALESCE(colname,0) > 1;

In a date eld, to treat NULL as the current date:


ORDER BY (COALESCE(TO_DAYS(date),TO_DAYS(CURDATE()))-TO_DAYS(CURDATE())) EXP(SUM(LOG(COALESCE(''*the field you want to multiply*'',1)))

The coalesce() function is there to guard against trying to calculate the logarithm of a null value and may be optional depending on your circumstances.
SELECT t4.gene_name, COALESCE(g2d.score,0), COALESCE(dgp.score,0), COALESCE(pocus.score,0) FROM t4 LEFT JOIN g2d ON t4.gene_name=g2d.gene_name LEFT JOIN dgp ON t4.gene_name=dgp.gene_name LEFT JOIN pocus ON t4.gene_name=pocus.gene_name;

Use of IFNULL() in your SELECT statement is to make the NULL any value you wish.
IFNULL(expr1,expr2)

If expr1 is not NULL, IFNULL() returns expr1, else it returns expr2. IFNULL() returns a numeric or string value, depending on the context in which it is used:
mysql> SELECT -> 1 mysql> SELECT -> 10 mysql> SELECT -> 10 mysql> SELECT -> 'yes' IFNULL(1,0); IFNULL(NULL,10); IFNULL(1/0,10); IFNULL(1/0,'yes');

Null handling can be very counter intuitive and could cause problems if you have an incorrect function in a delete statement that returns null. For example the following query will delete all entries.
DELETE FROM my_table WHERE field > NULL (or function returning NULL)

If you want to have NULL values presented last when doing an ORDER BY, try this:
SELECT * FROM my_table ORDER BY ISNULL(field), field [ ASC | DESC ]

44

10 Reserved Words
Difcult Column Names, Like DATE -- use backtick. If using "date" as a column name, enclose it in backticks as follows:
CREATE TABLE IF NOT EXISTS stocks ( pkey int NOT NULL auto_increment, date date, ticker varchar(5), open decimal (9,2), high decimal (9,2), low decimal (9,2), close decimal (9,2), volume int, timeEnter timestamp(14), PRIMARY KEY (pkey) );

fr:MySQL/Mots_rservs1

http://fr.wikibooks.org/wiki/MySQL%2FMots_r%E9serv%E9s

45

11 Data Types
11.0.1 VARCHAR
VARCHAR is shorthand for CHARACTER VARYING. 'n' represents the maximum column length (upto 65,535 characters). A VARCHAR(10) column can hold a string with a maximum length of 10 characters. The actual storage required is the length of the string (L), plus 1 or 2 bytes (1 if the length is < 255) to record the length of the string. For the string 'abcd', L is 4 and the storage requirement is 5 bytes. CHAR(n) is similar to varchar(n) with the only difference that char will occupy xed length of space in the database whereas varchar will need the space to store the actual text.

11.1 TEXT and BLOB


A BLOB or TEXT column with a maximum length of 65,535 characters. The required space is the real length of the stored data plus 2 bytes (1 byte if length is < 255). The BLOB / TEXT data is not stored in the table's datale. This makes all operations (INSERT / UPDATE / DELETE / SELECT) involving the BLOB / TEXT data slower, but makes all other operations faster.

11.2 integer
Specifying an n value has no effect whatsoever. Regardless of a supplied value for n, maximum (unsigned) value stored is 429 crores1 . If you want to add negative numbers, add the "signed" keyword next to it.

11.3 decimal
decimal(n,m) decimal(4,2) means numbers upto 99.99 (and NOT 9999.99 as you may expect) can be saved. Four digits with the last 2 reserved for decimal.

http://en.wikipedia.org/wiki/Crore

47

Data Types

11.4 Dates
Out of the three types DATETIME, DATE, and TIMESTAMP, the DATE type is used when you need only a date value, without a time part. MySQL retrieves and displays DATE values in 'YYYY-MMDD' format. The DATETIME type is used when you need values that contain both date and time information. The difference between DATETIME and TIMESTAMP is that the TIMESTAMP range is limited to 1970-2037 (see below). TIME can be used to only store the time of day (HH:MM:SS), without the date. It can also be used to represent a time interval (for example: -02:00:00 for "two hours in the past"). Range: '-838:59:59' => '838:59:59'. YEAR can be used to store the year number only. If you manipulate dates, you have to specify the actual date, not only the time - that is, MySQL will not automagically use today as the current date. On the contrary, MySQL will even interpret the HH:MM:SS time as a YY:MM:DD value, which will probably be invalid. The following examples show the precise date range for Unix-based timestamps, which starts at the Unix Epoch and stops just before the rst new year before the 231 1 usual limit (2038).
mysql> SET time_zone = '+00:00'; -- GMT Query OK, 0 rows affected (0.00 sec)

mysql> SELECT FROM_UNIXTIME(-1); +-------------------+ | FROM_UNIXTIME(-1) | +-------------------+ | NULL | +-------------------+ 1 row in set (0.00 sec) mysql> SELECT FROM_UNIXTIME(0); -- "Epoch" +---------------------+ | FROM_UNIXTIME(0) | +---------------------+ | 1970-01-01 00:00:00 | +---------------------+ 1 row in set (0.00 sec) mysql> SELECT FROM_UNIXTIME(2145916799); +---------------------------+ | FROM_UNIXTIME(2145916799) | +---------------------------+ | 2037-12-31 23:59:59 | +---------------------------+ 1 row in set (0.00 sec) mysql> SELECT FROM_UNIXTIME(2145916800); +---------------------------+ | FROM_UNIXTIME(2145916800) | +---------------------------+ | NULL | +---------------------------+ 1 row in set (0.00 sec)

48

set and enum

11.5 set and enum


A SET datatype can hold any number of strings from a predened list of strings specied during table creation. The SET datatype is similar to the ENUM datatype in that they both work with predened sets of strings, but where the ENUM datatype restricts you to a single member of the set of predened strings, the SET datatype allows you to store any of the values together, from none to all of them.

49

12 Operators
MySQL uses some standard SQL operators and some non-standard operators. They can be used to write expressions which involve constant values, variables, values contained in elds and / or other expressions.

12.1 Precedence
12.1.1 Operator precedence
Table of operator precedence:
INTERVAL BINARY, COLLATE ! - (unary minus), (unary bit inversion) *, /, DIV, %, MOD -, + <<, >> & | =, <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN BETWEEN, CASE, WHEN, THEN, ELSE NOT &&, AND XOR ||, OR :=

Modiers: PIPES_AS_CONCAT - If this SQL mode is enabled, || has precedence on , but - and have precedence on ||. HIGH_NOT_PRECEDENCE - If this SQL mode is enabled, NOT has the same precedence level as !.

12.1.2 Use of parenthesis


You can use parenthesis to force MySQL to evaluate a subexpression before another indipendently from operator precedence:
SELECT (1 + 1) * 5 -- returns 10

51

Operators You can also use parenthesis to make an expression more readable by humans, even if they don't affect the precedence:
SELECT 1 + (2 * 5) -- the same as 1 + 2 * 5

12.2 Assignment operators


You can use the = operator to assign a value to a column:
UPDATE myTable SET uselessField=0

When you want to assign a value to a variable, you must use the := operator, because the use of = would be ambigous (is it as assignment or a comparison?)
SELECT @myvar := 1

You can also use SELECT INTO to assign values to one or more variables.

12.3 Comparison operators


12.3.1 Equality
If you want to check if 2 values are equal, you must use the = operator:
SELECT True = True -- returns 1 SELECT True = False -- returns 0

If you want to check if 2 values are different, you can use the <> or != operators, which have the same meaning:
SELECT True <> False -- returns 1 SELECT True != True -- returns 0

<> return 1 where = returns 0 and vice versa.

12.3.2 IS and NULL-safe comparison


When you compare a NULL value with a non-NULL value, you'll get NULL. If you want to check if a value is null, you can use IS:

52

Comparison operators

SELECT (NULL IS NULL) -- returns 1 SELECT (1 IS NULL) -- returns 0 SELECT (True IS True) -- returns an error!

You can check if a value is non-NULL:


SELECT (True IS NOT NULL) -- returns 1

There is also an equality operator which considers NULL as a normal value, so it returns 1 (not NULL) if both values are NULL and returns 0 (not NULL) if one of the values is NULL:
SELECT col1 <=> col2 FROM myTable

There is not a NULL-safe non-equality operator, but you can type the following:
SELECT NOT (col1 <=> col2) FROM myTable

12.3.3 IS and Boolean comparisons


IS and IS NOT can also be used for Boolean comparisons. You can use them with the reserved words TRUE, FALSE and UNKNOWN (which is merely a synonym for NULL).
SELECT SELECT SELECT SELECT SELECT SELECT 1 IS TRUE -- returns 1 1 IS NOT TRUE -- returns 0 1 IS FALSE -- returns 0 (NULL IS NOT FALSE) -- returns 1: unkown is not false (NULL IS UNKOWN) -- returns 1 (NULL IS NOT UNKNOWN) -- returns 0

12.3.4 Greater, Less...


You can check if a value is greater than another value:
SELECT 100 > 0 -- returns 1 SELECT 4 > 5 -- return 0

You can also check if a value is minor than another value:


SELECT 1 < 2 -- returns 1 SELECT 2 < 2 -- returns 0

This kind of comparisons also works on TEXT values:

53

Operators

SELECT 'a' < 'b' -- returns 1

Generally speaking, alphabetical order is used for TEXT comparisons. However, the exact rules are dened by the COLLATION used. A COLLATION denes the sorting rules for a given CHARACTER SET. For example, a COLLATION may be case-sensitive, while another COLLATION may be case-insensitive. You can check if a value is equal or greater than another value. For example, the following queries have the same meaning:
SELECT a >= b FROM myTable SELECT NOT (a < b) FROM myTable

Similarly, you can check if a value is less or equal to another value:


SELECT a <= b FROM myTable

12.3.5 BETWEEN
If you want to check if a value is included in a given range, you can use the BETWEEN ... AND ... operator. AND doesn't have its usual meaning. Example:
SELECT 20 BETWEEN 10 AND 100 -- returns 1

The value after BETWEEN and the value after AND are included in the range. You can also use NOT BETWEEN to check if a value is not included in a range:
SELECT 8 NOT BETWEEN 5 AND 10 -- returns 0

12.3.6 IN
You can use the IN operator to check if a value is included in a list of values:
SELECT 5 IN (5, 6, 7) -- returns 1 SELECT 1 IN (5, 6, 7) -- returns 0

You should not include in the list both numbers and strings, or the results may be unpredictable. If you have numbers, you should quote them:
SELECT 4 IN ('a', 'z', '5')

54

Logical operators There is not a theoretical limit to the number of values included in the IN operator. You can also use NOT IN:
SELECT 1 NOT IN (1, 2, 3) -- returns 0

12.4 Logical operators


12.4.1 MySQL Boolean logic
MySQL doesn't have a real BOOLEAN datatype. FALSE is a synonym for 0. Empty strings are considered as FALSE in a Boolean context. TRUE is a synonym for 1. All non-NULL and non-FALSE data are considered as TRUE in a boolean context. UNKNOWN is a synonym for NULL. The special date 0/0/0 is NULL.

12.4.2 NOT
NOT is the only operator which has only one operand. It returns 0 if the operand is TRUE, returns 1 if the operand is FALSE and returns NULL if the operand is NULL.
SELECT SELECT SELECT SELECT NOT NOT NOT NOT 1 -- returns 0 FALSE -- returns 1 NULL -- returns NULL UNKNOWN -- returns NULL

! is a synonym for NOT.


SELECT !1

12.4.3 AND
AND returns 1 if both the operands are TRUE, else returns 0; if one of the operands is NULL, returns NULL.
SELECT 1 AND 1 -- returns 1 SELECT 1 AND -- return 0 SELECT AND NULL -- returns NULL

&& is a synonym for AND.

55

Operators

SELECT 1 && 1

12.4.4 OR
OR returns TRUE if at least one of the operands is TRUE, else returns FALSE; if one of the operands is NULL, returns NULL.
SELECT SELECT SELECT SELECT TRUE OR FALSE -- returns 1 1 OR 1 -- returns 1 FALSE OR FALSE -- returns 0 NULL OR TRUE -- returns NULL

|| is a synonym for OR.


SELECT 1 || 0

12.4.5 XOR
XOR (eXclusive OR) returns 1 if only one of the operands is TRUE and the other operand is FALSE; returns 0 if both the operands are TRUE o both the operands are FALSE; returns NULL if one of the operands is NULL.
SELECT SELECT SELECT SELECT SELECT 1 XOR 0 -- returns 1 FALSE XOR TRUE -- returns 1 1 XOR TRUE -- returns 0 0 XOR FALSE -- returns 0 NULL XOR 1 -- returns NULL

12.4.6 Synonyms
AND can be written as && OR can be written ad || NOT can be written as ! Only NOT (usually) has a different precedence from its synonym. See operator precedence for datail.

12.5 Arithmetic operators


MySQL supports operands which perform all basic arithmetic operations.

56

Arithmetic operators You can type positive values with a '+', if you want:
SELECT +1 -- return 1

You can type negative values with a '-'. - is an inversion operand:


SELECT -1 -- returns -1 SELECT -+1 -- returns -1 SELECT --1 -- returns 1

You can make sums with '+':


SELECT 1 + 1 -- returns 2

You can make subtractions with '-':


SELECT True - 1 -- returns 0

You can multiply a number with '*':


SELECT 1 * 1 -- returns 1

You can make divisions with '/'. Returns a FLOAT number:


SELECT 10 / 2 -- returns 5.0000 SELECT 1 / 1 -- returns 1.0000 SELECT 1 / 0 -- returns NULL (not an error)

You can make integer divisions with DIV. Resulting number is an INTEGER. No reminder. This has been added in MySQL 4.1.
SELECT 10 DIV 3 -- returns 3

You can get the reminder of a division with '%' or MOD:


SELECT 10 MOD 3 -- returns 1

12.5.1 Using + to cast data


You can convert an INTEGER to a FLOAT doing so:

57

Operators

SELECT 1 + 0.0 -- returns 1.0 SELECT 1 + 0.000 -- returns 1.000 SELECT TRUE + 0.000 -- returns 1.000

You can't convert a string to a FLOAT value by adding 0.0, but you can cast it to an INTEGER:
SELECT '1' + 0 -- returns 1 SELECT '1' + FALSE -- returns 1 SELECT '' + '' -- returns 0

12.6 Text operators


There are no concatenation operators in MySQL. Arithmetic operators convert the values into numbers and then perform arithmetic operations, so you can't use + to concatenate strings. You can use the CONCAT() function instead.

12.6.1 LIKE
The LIKE operator may be used to check if a string matches to a pattern. A simple example:
SELECT * FROM articles WHERE title LIKE 'hello world'

The pattern matching is usually case insensitive. There are two exceptions: when a LIKE comparison is performed against a column which has been declared with the BINARY ag (see CREATE TABLE); when the expression contains the BINARY clause:
SELECT * 'test' LIKE BINARY 'TEST' -- returns 0

You can use two special characters for LIKE comparisons: _ means "any character" (but must be 1 char, not 0 or 2) % means "any sequence of chars" (even 0 chars or 1000 chars) Note that "\" also escapes quotes ("'") and this behaviour can't be changed by the ESCAPE clause. Also, the escape character does not escape itself. Common uses of LIKE: Find titles starting with the word "hello":
SELECT * FROM articles WHERE title LIKE 'hello%'

Find titles ending with the word "world":

58

Text operators

SELECT * FROM articles WHERE title LIKE '%world'

Find titles containing the word "gnu":


SELECT * FROM articles WHERE title LIKE '%gnu%'

These special chars may be contained in the pattern itself: for example, you could need to search for the "_" character. In that case, you need to "escape" the char:
SELECT * FROM articles WHERE title LIKE '\_%' -- titles starting with _ SELECT * FROM articles WHERE title LIKE '\%%' -- titles starting with %

Sometimes, you may want to use an escape character different from "\". For example, you could use "/":
SELECT * FROM articles WHERE title LIKE '/_%' ESCAPE '/'

When you use = operator, trailing spaces are ignored. When you use LIKE, they are taken into account.
SELECT 'word' = 'word ' -- returns 1 SELECT 'word' LIKE 'word ' -- returns 0

LIKE also works with numbers.


SELECT 123 LIKE '%2%' -- returns 1

If you want to check if a pattern doesn't match, you can use NOT LIKE:
SELECT 'a' NOT LIKE 'b' -- returns 1

12.6.2 SOUNDS LIKE


You can use SOUNDS LIKE to check if 2 text values are pronounced in the same way. SOUNDS LIKE uses the SOUNDEX algorithm, which is based on English rules and is very approximate (but simple and thus fast).
SELECT word1 SOUNDS LIKE word2 FROM wordList -- short form SELECT SOUNDEX(word1) = SOUNDEX(word2) FROM wordList -- long form

SOUNDS LIKE is a MySQL-specic extension to SQL. It has been added in MySQL 4.1.

59

Operators

12.6.3 Regular expressions


You can use REGEXP to check if a string matches to a pattern using regular expressions.
SELECT 'string' REGEXP 'pattern'

You can use RLIKE as a synonym for REGEXP.

12.7 Bitwise operators


Bit-NOT:
SELECT 0 -- returns 18446744073709551615 SELECT 1 -- returns 18446744073709551614

Bit-AND:
SELECT 1 & 1 -- returns 1 SELECT 1 & 3 -- returns 1 SELECT 2 & 3 -- returns 2

Bit-OR:
SELECT 1 | 0 -- returns 1 SELECT 3 | 0 -- returns 3 SELECT 4 | 2 -- returns 6

Bit-XOR:
SELECT 1 0 -- returns 1 SELECT 1 1 -- returns 0 SELECT 3 1 -- returns 2

Left shift:
SELECT 1 << 2 -- returns 4

Right shift:
SELECT 1 >> 2 -- 0

60

13 Import/export
Aside from mysqldump (cf. MySQL/Administration1 ), you can also export / import raw data.

13.1 Export data


Data can be exported using the "INTO OUTFILE" keyword
SELECT * FROM destinataire INTO OUTFILE '/tmp/test' WHERE id IN (41, 141, 260, 317, 735, 888, 1207, 2211);

Beware that the MySQL daemon itself will write the le, not the user you run the MySQL client with. The le will be stored on the server, not on your host. Moreover, the server will need write access to the path you specify (usually, the server can _not_write in your home directory, e.g.). Hence why we (unsecurely) used /tmp in the examples. You can also use the command line to export data
mysql < query.txt > output.txt

where query.txt contains an sql-query and the output will be stored in output.txt

13.2 Import data


In another database/computer/etc. the data can be imported:
LOAD DATA INFILE '/tmp/test' INTO TABLE destinataire;

additional options are


FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' IGNORE 1 LINES

to specify how the document is set up and whether there is a header. The columns in the data le can be mapped to the columns of the database table if they do not correspond and it is thus also possible
1 Chapter 16 on page 73

61

Import/export to omit certain columns using a dummy variable:


LOAD DATA LOCAL INFILE '/tmp/test' INTO TABLE destinataire FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' IGNORE 1 LINES ( @dummy, name, phone_number, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy )

In this example, we only need the second and third column of the data le and store these values in the name and phone_number column of our database table.

62

14 Functions
14.1 Syntax
Function names are case insensitive. You can write them as you prefer:
SELECT database() -- ok SELECT DataBase() -- ok SELECT DATABASE() -- ok

If the IGNORE_SPACE SQL_MODE is not set, you can not put a space between the function name and the rst parenthesis. It would return a 1064 error. IGNORE_SPACE is usually 0. The reason is that the parser is faster if that ag is disabled. So:
SELECT DATABASE () -- usually not accepted SELECT DATABASE() -- always works fine

However, this restriction only applies to the native MySQL functions. UDFs and stored functions may be written with a space after the name. You can't use a value calculated in the SELECT clause as a constraint in the WHERE clause (its a chicken & egg problem); the WHERE clause is what determines the values in the SELECT clause. What you want is the HAVING clause which is applied *after* all matching rows have been found.

14.2 General functions


Type-indipendent functions. BENCHMARK(times, espression) Executes espression n times and returns how time it spent. Useful to nd bottlenecks in SQL expressions.
BENCHMARK(10000, CAST(666 AS TEXT))

CAST(value AS type) Returns value converted in the specied type. CHARSET(string) Returns the CHARACTER SET used by string.

63

Functions COALESCE(value, ...) Returns the rst argument which is not NULL. If all arguments are NULL, returns NULL. There must be at least one argument. COERCIBILITY(string) COLLATION(string) Returns the COLLATION used by the string. CONNECTION_ID() Returns the id of the current thread. CONVERT(value, type) Returns value converted to the specied type.
SELECT CONVERT ('666', UNSIGNED INTEGER)

CONVERT(string USING charset) Converts the passed string to the specied CHARACTER SET.
SELECT CONVERT ('This is a text' USING utf8)

CURRENT_USER() Returns the username and the hostname used in the current connection.
SELECT CURRENT_USER() SELECT CURRENT_USER -- it's correct

DATABASE() Returns the current database's name, set with the USE command.
SELECT DATABASE()

FOUND_ROWS() After a SELECT with a LIMIT clause and the SQL_CALC_FOUND_ROWS keyword, you can run another SELECT with the FOUND_ROWS() function. It returns the number of rows found by the previous query if it had no LIMIT clause.
SELECT SQL_CALC_FOUND_ROWS * FROM stats ORDER BY id LIMIT 10 OFFSET 50 SELECT FOUND_ROWS() AS n

GREATEST(value1, value2, ...)

64

Date and time Returns the greatest argument passed. IF(val1, val2, val3) If val1 is TRUE, returns val2. If val1 is FALSE or NULL, returns val3. IFNULL(val1, val2) If val1 is NULL, returns val2; else, returns val1. ISNULL(value) If the value passed is NULL returns 1, else returns 0. INTERVAL(val1, val2, val3, ...) NULLIF(val1, val2) If val1 = val2, returns NULL; else, returns val1. LEAST(value1, value2, ...) Returns the minimum argument passed.

14.3 Date and time


SELECT * FROM mytable WHERE datetimecol >= (CURDATE() - INTERVAL 1 YEAR) AND datetimecol < (CURDATE() - INTERVAL 1 YEAR) INTERVAL 1 DAY;

SELECT IF(DAYOFMONTH(CURDATE()) <= 15, DATE_FORMAT(CURDATE(), '%Y-%m-15'), DATE_FORMAT(CURDATE() + INTERVAL 1 MONTH, '%Y-%m-15')) AS next15 FROM table;

SELECT YEAR('2002-05-10'), MONTH('2002-05-10'), DAYOFMONTH('2002-05-10')

SELECT PurchaseDate FROM table WHERE YEAR(PurchaseDate) <= YEAR(CURDATE())

SELECT columns FROM table WHERE start_time >= '2004-06-01 10:00:00' AND end_time <= '2004-06-03 18:00:00'

SELECT * FROM t1 WHERE DATE_FORMAT(datetime_column, '%T') BETWEEN 'HH:MM:SS' AND 'HH:MM:SS'

SELECT Start_time, End_time FROM Table WHERE Start_time >= NOW() - INTERVAL 4 HOUR SELECT NOW() + INTERVAL 60 SECOND

65

Functions

SELECT UNIX_TIMESTAMP('2007-05-01'); -- 1177970400 SELECT FROM_UNIXTIME(1177970400); -- 2007-05-01 00:00:00

14.4 Aggregate functions


14.4.1 COUNT(eld)
If * is given, instead of the name of a eld, COUNT() returns the number of rows found by the query. It's commonly used to get the number of rows in a table.
SELECT COUNT(*) FROM antiques

If the DISTINCT keyword is used, identical rows are counted only once.
SELECT COUNT(DISTINCT *) FROM antiques

If a eld name is given, returns the number of non-NULL values.


SELECT COUNT(cost) FROM antiques

If a eld name is given and the DISTINCT keyword is given, returns the number of non-NULL values, and identical values are counted only once.
SELECT COUNT(DISTINCT cost) FROM antiques

You can count non-NULL values for an expression:


SELECT COUNT(longitude + latitude) FROM cities

This returns the number of rows where longitude and latitude are both non-NULL.

14.4.2 MAX(eld)
MAX() can be used to get the maximum value for an expression in the rows matching to a query. If no row matches the query, returns NULL.
SELECT MAX(cost) FROM antiques SELECT MAX(LENGTH(CONCAT(first_name, ' ', last_name))) FROM subscribers

66

Aggregate functions

14.4.3 MIN(eld)
MIN() can be used to get the minimum value for an expression in the rows matching to a query. If no row matches the query, returns NULL.
SELECT MIN(cost) FROM antiques

14.4.4 AVG(eld)
AVG() can be used to get the average value for an expression in the rows matching to a query. If no row matches the query, returns NULL.
SELECT AVG(cost) FROM antiques

14.4.5 SUM(eld)
SUM() can be used to get the sum of the values for an expression in the rows matching to a query. If no row matches the query, returns NULL. If SUM(SELECTED expr) is used, identical values are added only once. SUM(DISTINCT) has been added in MySQL 5.1.
SELECT SUM(cost) FROM antiques

14.4.6 GROUP_CONCAT(eld)
GROUP_CONCAT() can be used to concatenate values from all records for a group into a single string separated by comma or any additional token you like.
CREATE TEMPORARY TABLE p ( id INTEGER, ptype VARCHAR(10), pname VARCHAR(50) );

INSERT INTO p VALUES (1,'mp3','iPod'), (2,'mp3','Zune'), (3,'mp3','ZEN'), (4,'notebook','Acer Eee PC'), (4,'notebook','Everex CloudBook');

SELECT * FROM p;

SELECT ptype,group_concat(pname) FROM p GROUP BY ptype;

67

Functions

SELECT ptype,group_concat(' ',pname) FROM p GROUP BY ptype ;

14.4.7 Aggregate bit functions


General syntax:
FUNCTION_NAME(expression)

These functions calculate expression for each row of the result set and perform the calculation between all the expressions. These are bitwise functions. The precision used is 64 bit. AND
SELECT BIT_AND(ip) FROM log

OR
SELECT BIT_OR(ip) FROM log

(returns 0 if there are no rows) XOR


SELECT BIT_XOR(ip) FROM log

(returns 0 if there are no rows)

68

15 Exercises
15.1 Practicing SELECT
15.1.1 Table list
ID 1 2 3 4 5 6 7 8 9 Name Shantanu Shantanu Shantanu Ram Shyam Ram Samir Ram Shyam Surname Oak Oak Oak Joshi Sharma Naik Shah Joshi Sharma FlatHave Goregaon Andheri FlatWant

Dadar Goregaon Andheri Sion Parle Dadar Dadar

15.1.2 Exercise I - Questions


Who has a at in "Goreagon" and who wants to buy one? Who has a at in "Parle" and who wants to buy one? Where does "Shantanu Oak" own the ats and where does he want to buy one? How many entries have been recorded so far? How many ats are there for sale? What are the names of our clients? How many clients do we have? List the customers whose name start with "S"? Rearrange the list Alphabetically sorted.

15.1.3 Exercise I - Answers


select * from list where FlatHave = "Goregaon" or FlatWant = "Goregaon" select * from list where FlatHave = "Parle" or FlatWant = "Parle" select * from list where Name = "Shantanu" and Surname = "Oak" select count(*) from list select count(FlatHave) from list where FlatHave is not null select distinct Name, Surname from list select count(distinct Name, surname) from list

69

Exercises select * from list where Name like "S%" select Surname, Name, FlatHave, FlatWant from list order by Name

15.1.4 Table grades


ID 1 2 3 4 Name John Jim Bill Jeri Math 68 96 65 69 Physics 37 89 12 25 Literature 54 92 57 82

15.1.5 Exercise II - Questions


A list of all students who scored over 90 on his or her math paper? A list of all students who scored more than 85 in all subjects? Declare Results: Print the results of all students with result column. Find out total marks of all the students. What are the average marks of the class for each subject? What are the minimum marks in Math? What are the maximum marks in Math? Who got the highest marks in Math?

15.1.6 Exercise II - Answers


Note: many problems have more than one correct solution. SELECT * FROM grades WHERE math > 90 SELECT name FROM grades WHERE math > 85 AND physics > 85 AND literature > 85 SELECT *, IF( (math <= 35 OR physics <= 35 OR literature <= 35), 'fail', 'pass') AS result FROM grades ORDER BY result DESC SELECT name, math+physics+literature FROM grades SELECT AVG(math), AVG(physics), AVG(literature) FROM grades SELECT MIN(math) FROM grades SELECT MAX(math) FROM grades SELECT * FROM grades ORDER BY math DESC LIMIT 1 -- this is good if we have only one guy with top score.
SELECT * FROM grades where math=max(math); -- the max() function cannot be used after "where". Such usage results in "ERROR 1111 (HY000): Invalid use of group function"

70

Examples

15.2 Examples
15.2.1 Finding Duplicates
SELECT Vendor, ID, Count(1) as dupes FROM table_name GROUP BY Vendor, ID HAVING Count(1) >1

SELECT txt, COUNT(*) FROM dupes GROUP BY txt HAVING COUNT(*) > 1;

SELECT id, COUNT( id ) AS cnt, FROM myTable GROUP BY id HAVING cnt > 1

15.2.2 Remove duplicate entries


Assume the following table and data. CREATE TABLE IF NOT EXISTS dupTest (pkey int(11) NOT NULL auto_increment, a int, b int, c int, timeEnter timestamp(14), PRIMARY KEY (pkey)); insert into dupTest (a,b,c) values (1,2,3),(1,2,3), (1,5,4),(1,6,4); Note, the rst two rows contains duplicates in columns a and b. It contains other duplicates; but, leaves the other duplicates alone. ALTER IGNORE TABLE dupTest ADD UNIQUE INDEX(a,b);

71

16 Administration
16.1 Installation
16.1.1 Debian packages
The package name is usually mysql-server, either directly or as a transitional package for the latest version. Stable There are two Debian packages in the current stable release: mysql-server1 : depends on latest MySQL version mysql-server-5.02 : MySQL 5.0 You can install it using this command:
apt-get install mysql-server

or by installing the package you want using the Synaptic GUI. Backports Backports.org may also offers more recent versions. To install it, you need to add the backports source in your /etc/apt/sources.list:
deb http://www.backports.org/debian lenny-backports main

and then use aptitude:


apt-get install -t lenny-backports mysql-server-5.1

1 2

http://packages.debian.org/lenny/mysql-server http://packages.debian.org/lenny/mysql-server-5.0

73

Administration Uninstall To simply remove the program:


apt-get remove mysql-server

To remove the conguration les as well, resulting in a clean environment:


apt-get remove --purge mysql-server

Debconf will ask you if you want to remove the existing databases as well. Answer wisely!

16.1.2 Fedora Core 5


The package name is [ftp://ftp.tu-chemnitz.de/pub/linux/fedora-core/5/i386/os/Fedora/RPMS/mysqlserver-5.0.18-2.1.i386.rpm mysql-server]. You can install it using this command:
yum install mysql-server

which will take care of installing the needed dependencies. Using pirut (Applications->Add/Remove Software), you can also server MySQL Database in the Servers category:

74

Installation

Figure 1

16.1.3 Gentoo
MySQL is available in the main Portage tree as "dev-db/mysql". You must use the fully qualied ebuild name as "mysql" is made ambiguous by "virtual/mysql" Command:
emerge dev-db/mysql

16.1.4 FreeBSD
The stable FreeBSD port is version 5.03 , and beta version 5.1 is also available. You can install it using this command:
cd /usr/ports/databases/mysql50-server/ && make install clean

http://www.freshports.org/databases/mysql50-server/

75

Administration This command will install the MySQL 5.0 server as well as all necessary dependencies (which includes the MySQL client). t

16.2 Start the service


16.2.1 Debian
In Debian, you use the mysql init script.
/etc/init.d/mysql start /etc/init.d/mysql stop /etc/init.d/mysql restart

If you need to do so in scripts, prefer the invoke-rc.d command, which only restart the service if it is launched on system startup. That way, you do not launch a service if it wasn't meant to be run:
invoke-rc.d mysql start|stop|restart

If you want to control whether to launch MySQL on startup, you can use the rcconf package, or update-rc.d:
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/anysqlservernamehere chmod +x /etc/init.d/anysqlservernamehere update-rc.d anysqlservernamehere defaults

16.2.2 Fedora Core


Fedora Core suggests that you use the service wrapper, which cleans the environment before to run the service, so that all services run in the same standard environment (for example, the current directory is set to the system root /).
service mysqld start|stop|restart service mysqld --full-restart # means stop, then start - not a direct restart

You can also use the /etc/init.d/mysqld if needed. FC5 displays useful hints the rst time you launch the MySQL server (i.e. when launching /usr/bin/mysql_install_db):
$ service mysqld start [...] PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands:

76

Client connection

/usr/bin/mysqladmin -u root password 'new-password' /usr/bin/mysqladmin -u root -h localhost password 'new-password' [...]

See the next section about changing passwords. To control whether to launch MySQL on startup, you can use the ntsysv tool:

Figure 2

16.3 Client connection


There are two ways to connect to a MySQL server, using Unix sockets and TCP/IP. The default TCP/IP port is 3306:
# grep mysql /etc/services mysql 3306/tcp mysql 3306/udp mysql-cluster 1186/tcp Manager mysql-cluster 1186/udp Manager mysql-im 2273/tcp Manager

# MySQL # MySQL # MySQL Cluster # MySQL Cluster # MySQL Instance

77

Administration

mysql-im Manager

2273/udp

# MySQL Instance

As a client, MySQL interprets 'localhost' as 'use the Unix socket'. This means that MySQL won't connect to 127.0.0.1:3306, but will use /var/run/mysqld/mysqld.sock:
$ mysql -h localhost mysql> \s -------------mysql Ver 14.12 Distrib 5.0.22, for redhat-linux-gnu (i386) using readline 5.0 [...] Current user: sylvain@localhost [...] Connection: Localhost via UNIX socket [...] UNIX socket: /var/lib/mysql/mysql.sock

If you really need to connect to MySQL via TCP/IP to the local host without using Unix sockets, then specify '127.0.0.1' instead of 'localhost':
$ mysql -h 127.0.0.1 mysql> \s -------------mysql Ver 14.12 Distrib 5.0.22, for redhat-linux-gnu (i386) using readline 5.0 [...] Current user: sylvain@localhost [...] Connection: 127.0.0.1 via TCP/IP [...] TCP port: 3306

In both cases, MySQL will understand your machine name as 'localhost' (this is used in the privileges system).

16.4 Conguration
Congure /etc/mysql/my.cnf - for heavily loaded databases, for fat databases...; different kinds of connexions (Unix sockets, TCP/IP w/ or w/o SSL, MySQL+SSL licensing issues)

16.4.1 Change the root password


$ mysql -u root mysql> SET PASSWORD = PASSWORD('PassRoot');

For more information, see the #SET_PASSWORD4 section.

Chapter 16.5.7 on page 81

78

Privileges

16.4.2 Network conguration


--bind-address=127.0.0.1 # localhost only --bind-address=0.0.0.0 # listen on all interfaces --bind-address=192.168.1.120 # listen on that IP only

skip-networking When you specify skip-networking in the conguration, then MySQL will not listen on any port, not even on localhost (127.0.0.1). This means that only programs running on the same machine than the MySQL server will be able to connect to it. This is a common setup on dedicated servers. The only way to contact MySQL will be to use the local Unix socket, such as /var/run/mysqld/mysqld.sock (Debian) or /var/lib/mysql/mysql.sock (FC5). You can specify where the socket is located using the socket parameter in the [mysqld] section of the conguration:
[mysqld] ... socket=/var/lib/mysql/mysql.sock

16.5 Privileges
The MySQL privileges system.

16.5.1 Introduction
MySQL requires you to identify yourself when you connect to the database. You provide the following credentials: an identity, composed of: a username a machine name or IP adress (detected automatically by the server) a password, to prove your identity Usually, MySQL-aware applications also ask you for a database name, but that's not part of the credentials, because this does not relate to who you are. MySQL then associate privileges to these credentials; for example, the right to query a given database, add data to another one, create additional databases or remove existing ones, etc.

16.5.2 Who am I?
Once connected, it is not necessarily obvious who MySQL thinks you are. CURRENT_USER() provides this information:

79

Administration

mysql> SELECT CURRENT_USER(); +----------------+ | CURRENT_USER() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec)

16.5.3 SHOW GRANTS


Prototype:
SHOW GRANTS FOR user SHOW GRANTS --current user

SHOW GRANTS allow you to check the current privileges for a given user. For example, here are the default privileges for user root:
mysql> SHOW GRANTS FOR 'root'@'localhost'; +--------------------------------------------------------------------+ | Grants for root@localhost | +--------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION | +--------------------------------------------------------------------+ 1 row in set (0.00 sec)

You also use use SHOW GRANTS; to check the privileges for the current user.

16.5.4 GRANT
The GRANT command allow you to give (GRANT) privileges to a given user.

16.5.5 DROP USER


DROP USER 'mediawiki'; DROP USER 'mediawiki'@'host';

Starting with v5.0.2, this removes the associated privileges as well. With earlier versions you also need to REVOKE its PRIVILEGES manually.

80

Privileges

16.5.6 REVOKE
REVOKE ALL PRIVILEGES ON database.* FROM 'user'@'host'; REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'user'@'host';

16.5.7 SET PASSWORD


Prototype:
SET PASSWORD [FOR user] = PASSWORD('your_password')

If user is not specied, the current user is used (this is useful when you connect to mysql using the command line). Example with an explicit user:
SET PASSWORD FOR 'mediawiki'@'localhost' = PASSWORD('ifda8GQg');

There is a command-line synonym:


mysqladmin password 'your_password'

(with the usual connection options -h -u and -p) However, using passwords on the command line presents a security risk. For example, if root changes his MySQL password:
root# mysqladmin password 'K2ekiEk3'

Then another user can spy on him by looking at the process list:
user$ ps aux | grep mysqladmin root 7768 0.0 0.1 7044 mysqladmin password K2ekiEk3

1516 pts/1

S+

16:57

0:00

Conclusion: don't user mysqladmin password. If you are looking for a way to generate passwords, either secure or easy to remember, try the pwgen program (there is a Debian package available):
$ pwgen ooGoo7ba ir4Raeje Ya2veigh zaXeero8 Dae8aiqu rai9ooYi phoTi6gu Yeingo9r tho9aeDa Ohjoh6ai Aem8chee aheich8A Aelaeph3 eu4Owudo koh6Iema oH6ufuya [...] $ pwgen -s # secure zCRhn8LH EJtzzLRE G4Ezb5BX e7hQ88In TB8hE6nn f8IqdMVQ t7BBDWTH

81

Administration

ZZMhZyhR gbsXdIes hCQMbPE6 XD8Owd0b xitloisw XCWKX9B3 MEATkWHH vW2Y7HnA 3V5ubf6B [...]

Very handy if you manage a lot of accounts :)

16.5.8 MySQL 4.1 password issues


As of version 4.1, MySQL introduced a password-related change. You'll experience this via errors such as: Client does not support authentication protocol requested by server; consider upgrading MySQL client. 5 If you wish to support older client programs, you need to dene the MySQL account password this way:
SET PASSWORD [FOR user] = OLD_PASSWORD('your_pass');

There is apparently no way to use old passwords with the GRANT ... 'password' syntax.

IDENTIFIED BY

Alternatively, you can use the old_passwords conguration option in your server's my.cnf. This means that new passwords will be encoded using the old-style, shorter, less secure format. For example, in Debian Sarge and FC5, the MySQL default conguration enforces old-style password for backward compatibility with older clients:
[mysqld] ... old_passwords=1

16.6 Processes
MySQL provides a Unix-like way to show the current server threads and kill them.

16.6.1 SHOW PROCESSLIST


Here is a peaceful MySQL server:

For example, you can get this error on Debian Sarge's apache+libapache_mod_php4+php4-mysql, the latter depends on libmysqlclient12 aka MySQL 4.0 (ldd /usr/lib/php4/20020429/mysql.so gives libmysqlclient.so.12 => /usr/lib/libmysqlclient.so.12). If you rely and libmysqlclient14 or later, then your application supports both the old and the new password formats.

82

Processes

mysql> SHOW PROCESSLIST; +----+----------+-----------+-----------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+----------+-----------+-----------+---------+------+-------+------------------+ | 34 | monddprod | localhost | monddprod | Sleep | 1328 | | NULL | | 43 | root | localhost | NULL | Query | 0 | NULL | SHOW PROCESSLIST | +----+----------+-----------+-----------+---------+------+-------+------------------+ 2 rows in set (0.00 sec)

mysqladmin provides a command-line synonym:


$ mysqladmin processlist +----+----------+-----------+-----------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+----------+-----------+-----------+---------+------+-------+------------------+ | 34 | monddprod | localhost | monddprod | Sleep | 1368 | | | | 44 | root | localhost | | Query | 0 | | show processlist | +----+----------+-----------+-----------+---------+------+-------+------------------+

16.6.2 KILL
If a heavy, nasty query is consuming too much resources on your server, you need to shut it down.
TODO: Add a sample SHOW PROCESSLIST output here

The brute force way is to restart the server:


/etc/init.d/mysql restart

A more subtle way is to use SHOW PROCESSLIST to identify the nasty query, and kill it independently of other server threads.
mysql> KILL 342; Query OK, 0 rows affected (0.00 sec)

There is also a command-line synonym:

83

Administration

$ mysqladmin kill 342

16.7 Security
Basic security: rewall (iptables), SELinux? also some words about: do not store passwords as cleartext

16.8 Backup
Backup/recovery and import/export techniques.

16.8.1 mysqldump
mysqldump --opt -h 192.168.2.105 -u john -p'****' mybase | gzip > mybase-date +%Y%m%d.sql.gz

This creates the mybase-20061027.sql.gz le. --opt is the magical option that uses all the options that are generally useful. In recent versions of mysqldump, it is even enabled by default, so you need not type it. --opt means --add-drop-table --add-locks --create-options --disable-keys --extended-insert --lock-tables --quick --set-charset - so it will lock tables during the backup for consistency, add DROP TABLE statements so the dump can be applied without cleaning the target database, will use the most efcient ways to perform the INSERTs and specify the charset (latin1, Unicode/UTF-8...) used. If you don't provide a database to mysqldump, you'll get a backup containing all databases - which is less easy to use for restoring a single database later on.

16.8.2 Daily rotated mysqldump with logrotate


We're using logrotate in a slightly non-standard way to keep a batch of dumps. Each day, logrotate will cycle the dumps so as to keep the last N dumps, removing old backups automatically, and generating the new one immediately through a postrotate hook. The following conguration keeps 2 months of daily backups:
/dumps/mybase.sql.gz { rotate 60 dateext daily nocompress nocopytruncate postrotate HOME=/root mysqldump --opt mybase | gzip >

84

Backup

/dumps/mybase.sql.gz endscript }

Cf. logrotate(8) in the GNU/Linux man pages for more information. Variant to backup all databases at once:
/dumps/*/*.sql.gz { daily rotate 20 dateext nocompress sharedscripts create postrotate export HOME=/root for i in $(mysql --batch --skip-column-names -e 'SHOW DATABASES' | grep -v 'information_schema$'); do if [ ! -e /dumps/$i ]; then mkdir -m 700 /dumps/$i; fi mysqldump --opt $i | gzip -c > /dumps/$i/$i.sql.gz done endscript }

Setup: Create your /.my.cnf for password-less database access Place the logrotate conguration le above in the /etc/logrotate.d/ directory Bootstrap the rst dump: mkdir -m 700 /dumps mkdir -m 700 /dumps/mybase touch /dumps/mybase/mybase.sql.gz logrotate -f /etc/logrotate.d/mysql-dumps Check the dump using zcat /dumps/mybase.sql.gz. Comments on the code: HOME=/root is needed for systems (such as FC5) that set HOME=/ in their cron, which prevents mysqldump from nding the .my.cnf conguration. We also use | gzip instead of logrotate's compress option for disk I/O efciency (single-step). In production, you'll get something like this:
# ls -lt /dumps total 16520 -rw-r----- 1 root clisscom -rw-r----- 1 root clisscom clisscom.sql.gz-20100302 -rw-r----- 1 root clisscom clisscom.sql.gz-20100301 -rw-r----- 1 root clisscom clisscom.sql.gz-20100228 -rw-r----- 1 root clisscom clisscom.sql.gz-20100227 -rw-r----- 1 root clisscom

2819533 mar 2815193 mar

2 06:25 clisscom.sql.gz 1 06:25

2813579 fv 28 06:26 2812251 fv 27 06:25 2810803 fv 26 06:25 2808785 fv 25 06:25

85

Administration

clisscom.sql.gz-20100226 ...

Beware that the date in the lename is the date of the rotation, not the date of the dump. Using dateext helps with remote backups, because lenames don't change daily, not you avoid redownloading all of /dumps each time.

16.8.3 Remote mysqldump using CGI


mysqldump can be found sometimes in shared-hosting facilities. You can use a simple CGI script to get a direct dump:
#!/bin/sh echo "Content-Type: application/x-tar" echo "Content-Encoding: x-gzip" echo "" mysqldump --host=mysql.hosting.com --user=john --password=XXXXX my_base | gzip 2>&1

You can then get it with your browser or wget:


$ wget -O- --quiet http://localhost/sylvain/test2.cgi > base-date +%Y%m%d.sql.gz

You can even re-inject it on-the-y in your local test database:


$ wget -O- --quiet http://localhost/sylvain/test2.cgi | gunzip | mysql test_install -u myself -pXXXX

Protect the script with a .htaccess, write a .netrc for wget to use, and you'll have a simple, unattended way to grap a backup even without command-line access. This allows to gain time when grabing a dump (compared to using phpMyAdmin) and to setup remote automated backups (no interaction is needed). Something similar should be feasible in PHP provided you have access to exec().

16.8.4 Exporting a single table


If you need to import/export a table, not a complete database, check MySQL/Language#Import_.2F_export6 .

http://en.wikibooks.org/wiki/MySQL%2FLanguage%23Import_.2F_export

86

Binary logs

16.9 Binary logs


Binary logs are a mechanism to keep track of everything that happens on the MySQL server (forensics), allowing to replay the same sequence of commands on a different computer (master/slave replication), or at a later time (crash recovery). On Debian they are stored in /var/log/mysql/mysql-bin.0*. To view the SQL commands in a binary log, you use the mysqlbinlog command:
mysqlbinlog /var/log/mysql/mysql-bin.000001

For the crash recovery to be useful, binary logs are usually stored on a different computer (via a NFS mount, for example). Note that it is meant to recover the full mysql server, not just one database. You could attempt to lter the log by database, but this isn't straightforward. So in order use binary logs as a recovery plan, you usually combine them with a full standard backup:
mysqldump -A | gzip > all.sql.gz

To ush/reset the logs at the same time (TODO: test):


mysqldump -A --master-data --flush-logs | gzip > all.sql.gz

To recover you'll just combine the two sources (preferably, disable binary logging in the server conguration during the recovery, and re-enable it right after.):
(zcat all.sql.gz && mysqlbinlog /var/log/mysql/mysql-bin.0*) | mysql

16.10 Logs
Where interesting logs are located, common errors to look at. For example:
tail -f /var/log/mysql.log

16.11 Admin Tools


Various third-party graphical interfaces and utilities.

87

Administration

16.11.1 Web interfaces


phpMyAdmin (wikipedia: phpMyAdmin7 ) eSKUeL8 : an alternative to phpMyAdmin MySQL on Servers Support9

16.11.2 Desktop GUI


MySQL Administrator:10 from MySQL AB. If you want to create real backups, though, do not use this, since it runs backups using at on the client machine - which is likely not to be online every day.
11

7 8 9 10 11

http://en.wikipedia.org/wiki/PhpMyAdmin http://eskuel.sourceforge.net/ http://www.runmapglobal.com/blog/mysql-databases-on-dedicated-servers/ http://mysql.com/products/tools/administrator/ http://en.wikibooks.org/wiki/Category%3A

88

17 Replication
17.1 What is replication
Replication means that data written on a master MySQL will be send to separate server and executed there. Applications: backups spread read access on multiple servers for scalability failover/HA Replication types: Asynchronous replication (basic master/slave) Semi-asynchronous replication (asynchronous replication + enforce 1 slave replication before completing queries) Replication congurations: standard: master->slave dual master: master<->master In Master-Master replication both hosts are masters and slaves at the same time. ServerA replicates to serverB which replicates to serevrA. There are no consistency checks and even with auto_increment_increment/auto_increment_offset congured both servers should not be used for concurrent writes.

17.2 Asynchronous replication


That's the most simple replication. A master writes a binary log le, and slaves can read this log le (possibly selectively) to replay the query statements. It's asynchronous, which mean the master and slaves may have different states at a specic point of time; also this setup can survive a network disconnection.

17.2.1 Conguration on the master


In /etc/mysql/my.cnf, in the [mysqld] section: Dene a server identier (detects loops?); customarily we'll use 1 for the server, but it can be different:

89

Replication

server-id = 1

Replication is based on binary logs1 , so enable them:


log-bin # or log-bin = /var/log/mysql/mysql-bin.log

Create a new user for the slave to connect with:


CREATE USER 'myreplication'; SET PASSWORD FOR 'myreplication' = PASSWORD('mypass'); GRANT REPLICATION SLAVE ON *.* to 'myreplication';

Verify your server identier:


SHOW VARIABLES LIKE 'server_id';

17.2.2 Conguration on each slave


In /etc/mysql/my.cnf, in the [mysqld] section: Dene a server identier, different than the master (and different than the other slaves):
server-id = 2

Verify with:
SHOW VARIABLES LIKE 'server_id';

You can also declare the slave hostname to the master (cf. SHOW SLAVE HOSTS below):
report-host=slave1

Declare the master:


CHANGE MASTER TO MASTER_HOST='master_addr', MASTER_USER='myreplication', MASTER_PASSWORD='mypass';

If setting up replication from backup, specify start point (add to previous command):
MASTER_LOG_FILE='<binary_log_from_master>', MASTER_LOG_POS=<master_binary_log_position>;

Start the replication:

Chapter 16.9 on page 87

90

Asynchronous replication

START SLAVE;

This will create a le named master.info in your data directory, typically /var/lib/mysql/master.info; this le will contain the slave conguration and status. TODO:
Oct 15 21:11:19 builder mysqld[4266]: 101015 21:11:19 [Warning] Neither --relay-log nor --relay-log-index were used; so replication may break when this MySQL server acts as a slave and has his hostname changed!! Please use '--relay-log=mysqld-relay-bin' to avoid this problem.

17.2.3 Check the replication


On the slave On a slave, type:
SHOW SLAVE STATUS;

Or more for a more readable (line-based) output:


SHOW SLAVE STATUS\G

Example:
*************************** 1. row *************************** Slave_IO_State: Master_Host: master_addr Master_User: myreplication Master_Port: 3306 ...

Check in particular:
Slave_IO_Running: Yes Slave_SQL_Running: Yes

You can suspect the asynchronous nature of the replication:


Seconds_Behind_Master: 0

91

Replication See also:


mysql> SHOW GLOBAL VARIABLES LIKE "%SLAVE%";

On the master You can see a connection from the slave in the process list.
mysql> SHOW PROCESSLIST\G [...] *************************** 6. row *************************** Id: 14485 User: myreplication Host: 10.1.0.106:33744 db: NULL Command: Binlog Dump Time: 31272 State: Has sent all binlog to slave; waiting for binlog to be updated Info: NULL

If you enabled report-host, the slave is also visible in:


mysql> SHOW SLAVE HOSTS; +-----------+---------+------+-------------------+-----------+ | Server_id | Host | Port | Rpl_recovery_rank | Master_id | +-----------+---------+------+-------------------+-----------+ | 2 | myslave | 3306 | 0 | 1 | +-----------+---------+------+-------------------+-----------+ 1 row in set (0.00 sec)

17.2.4 Consistency
Note that this replication is a simple replay, similar to feeding a mysqldump output to the mysql client. Consequently, to maintain the consistency: Do not write on the slave (this is possible!!) Start the replication with identical initial data on both the master and the slave To test: we suspect it would be best to use the same version of MySQL on the master and slaves

17.2.5 Fixing
By default, replicate will stop if it meets an error. This can happen if your master and slaves were not consistent in the beginning, or due to a network error causing a malformed query. In this case, you'll get a trace in the system log (typically /var/log/syslog):

92

Asynchronous replication

Oct 15 21:11:19 builder mysqld[4266]: 101015 21:11:19 [ERROR] Slave: Error 'Table 'mybase.form' doesn't exist' on query. Default database: 'mybase'. Query: 'INSERT INTO form (form_id,timestamp,user_id) VALUES ('abed',1287172429,0)', Error_code: 1146

The best way is to reset the replication entirely. You can also x the mistake manually, and then ask MySQL to skip 1 statement this way:
STOP SLAVE; SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1; START SLAVE;

You can set SQL_SLAVE_SKIP_COUNTER to any number, e.g. 100. Beware that in this case, it will skip both valid and invalid statements, not only errors. Another way to x broken replication is to use Maatkit tools. mk-slave-restart (to restart replication on slave it there are more errors and SQL_SLAVE_SKIP_COUNTER can't help) mk-table-checksum (to perform checksumming of tables on master and slave) mk-table-sync (to sync slave with master based on stats generated by mk-table-checksum)

17.2.6 Uninstalling
To erase the replication: Type:
mysql> RESET SLAVE;

Note: at this point, MySQL paused the slave and replaced the conguration with default values. The master.info le was also removed. Restart MySQL to clear all conguration. Warning: STOP SLAVE will stop replication. It can be started manually again or (by default) it will automatically resume if you restart the MySQL server. To avoid auto start of replication during process of startup, add to your conguration le:
slave-skip-start

If you want to stop the replication for good (and use the server for another purpose), you need to reset the conguration as explained above. At this point your slave conguration should be completely empty:

93

Replication

mysql> SHOW SLAVE STATUS; Empty set (0.00 sec)

94

18 Databases manipulation
18.1 Creation
CREATE DATABASE database; Require ? privilege. mysqladmin create is a command-line wrapper for this function.

18.2 Deletion
DROP DATABASE database; Require ? privilege. mysqladmin drop is a command-line wrapper for this function. The -f option can be used to suppress the interactive conrmation (useful for unattended scripts).

18.3 Rename
In some 5.1.x versions there was a RENAME DATABASE command, but it has been removed because renaming databases via SQL caused some problems. However, in the command-line, you can create/export/import/delete:
mysqladmin create name2 mysqldump --opt name1 | mysql name2 mysqladmin drop -f name1

Another option, if you have root access, is to rename the database directory:
cd /var/lib/mysql/ /etc/init.d/mysql stop mv name1/ name2/ /etc/init.d/mysql start

You also need to drop privileges on name1 and recreate them on name2:

95

Databases manipulation

UPDATE mysql.db SET Db='name2' WHERE Db='name1'; FLUSH PRIVILEGES;

18.4 Copy
There is no direct copy command in MySQL. However, this can easily be done using some tools.

18.4.1 With phpMyAdmin 18.4.2 With mysqldump


As seen in the Backup section, mysqldump can be used to generate a complete at-le copy of the database. You can then reinject this copy in another database. This requires a direct access to the database; if you do not have it, you may need to use phpMyAdmin instead.
# First, clean-up the target database: mysqladmin drop -f base2 mysqladmin create base2 # Copy base1 to base2: mysqldump --opt base1 | mysql base2

18.5 Migration from other databases


TODO Tools: MySQL Migration Toolkit1

18.6 Tools for data modeling


MySQL Query Browser apparently includes a MySQL Table Editor module. WWW SQL Designer2 demo3 Kexi4 (wikipedia: Kexi5 )

1 2 3 4 5

http://mysql.com/products/tools/migration-toolkit/ http://ondras.praha12.net/sql http://ondras.praha12.net/sql/demo http://www.kexi-project.org/ http://en.wikipedia.org/wiki/Kexi

96

Tools for data modeling

18.6.1 DB Designer 4 and MySQL Workbench


DBDesigner begins to be old. It is released under the GNU GPL, but it cannot be fully considered as free software since it requires the non-free Kylix compiler to build. But MySQL AB aquired fabFORCE 6 , who distributed DB Designer, and MySQL Workbench7 is the next version. For now the project is still Alpha and not ready for use yet. Meanwhile, if you use the latest release of DBDesigner, you'll nd that it cannot connect to MySQL, with the "unable to load libmysqlclient.so" error. To workaround this, Install the MySQL "Shared compatibility libraries" (from http://dev.mysql.com/ downloads/mysql/5.0.html#downloads for version 5.0, generic RPMS aka MySQLshared-compat.i386 will do). Replace DBDesigner's version of libmysqlclient.so with the newly installed one:
sudo ln -sf /usr/lib/libmysqlclient.so.10 /usr/lib/DBDesigner4/libmysqlclient.so

Find and install kylixlibs3-unwind-3.0-rh.4.i386.rpm Find an old xorg (e.g. xorg-x11-libs-6.8.2-37.FC4.49.2.1.i386.rpm from FC4) and extract it:
rpm2cpio x.rpm | cpio -i

Get libXft.so.1.1 in that package and install it:


sudo cp libXft.so.1.1 /usr/lib ldconfig

You now can connect to your MySQL5 server from DBDesigner4. Consider this a temporary work-around waiting for community (free) and commercial (not free) versions MySQL Workbench.

18.6.2 OpenOfce Base and ODBC


Typical conguration : MySQL database on a host machine (which name is mysqlhost below) OOo 2 on a client machine (Debian GNU/Linux for instance) Connection via ODBC8 . It's a client conguration : we need mysql-client:
aptitude install mysql-client

6 7 8

In the forums: http://www.mysqltalk.org/db-designer-4-vt146168.html but we'd need something more ofcial http://en.wikipedia.org/wiki/MySQL%20Workbench http://en.wikipedia.org/wiki/ODBC

97

Databases manipulation Under Fedora/CentOS:


yum install mysql

Before installing ODBC, we can test the remote connexion locally:


$ mysql -h mysqlhost -u user1 mysqldatabase -p Enter password: PassUser1

You must have create the database mysqldatabase and the user user1 on mysqlhost. It seems there is no problem (hope there is not ;-)):
Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 33 to server version: 5.0.24a-Debian_5bpo.1-log Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>

Then, it's possible to test, through different queries :


mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysqldatabase | +--------------------+ 2 rows in set (0.00 sec) .... mysql> quit; Bye

Fine ! Let's go with OOo and ODBC, on the client machine:


aptitude install libmyodbc unixodbc

For Fedora/CentOS:
yum install mysql-connector-odbc unixODBC

/etc/odbc.ini (empty le) and /etc/odbcinst.ini are created. odbcinst.ini declares the available ODBC driver. Here's the MySQL statement (paths to the .so les may vary depending on the distribution); for Debian:
[MySQL] Description Driver

= MySQL driver = /usr/lib/odbc/libmyodbc.so

98

Tools for data modeling

Setup CPTimeout CPReuse FileUsage

= /usr/lib/odbc/libodbcmyS.so = = = 1

for CentOS:
[MySQL] Description Driver Setup FileUsage

= = = =

ODBC for MySQL /usr/lib/libmyodbc3.so /usr/lib/libodbcmyS.so 1

Now we can use odbcinst :


# odbcinst -j unixODBC 2.2.4 DRIVERS............: /etc/odbcinst.ini SYSTEM DATA SOURCES: /etc/odbc.ini USER DATA SOURCES..: /root/.odbc.ini

For further options : man odbcinst First of all, we have to create at least one DSN (Data Source Name or Data Set Name), because every ODBC connection is initialized through an existing DSN. It's true in every cases, so it is required for an ODBC connection from OOo. To create a DSN, one have different possibilities : Modify /etc/odbc.ini (concerns all users) Modify /.odbc.ini (concerns a specic user) Use graphical applications such as ODBCCong (Debian: unixodbc-bin, Fedora: unixODBC-kde). Finally, these graphical applications modify /etc/odbc.ini or /.odbc.ini For instance, a /etc/odbc.ini le (the name of the DSN is between brackets []):
[MySQL-test] Description TraceFile Driver SERVER USER PASSWORD DATABASE

= = = = = = =

MySQL ODBC Database stderr MySQL mysqlhost user1 mysqldatabase

In that case, the DSN is called MySQL-test Then we can test, using isql command:
$ isql -v MySQL-test user1 PassUser1 +---------------------------------------+ | Connected! | | |

99

Databases manipulation

| sql-statement | | help [tablename] | | quit | | | +---------------------------------------+ SQL> show databases; +-------------------+ | Database | +-------------------+ | information_schema| | mysqldatabase | +-------------------+ 2 rows affected 2 rows returned SQL> quit;

And now, from OOo:


-> File -> New -> Database -> Connecting to an existing database -> MySQL -> Next -> Connect using ODBC -> Next -> Choosing a Data Source -> MySQL-test -> Next -> Username : user1 (tick password required) -> Yes, register the database for me -> Finish

At that step, one is connected to the mysqldatabase database, under the user user1. Just before accessing the database, for example to create tables, one will give user1 password. Then, through OOo, it is now quite easy to access and manipulate the database. We can just notice that Java is required in the following cases : Wizard to create a form (at the opposite, to create a form directly don't need any JRE). Wizard to create reports. Wizard to create queries (at the opposite, to create a query directly or through a view don't need any JRE). Wizard to create tables (at the opposite, to create a table directly or to create a view don't need any JRE). GNU/Linux distros usually ships OpenOfce with IcedTea (openjdk-6-jre/java-1.6.0-openjdk) or GCJ (java-gcj-compat/java-1.4.2-gcj-compat) so that these Java-based features work.
9

http://en.wikibooks.org/wiki/Category%3A

100

19 Appendixes
19.1 Cheat Sheet
19.1.1 Query
SELECT SELECT SELECT SELECT SELECT SELECT SELECT SELECT SELECT SELECT SELECT * FROM table * FROM table1, table2, ... field1, field2, ... FROM table1, table2, ... ... FROM ... WHERE condition ... FROM ... WHERE condition GROUP BY field ... FROM ... WHERE condition GROUP BY field HAVING condition2 ... FROM ... WHERE condition ORDER BY field1, field2 ... FROM ... WHERE condition ORDER BY field1, field2 DESC ... FROM ... WHERE condition LIMIT 10 DISTINCT field1 FROM ... DISTINCT field1, field2 FROM ...

SELECT SELECT SELECT SELECT

... ... ... ...

FROM FROM FROM FROM

t1 t1 t1 t1

JOIN LEFT JOIN JOIN

t2 ON t1.id1 = t2.id2 WHERE condition JOIN t2 ON t1.id1 = t2.id2 WHERE condition (t2 JOIN t3 ON ...) ON ... t2 USING(id) WHERE condition

19.1.2 Conditionals
field1 = value1 field1 <> value1 field1 LIKE 'value _ %' field1 IS NULL field1 IS NOT NULL field1 IN (value1, value2) field1 NOT IN (value1, value2) condition1 AND condition2 condition1 OR condition2

19.1.3 Data Manipulation


INSERT INTO table1 (field1, field2, ...) VALUES (value1, value2, ...) INSERT table1 SET field1=value_1, field2=value_2 ...

DELETE FROM table1 / TRUNCATE table1 DELETE FROM table1 WHERE condition -- join: DELETE FROM table1, table2 WHERE table1.id1 = table2.id2 AND condition

101

Appendixes

UPDATE table1 SET field1=new_value1 WHERE condition -- join: UPDATE table1, table2 SET field1=new_value1, field2=new_value2, ... WHERE table1.id1 = table2.id2 AND condition

19.1.4 Browsing
SHOW SHOW SHOW DESC SHOW SHOW SHOW SHOW KILL DATABASES TABLES FIELDS FROM table / SHOW COLUMNS FROM table / DESCRIBE table / table / EXPLAIN table CREATE TABLE table CREATE TRIGGER trigger TRIGGERS LIKE '%update%' PROCESSLIST process_number

$ mysqlshow $ mysqlshow database

19.1.5 Create / delete / select / alter database


CREATE DATABASE [IF NOT EXIST] mabase [CHARACTER SET charset] [COLLATE collation] CREATE DATABASE mabase CHARACTER SET utf8 DROP DATABASE mabase USE mabase

ALTER DATABASE mabase CHARACTER SET utf8

19.1.6 Create/delete/modify table


CREATE TABLE table (field1 type1, field2 type2, ...) CREATE TABLE table (field1 type1, field2 type2, ..., INDEX (field)) CREATE TABLE table (field1 type1, field2 type2, ..., PRIMARY KEY (field1)) CREATE TABLE table (field1 type1, field2 type2, ..., PRIMARY KEY (field1, field2)) CREATE TABLE table1 (fk_field1 type1, field2 type2, ..., FOREIGN KEY (fk_field1) REFERENCES table2 (t2_fieldA)) [ON UPDATE|ON DELETE] [CASCADE|SET NULL] CREATE TABLE table1 (fk_field1 type1, fk_field2 type2, ..., FOREIGN KEY (fk_field1, fk_field2) REFERENCES table2 (t2_fieldA, t2_fieldB)) CREATE TABLE table IF NOT EXISTS (...)

CREATE TABLE new_tbl_name LIKE tbl_name [SELECT ... FROM tbl_name ...]

CREATE TEMPORARY TABLE table (...)

DROP DROP DROP DROP

TABLE table TABLE IF EXISTS table TABLE table1, table2, ... TEMPORARY TABLE table

102

Cheat Sheet

ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE NULL ... ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE ALTER TABLE

table table table table table table table table table table table table

MODIFY MODIFY CHANGE CHANGE

field1 type1 field1 type1 NOT NULL ... old_name_field1 new_name_field1 type1 old_name_field1 new_name_field1 type1 NOT

ALTER field1 SET DEFAULT ... ALTER field1 DROP DEFAULT ADD new_name_field1 type1 ADD new_name_field1 type1 FIRST ADD new_name_field1 type1 AFTER another_field DROP field1 ADD INDEX (field); ADD PRIMARY KEY (field);

-- Change field order: ALTER TABLE table MODIFY ALTER TABLE table MODIFY ALTER TABLE table CHANGE ALTER TABLE table CHANGE another_field

field1 type1 FIRST field1 type1 AFTER another_field old_name_field1 new_name_field1 type1 FIRST old_name_field1 new_name_field1 type1 AFTER

ALTER TABLE old_name RENAME new_name;

19.1.7 Keys
CREATE TABLE table (..., PRIMARY KEY (field1, field2)) CREATE TABLE table (..., FOREIGN KEY (field1, field2) REFERENCES table2 (t2_field1, t2_field2)) ALTER TABLE table ADD PRIMARY KEY (field);

19.1.8 Privileges
GRANT ALL PRIVILEGES ON base.* TO 'user'@'localhost' IDENTIFIED BY 'password'; GRANT SELECT, INSERT, DELETE ON base.* TO 'user'@'localhost' IDENTIFIED BY 'password'; REVOKE ALL PRIVILEGES ON base.* FROM 'user'@'host'; -- one permission only REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'user'@'host'; -- all permissions

SET PASSWORD = PASSWORD('new_pass') SET PASSWORD FOR 'user'@'host' = PASSWORD('new_pass') SET PASSWORD = OLD_PASSWORD('new_pass')

DROP USER 'user'@'host'

19.1.9 Main data types


TINYINT (1o: -127+128) SMALLINT (2o: +-65 000) MEDIUMINT (3o: +-16 000 000) INT (4o: +- 2 000 000 000) BIGINT (8o: +-9.1018) Precise interval: -(2(8*N-1)) -> (28*N)-1 /!\ INT(2) = "2 digits displayed" -- NOT "number with 2 digits max"

103

Appendixes

INT NOT NULL auto_increment PRIMARY KEY -- auto-counter for PK

FLOAT(M,D) DOUBLE(M,D) FLOAT(D=0->53) /!\ 8,3 -> 12345,678 -- NOT 12345678,123!

TIME (HH:MM) YEAR (AAAA) DATE (AAAA-MM-JJ) DATETIME (AAAA-MM-JJ HH:MM; annes 1000->9999) TIMESTAMP (like DATETIME, but 1970->2038, compatible with Unix)

VARCHAR (single-line; explicit size) TEXT (multi-lines; max size=65535) BLOB (binary; max size=65535) Variants for TEXT&BLOB: TINY (max=255) MEDIUM (max=16000) LONG (max=4Go) Ex: VARCHAR(32), TINYTEXT, LONGBLOB, MEDIUMTEXT

ENUM ('value1', 'value2', ...) -- (default NULL, or '' if NOT NULL)

19.1.10 Forgot root password?


$ /etc/init.d/mysql stop $ mysqld_safe --skip-grant-tables $ mysql # on another terminal mysql> UPDATE mysql.user SET password=PASSWORD('nouveau') WHERE user='root'; ## Kill mysqld_safe from the terminal, using Control + \ $ /etc/init.d/mysql start

19.1.11 Repair tables after unclean shutdown


mysqlcheck --all-databases mysqlcheck --all-databases --fast

19.1.12 load data from local le


1

19.2 About this book


19.2.1 Contributors
Beuc2 : structured the book in chapters and setup the print version; wrote the initial Administration, Database Manipulation, CheatSheet section; contributed to Introduction (MySQL license), Optimization (query cache and benchmark examples, indices exercise), Table types (reference other possible table types), Language (datetime/timestamp valid intervals), Pivot table (alternate

1 2

http://en.wikibooks.org/wiki/Category%3A http://en.wikibooks.org/wiki/User%3ABeuc

104

About this book version w/o maths). I'd like to thank my employer, Cliss XXI3 , for giving me time to work on these chapters. Then wrote the Replication section (on free time). Lathspell4 : wrote the initial Optimization section LucienPetit5 : wrote the initial OpenOfce Base and ODBC section. I'd like to thank my employer, Cliss XXI6 , for giving me time to work on it (but I also worked on my free time). Shantanuo7 : wrote the initial Language section. Sante Caserio8 : started Stored Programs; started APIs; edited some existing stuff; added Language.Operators; added Table Types.Metadata about Sorage Engines;

3 4 5 6 7 8 9

http://www.cliss21.com http://en.wikibooks.org/wiki/User%3ALathspell http://en.wikibooks.org/wiki/User%3ALucienPetit http://www.cliss21.com http://en.wikibooks.org/wiki/User%3AShantanuo http://en.wikibooks.org/wiki/User%3ASante%20Caserio http://en.wikibooks.org/wiki/Category%3A

105

20 GNU Free Documentation License


1. REDIRECT Wikibooks:GNU Free Documentation License1

http://en.wikibooks.org/wiki/GNU%20Free%20Documentation%20License

107

21 Contributors
Edits 1 25 1 1 146 7 2 2 1 2 1 3 2 1 1 1 7 2 1 1 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

User Aaroncool91 Adrignola2 Antidrugue3 Avicennasis4 Beuc5 Dashed6 Fishpi7 Frozen Wind8 Guanaco9 Hannes Rst10 Istarlamanion11 JackPotte12 Jamesday13 Jomegat14 Lathspell15 Logictheo16 LucienPetit17 Marisap18 Matthew-RM19 Nitrodist20 Oliebollen21

http://en.wikibooks.org/w/index.php?title=User:Aaroncool9 http://en.wikibooks.org/w/index.php?title=User:Adrignola http://en.wikibooks.org/w/index.php?title=User:Antidrugue http://en.wikibooks.org/w/index.php?title=User:Avicennasis http://en.wikibooks.org/w/index.php?title=User:Beuc http://en.wikibooks.org/w/index.php?title=User:Dashed http://en.wikibooks.org/w/index.php?title=User:Fishpi http://en.wikibooks.org/w/index.php?title=User:Frozen_Wind http://en.wikibooks.org/w/index.php?title=User:Guanaco http://en.wikibooks.org/w/index.php?title=User:Hannes_R%C3%B6st http://en.wikibooks.org/w/index.php?title=User:Istarlamanion http://en.wikibooks.org/w/index.php?title=User:JackPotte http://en.wikibooks.org/w/index.php?title=User:Jamesday http://en.wikibooks.org/w/index.php?title=User:Jomegat http://en.wikibooks.org/w/index.php?title=User:Lathspell http://en.wikibooks.org/w/index.php?title=User:Logictheo http://en.wikibooks.org/w/index.php?title=User:LucienPetit http://en.wikibooks.org/w/index.php?title=User:Marisap http://en.wikibooks.org/w/index.php?title=User:Matthew-RM http://en.wikibooks.org/w/index.php?title=User:Nitrodist http://en.wikibooks.org/w/index.php?title=User:Oliebollen

109

Contributors 1 1 2 1 28 1 14 Olli22 PeEll23 Pedromengas24 QuiteUnusual25 Sante Caserio26 Shanjan.sapra27 Tbe28

22 23 24 25 26 27 28

http://en.wikibooks.org/w/index.php?title=User:Olli http://en.wikibooks.org/w/index.php?title=User:PeEll http://en.wikibooks.org/w/index.php?title=User:Pedromengas http://en.wikibooks.org/w/index.php?title=User:QuiteUnusual http://en.wikibooks.org/w/index.php?title=User:Sante_Caserio http://en.wikibooks.org/w/index.php?title=User:Shanjan.sapra http://en.wikibooks.org/w/index.php?title=User:Tbe

110

List of Figures
GFDL: Gnu Free Documentation License. http://www.gnu.org/licenses/fdl. html cc-by-sa-3.0: Creative Commons Attribution ShareAlike 3.0 License. creativecommons.org/licenses/by-sa/3.0/ cc-by-sa-2.5: Creative Commons Attribution ShareAlike 2.5 License. creativecommons.org/licenses/by-sa/2.5/ cc-by-sa-2.0: Creative Commons Attribution ShareAlike 2.0 License. creativecommons.org/licenses/by-sa/2.0/ cc-by-sa-1.0: Creative Commons Attribution ShareAlike 1.0 License. creativecommons.org/licenses/by-sa/1.0/ http:// http:// http:// http://

cc-by-2.0: Creative Commons Attribution 2.0 License. http://creativecommons. org/licenses/by/2.0/ cc-by-2.0: Creative Commons Attribution 2.0 License. http://creativecommons. org/licenses/by/2.0/deed.en cc-by-2.5: Creative Commons Attribution 2.5 License. http://creativecommons. org/licenses/by/2.5/deed.en cc-by-3.0: Creative Commons Attribution 3.0 License. http://creativecommons. org/licenses/by/3.0/deed.en GPL: GNU General Public License. http://www.gnu.org/licenses/gpl-2.0. txt LGPL: GNU Lesser General Public License. http://www.gnu.org/licenses/lgpl. html PD: This image is in the public domain. ATTR: The copyright holder of this le allows anyone to use it for any purpose, provided that the copyright holder is properly attributed. Redistribution, derivative work, commercial use, and all other use is permitted. EURO: This is the common (reverse) face of a euro coin. The copyright on the design of the common face of the euro coins belongs to the European Commission. Authorised is reproduction in a format without relief (drawings, paintings, lms) provided they are not detrimental to the image of the euro. LFK: Lizenz Freie Kunst. http://artlibre.org/licence/lal/de CFR: Copyright free use.

111

List of Figures EPL: Eclipse Public License. epl-v10.php http://www.eclipse.org/org/documents/

Copies of the GPL, the LGPL as well as a GFDL are included in chapter Licenses29 . Please note that images in the public domain do not require attribution. You may click on the image numbers in the following table to open the webpage of the images in your webbrower.

29

Chapter 22 on page 115

112

List of Figures

1 2

GPL GPL

113

22 Licenses
22.1 GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007 Copyright 2007 Free Software Foundation, Inc. <http://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a programto make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers and authors protection, the GPL clearly explains that there is no warranty for this free software. For both users and authors sake, the GPL requires that modied versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modied versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modication follow. TERMS AND CONDITIONS 0. Denitions. This License refers to version 3 of the GNU General Public License. Copyright also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. The Program refers to any copyrightable work licensed under this License. Each licensee is addressed as you. Licensees and recipients may be individuals or organizations. To modify a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a modied version of the earlier work or a work based on the earlier work. A covered work means either the unmodied Program or a work based on the Program. To propagate a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modication), making available to the public, and in some countries other activities as well. To convey a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays Appropriate Legal Notices to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The source code for a work means the preferred form of the work for making modications to it. Object code means any non-source form of a work. A Standard Interface means an interface that either is an ofcial standard dened by a recognized standards body, or, in the case of interfaces specied for a particular programming language, one that is widely used among developers working in that language. The System Libraries of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A Major Component, in this context, means a major essential component (kernel, window system, and so on) of the specic operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The Corresponding Source for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those * a) The work must carry prominent notices stating that you modied it, and giving a relevant date. * b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modies the requirement in section 4 to keep intact all notices. * c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. * d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an aggregate if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilations users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: * a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source xed on a durable physical medium customarily used for software interchange. * b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. * c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. * d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to nd the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. * e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. activities. However, it does not include the works System Libraries, or general-purpose tools or generally available free programs which are used unmodied in performing those activities but which are not part of the work. For example, Corresponding Source includes interface denition les associated with source les for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specically designed to require, such as by intimate data communication or control ow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly afrms your unlimited permission to run the unmodied Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fullling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modication of the work as a means of enforcing, against the works users, your or third parties legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Programs source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modied Source Versions. You may convey a work based on the Program, or the modications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: A User Product is either (1) a consumer product, which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, normally used refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only signicant mode of use of the product. Installation Information for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modied versions of a covered work in that User Product from a modied version of its Corresponding Source. The information must sufce to ensure that the continued functioning of the modied object code is in no case prevented or interfered with solely because modication has been made. If you convey an object code work under this section in, or with, or specically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a xed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modied object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modied or installed by the recipient, or for the User Product in which it has been modied or installed. Access to a network may be denied when the modication itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. Additional permissions are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: * a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or * b) Requiring preservation of specied reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or * c) Prohibiting misrepresentation of the origin of that material, or requiring that modied versions of such material be marked in reasonable ways as different from the original version; or * d) Limiting the use for publicity purposes of names of licensors or authors of the material; or * e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or * f) Requiring indemnication of licensors and authors of that material by anyone who conveys the material (or modied versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered further restrictions within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source les, a statement of the additional terms that apply to those les, or a notice indicating where to nd the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and nally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder noties you of the violation by some reasonable means, this is the rst time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An entity transaction is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the partys predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or afrmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A contributor is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributors contributor version. A contributors essential patent claims are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modication of the contributor version. For purposes of this denition, control includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributors essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a patent license is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To grant such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benet of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. Knowingly relying means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipients use of the covered work in a country, would infringe one or more identiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specic copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is discriminatory if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specic products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program species that a certain numbered version of the GNU General Public License or any later version applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program species that a proxy can decide which future versions of the GNU General Public License can be used, that proxys public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright

holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR

LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source le to most effectively state the exclusion of warranty; and each le should have at least the copyright line and a pointer to where the full notice is found.

<one line to give the programs name and a brief idea of what it does.> Copyright (C) <year> <name of author> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode:

<program> Copyright (C) <year> <name of author> This program comes with ABSOLUTELY NO WARRANTY; for details type show w. This is free software, and you are welcome to redistribute it under certain conditions; type show c for details.

The hypothetical commands show w and show c should show the appropriate parts of the General Public License. Of course, your programs commands might be different; for a GUI interface, you would use an about box.

You should also get your employer (if you work as a programmer) or school, if any, to sign a copyright disclaimer for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see <http://www.gnu.org/licenses/>.

The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But rst, please read <http://www.gnu.org/philosophy/why-not-lgpl.html>.

22.2 GNU Free Documentation License


Version 1.3, 3 November 2008 Copyright 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc. <http://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. 0. PREAMBLE The purpose of this License is to make a manual, textbook, or other functional and useful document "free" in the sense of freedom: to assure everyone the effective freedom to copy and redistribute it, with or without modifying it, either commercially or noncommercially. Secondarily, this License preserves for the author and publisher a way to get credit for their work, while not being considered responsible for modications made by others. This License is a kind of "copyleft", which means that derivative works of the document must themselves be free in the same sense. It complements the GNU General Public License, which is a copyleft license designed for free software. We have designed this License in order to use it for manuals for free software, because free software needs free documentation: a free program should come with manuals providing the same freedoms that the software does. But this License is not limited to software manuals; it can be used for any textual work, regardless of subject matter or whether it is published as a printed book. We recommend this License principally for works whose purpose is instruction or reference. 1. APPLICABILITY AND DEFINITIONS This License applies to any manual or other work, in any medium, that contains a notice placed by the copyright holder saying it can be distributed under the terms of this License. Such a notice grants a world-wide, royalty-free license, unlimited in duration, to use that work under the conditions stated herein. The "Document", below, refers to any such manual or work. Any member of the public is a licensee, and is addressed as "you". You accept the license if you copy, modify or distribute the work in a way requiring permission under copyright law. A "Modied Version" of the Document means any work containing the Document or a portion of it, either copied verbatim, or with modications and/or translated into another language. A "Secondary Section" is a named appendix or a front-matter section of the Document that deals exclusively with the relationship of the publishers or authors of the Document to the Documents overall subject (or to related matters) and contains nothing that could fall directly within that overall subject. (Thus, if the Document is in part a textbook of mathematics, a Secondary Section may not explain any mathematics.) The relationship could be a matter of historical connection with the subject or with related matters, or of legal, commercial, philosophical, ethical or political position regarding them. The "Invariant Sections" are certain Secondary Sections whose titles are designated, as being those of Invariant Sections, in the notice that says that the Document is released under this License. If a section does not t the above denition of Secondary then it is not allowed to be designated as Invariant. The Document may contain zero Invariant Sections. If the Document does not identify any Invariant Sections then there are none. The "Cover Texts" are certain short passages of text that are listed, as FrontCover Texts or Back-Cover Texts, in the notice that says that the Document is released under this License. A Front-Cover Text may be at most 5 words, and a Back-Cover Text may be at most 25 words. A "Transparent" copy of the Document means a machine-readable copy, represented in a format whose specication is available to the general public, that is suitable for revising the document straightforwardly with generic text editors or (for images composed of pixels) generic paint programs or (for drawings) some widely available drawing editor, and that is suitable for input to text formatters or for automatic translation to a variety of formats suitable for input to text formatters. A copy made in an otherwise Transparent le format whose markup, or absence of markup, has been arranged to thwart or discourage subsequent modication by readers is not Transparent. An image format is not Transparent if used for any substantial amount of text. A copy that is not "Transparent" is called "Opaque". Examples of suitable formats for Transparent copies include plain ASCII without markup, Texinfo input format, LaTeX input format, SGML or XML using a publicly available DTD, and standard-conforming simple HTML, PostScript or PDF designed for human modication. Examples of transparent image formats include PNG, XCF and JPG. Opaque formats include proprietary formats that can be read and edited only by proprietary word processors, SGML or XML for which the DTD and/or processing tools are not generally available, and the machine-generated HTML, PostScript or PDF produced by some word processors for output purposes only. The "Title Page" means, for a printed book, the title page itself, plus such following pages as are needed to hold, legibly, the material this License requires to appear in the title page. For works in formats which do not have any title page as such, "Title Page" means the text near the most prominent appearance of the works title, preceding the beginning of the body of the text. The "publisher" means any person or entity that distributes copies of the Document to the public. A section "Entitled XYZ" means a named subunit of the Document whose title either is precisely XYZ or contains XYZ in parentheses following text that translates XYZ in another language. (Here XYZ stands for a specic section name mentioned below, such as "Acknowledgements", "Dedications", "Endorsements", or "History".) To "Preserve the Title" of such a section when you modify the Document means that it remains a section "Entitled XYZ" according to this denition. The Document may include Warranty Disclaimers next to the notice which states that this License applies to the Document. These Warranty Disclaimers are considered to be included by reference in this License, but only as regards disclaiming warranties: any other implication that these Warranty Disclaimers may have is void and has no effect on the meaning of this License. 2. VERBATIM COPYING You may copy and distribute the Document in any medium, either commercially or noncommercially, provided that this License, the copyright notices, and the license notice saying this License applies to the Document are reproduced in all copies, and that you add no other conditions whatsoever to those of this License. You may not use technical measures to obstruct or control the reading or further copying of the copies you make or distribute. However, you may accept compensation in exchange for copies. If you distribute a large enough number of copies you must also follow the conditions in section 3. You may also lend copies, under the same conditions stated above, and you may publicly display copies. 3. COPYING IN QUANTITY If you publish printed copies (or copies in media that commonly have printed covers) of the Document, numbering more than 100, and the Documents license notice requires Cover Texts, you must enclose the copies in covers that carry, clearly and legibly, all these Cover Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on the back cover. Both covers must also clearly and legibly identify you as the publisher of these copies. The front cover must present the full title with all words of the title equally prominent and visible. You may add other material on the covers in addition. Copying with changes limited to the covers, as long as they preserve the title of the Document and satisfy these conditions, can be treated as verbatim copying in other respects. If the required texts for either cover are too voluminous to t legibly, you should put the rst ones listed (as many as t reasonably) on the actual cover, and continue the rest onto adjacent pages. If you publish or distribute Opaque copies of the Document numbering more than 100, you must either include a machine-readable Transparent copy along with each Opaque copy, or state in or with each Opaque copy a computer-network location from which the general network-using public has access to download using public-standard network protocols a complete Transparent copy of the Document, free of added material. If you use the latter option, you must take reasonably prudent steps, when you begin distribution of Opaque copies in quantity, to ensure that this Transparent copy will remain thus accessible at the stated location until at least one year after the last time you distribute an Opaque copy (directly or through your agents or retailers) of that edition to the public. It is requested, but not required, that you contact the authors of the Document well before redistributing any large number of copies, to give them a chance to provide you with an updated version of the Document. 4. MODIFICATIONS You may copy and distribute a Modied Version of the Document under the conditions of sections 2 and 3 above, provided that you release the Modied Version under precisely this License, with the Modied Version lling the role of the Document, thus licensing distribution and modication of the Modied Version to whoever possesses a copy of it. In addition, you must do these things in the Modied Version: * A. Use in the Title Page (and on the covers, if any) a title distinct from that of the Document, and from those of previous versions (which should, if there were any, be listed in the History section of the Document). You may use the same title as a previous version if the original publisher of that version gives permission. * B. List on the Title Page, as authors, one or more persons or entities responsible for authorship of the modications in the Modied Version, together with at least ve of the principal authors of the Document (all of its principal authors, if it has fewer than ve), unless they release you from this requirement. * C. State on the Title page the name of the publisher of the Modied Version, as the publisher. * D. Preserve all the copyright notices of the Document. * E. Add an appropriate copyright notice for your modications adjacent to the other copyright notices. * F. Include, immediately after the copyright notices, a license notice giving the public permission to use the Modied Version under the terms of this License, in the form shown in the Addendum below. * G. Preserve in that license notice the full lists of Invariant Sections and required Cover Texts given in the Documents license notice. * H. Include an unaltered copy of this License. * I. Preserve the section Entitled "History", Preserve its Title, and add to it an item stating at least the title, year, new authors, and publisher of the Modied Version as given on the Title Page. If there is no section Entitled "History" in the Document, create one stating the title, year, authors, and publisher of the Document as given on its Title Page, then add an item describing the Modied Version as stated in the previous sentence. * J. Preserve the network location, if any, given in the Document for public access to a Transparent copy of the Document, and likewise the network locations given in the Document for previous versions it was based on. These may be placed in the "History" section. You may omit a network location for a work that was published at least four years before the Document itself, or if the original publisher of the version it refers to gives permission. * K. For any section Entitled "Acknowledgements" or "Dedications", Preserve the Title of the section, and preserve in the section all the substance and tone of each of the contributor acknowledgements and/or dedications given therein. * L. Preserve all the Invariant Sections of the Document, unaltered in their text and in their titles. Section numbers or the equivalent are not considered part of the section titles. * M. Delete any section Entitled "Endorsements". Such a section may not be included in the Modied Version. * N. Do not retitle any existing section to be Entitled "Endorsements" or to conict in title with any Invariant Section. * O. Preserve any Warranty Disclaimers. If the Modied Version includes new front-matter sections or appendices that qualify as Secondary Sections and contain no material copied from the Document, you may at your option designate some or all of these sections as invariant. To do this, add their titles to the list of Invariant Sections in the Modied Versions license notice. These titles must be distinct from any other section titles. You may add a section Entitled "Endorsements", provided it contains nothing but endorsements of your Modied Version by various partiesfor example, statements of peer review or that the text has been approved by an organization as the authoritative denition of a standard. You may add a passage of up to ve words as a Front-Cover Text, and a passage of up to 25 words as a Back-Cover Text, to the end of the list of Cover Texts in the Modied Version. Only one passage of Front-Cover Text and one of Back-Cover Text may be added by (or through arrangements made by) any one entity. If the Document already includes a cover text for the same cover, previously added by you or by arrangement made by the same entity you are acting on behalf of, you may not add another; but you may replace the old one, on explicit permission from the previous publisher that added the old one. The author(s) and publisher(s) of the Document do not by this License give permission to use their names for publicity for or to assert or imply endorsement of any Modied Version. 5. COMBINING DOCUMENTS You may combine the Document with other documents released under this License, under the terms dened in section 4 above for modied versions, provided that you include in the combination all of the Invariant Sections of all of the original documents, unmodied, and list them all as Invariant Sections of your combined work in its license notice, and that you preserve all their Warranty Disclaimers. The combined work need only contain one copy of this License, and multiple identical Invariant Sections may be replaced with a single copy. If there are multiple Invariant Sections with the same name but different contents, make the title of each such section unique by adding at the end of it, in parentheses, the name of the original author or publisher of that section if known, or else a unique number. Make the same adjustment to the section titles in the list of Invariant Sections in the license notice of the combined work. In the combination, you must combine any sections Entitled "History" in the various original documents, forming one section Entitled "History"; likewise combine any sections Entitled "Acknowledgements", and any sections Entitled "Dedications". You must delete all sections Entitled "Endorsements". 6. COLLECTIONS OF DOCUMENTS You may make a collection consisting of the Document and other documents released under this License, and replace the individual copies of this License in the various documents with a single copy that is included in the collection, provided that you follow the rules of this License for verbatim copying of each of the documents in all other respects. You may extract a single document from such a collection, and distribute it individually under this License, provided you insert a copy of this License into the extracted document, and follow this License in all other respects regarding verbatim copying of that document. 7. AGGREGATION WITH INDEPENDENT WORKS A compilation of the Document or its derivatives with other separate and independent documents or works, in or on a volume of a storage or distribution medium, is called an "aggregate" if the copyright resulting from the compilation is not used to limit the legal rights of the compilations users beyond what the individual works permit. When the Document is included in an aggregate, this License does not apply to the other works in the aggregate which are not themselves derivative works of the Document. If the Cover Text requirement of section 3 is applicable to these copies of the Document, then if the Document is less than one half of the entire aggregate, the Documents Cover Texts may be placed on covers that bracket the Document within the aggregate, or the electronic equivalent of covers if the Document is in electronic form. Otherwise they must appear on printed covers that bracket the whole aggregate. 8. TRANSLATION Translation is considered a kind of modication, so you may distribute translations of the Document under the terms of section 4. Replacing Invariant Sections with translations requires special permission from their copyright holders, but you may include translations of some or all Invariant Sections in addition to the original versions of these Invariant Sections. You may include a translation of this License, and all the license notices in the Document, and any Warranty Disclaimers, provided that you also include the original English version of this License and the original versions of those notices and disclaimers. In case of a disagreement between the translation and the original version of this License or a notice or disclaimer, the original version will prevail. If a section in the Document is Entitled "Acknowledgements", "Dedications", or "History", the requirement (section 4) to Preserve its Title (section 1) will typically require changing the actual title. 9. TERMINATION You may not copy, modify, sublicense, or distribute the Document except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, or distribute it is void, and will automatically terminate your rights under this License. However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and nally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder noties you of the violation by some reasonable means, this is the rst time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, receipt of a copy of some or all of the same material does not give you any rights to use it. 10. FUTURE REVISIONS OF THIS LICENSE The Free Software Foundation may publish new, revised versions of the GNU Free Documentation License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. See http://www.gnu.org/copyleft/. Each version of the License is given a distinguishing version number. If the Document species that a particular numbered version of this License "or any later version" applies to it, you have the option of following the terms and conditions either of that specied version or of any later version that has been published (not as a draft) by the Free Software Foundation. If the Document does not specify a version number of this License, you may choose any version ever published (not as a draft) by the Free Software Foundation. If the Document species that a proxy can decide which future versions of this License can be used, that proxys public statement of acceptance of a version permanently authorizes you to choose that version for the Document. 11. RELICENSING "Massive Multiauthor Collaboration Site" (or "MMC Site") means any World Wide Web server that publishes copyrightable works and also provides prominent facilities for anybody to edit those works. A public wiki that anybody can edit is an example of such a server. A "Massive Multiauthor Collaboration" (or "MMC") contained in the site means any set of copyrightable works thus published on the MMC site. "CC-BY-SA" means the Creative Commons Attribution-Share Alike 3.0 license published by Creative Commons Corporation, a not-for-prot corporation with a principal place of business in San Francisco, California, as well as future copyleft versions of that license published by that same organization. "Incorporate" means to publish or republish a Document, in whole or in part, as part of another Document. An MMC is "eligible for relicensing" if it is licensed under this License, and if all works that were rst published under this License somewhere other than this MMC, and subsequently incorporated in whole or in part into the MMC, (1) had no cover texts or invariant sections, and (2) were thus incorporated prior to November 1, 2008. The operator of an MMC Site may republish an MMC contained in the site under CC-BY-SA on the same site at any time before August 1, 2009, provided the MMC is eligible for relicensing. ADDENDUM: How to use this License for your documents To use this License in a document you have written, include a copy of the License in the document and put the following copyright and license notices just after the title page: Copyright (C) YEAR YOUR NAME. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". If you have Invariant Sections, Front-Cover Texts and Back-Cover Texts, replace the "with . . . Texts." line with this: with the Invariant Sections being LIST THEIR TITLES, with the FrontCover Texts being LIST, and with the Back-Cover Texts being LIST. If you have Invariant Sections without Cover Texts, or some other combination of the three, merge those two alternatives to suit the situation. If your document contains nontrivial examples of program code, we recommend releasing these examples in parallel under your choice of free software license, such as the GNU General Public License, to permit their use in free software.

22.3 GNU Lesser General Public License


GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Denitions. The Library refers to a covered work governed by this License, other than an Application or a Combined Work as dened below. A Combined Work is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the Linked Version.

Copyright 2007 Free Software Foundation, Inc. <http://fsf.org/> As used herein, this License refers to version 3 of the GNU Lesser General Public License, and the GNU GPL refers to version 3 of the GNU General Public License. An Application is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Dening a subclass of a class dened by the Library is deemed a mode of using an interface provided by the Library. The Minimal Corresponding Source for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.

Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.

The Corresponding Application Code for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modied Versions. If you modify a copy of the Library, and, in your modications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modied version: * a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or * b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header le that is part of the Library. You may convey such object code

under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: * a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. * b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modication of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modications, if you also do each of the following: * a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. * b) Accompany the Combined Work with a copy of the GNU GPL and this license document. * c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. * d) Do one of the following: o 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms

that permit, the user to recombine or relink the Application with a modied version of the Linked Version to produce a modied Combined Work, in the manner specied by section 6 of the GNU GPL for conveying Corresponding Source. o 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the users computer system, and (b) will operate properly with a modied version of the Library that is interface-compatible with the Linked Version. * e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modied version of the Combined Work produced by recombining or relinking the Application with a modied version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specied by section 6 of the GNU GPL for conveying Corresponding Source.)

* a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. * b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to nd the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it species that a certain numbered version of the GNU Lesser General Public License or any later version applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it species that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxys public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.

5. Combined Libraries.

You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:

You might also like