1.6 MySQL Standards Compliance

This section describes how MySQL relates to the ANSI/ISO SQL standards. MySQL Server has many extensions to the SQL standard, and here you can find out what they are and how to use them. You can also find information about functionality missing from MySQL Server, and how to work around some of the differences.

The SQL standard has been evolving since 1986 and several versions exist. In this manual, 'SQL-92' refers to the standard released in 1992. 'SQL:1999', 'SQL:2003', 'SQL:2008', and 'SQL:2011' refer to the versions of the standard released in the corresponding years, with the last being the most recent version. We use the phrase 'the SQL standard' or 'standard SQL' to mean the current version of the SQL Standard at any time.

One of our main goals with the product is to continue to work toward compliance with the SQL standard, but without sacrificing speed or reliability. We are not afraid to add extensions to SQL or support for non-SQL features if this greatly increases the usability of MySQL Server for a large segment of our user base. The note 'HANDLER': handler. interface is an example of this strategy. See note handler::.

We continue to support transactional and nontransactional databases to satisfy both mission-critical 24/7 usage and heavy Web or logging usage.

MySQL Server was originally designed to work with medium-sized databases (10-100 million rows, or about 100MB per table) on small computer systems. Today MySQL Server handles terabyte-sized databases, but the code can also be compiled in a reduced version suitable for hand-held and embedded devices. The compact design of the MySQL server makes development in both directions possible without any conflicts in the source tree.

We are not targeting real-time support, although MySQL replication capabilities offer significant functionality.

MySQL supports ODBC levels 0 to 3.51.

MySQL supports high-availability database clustering using the note 'NDBCLUSTER': mysql-cluster. storage engine. See note mysql-cluster::.

We implement XML functionality which supports most of the W3C XPath standard. See *note xml-functions::.

MySQL (5.7.8 and later) supports a native JSON data type as defined by RFC 7159, and based on the ECMAScript standard (ECMA-262). See note json::. MySQL also implements a subset of the SQL/JSON functions specified by a pre-publication draft of the SQL:2016 standard; see note json-functions::, for more information.

Selecting SQL Modes

The MySQL server can operate in different SQL modes, and can apply these modes differently for different clients, depending on the value of the 'sql_mode' system variable. DBAs can set the global SQL mode to match site server operating requirements, and each application can set its session SQL mode to its own requirements.

Modes affect the SQL syntax MySQL supports and the data validation checks it performs. This makes it easier to use MySQL in different environments and to use MySQL together with other database servers.

For more information on setting the SQL mode, see *note sql-mode::.

Running MySQL in ANSI Mode

To run MySQL Server in ANSI mode, start *note 'mysqld': mysqld. with the '--ansi' option. Running the server in ANSI mode is the same as starting it with the following options:

 --transaction-isolation=SERIALIZABLE --sql-mode=ANSI

To achieve the same effect at runtime, execute these two statements:

 SET GLOBAL TRANSACTION ISOLATION LEVEL SERIALIZABLE;
 SET GLOBAL sql_mode = 'ANSI';

You can see that setting the 'sql_mode' system variable to ''ANSI'' enables all SQL mode options that are relevant for ANSI mode as follows:

 mysql> SET GLOBAL sql_mode='ANSI';
 mysql> SELECT @@GLOBAL.sql_mode;
         -> 'REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ANSI'

Running the server in ANSI mode with '--ansi' is not quite the same as setting the SQL mode to ''ANSI'' because the '--ansi' option also sets the transaction isolation level.

See *note server-options::.

 File: manual.info.tmp, Node: extensions-to-ansi, Next: differences-from-ansi, Prev: compatibility, Up: compatibility

1.6.1 MySQL Extensions to Standard SQL

MySQL Server supports some extensions that are likely not to be found in other SQL DBMSs. Be warned that if you use them, your code is not portable to other SQL servers. In some cases, you can write code that includes MySQL extensions, but is still portable, by using comments of the following form:

 /*! MYSQL-SPECIFIC CODE */

In this case, MySQL Server parses and executes the code within the comment as it would any other SQL statement, but other SQL servers ignore the extensions. For example, MySQL Server recognizes the 'STRAIGHT_JOIN' keyword in the following statement, but other servers do not:

 SELECT /*! STRAIGHT_JOIN */ col1 FROM table1,table2 WHERE ...

If you add a version number after the '!' character, the syntax within the comment is executed only if the MySQL version is greater than or equal to the specified version number. The 'KEY_BLOCK_SIZE' clause in the following comment is executed only by servers from MySQL 5.1.10 or higher:

 CREATE TABLE t1(a INT, KEY (a)) /*!50110 KEY_BLOCK_SIZE=1024 */;

The following descriptions list MySQL extensions, organized by category.

 File: manual.info.tmp, Node: differences-from-ansi, Next: constraints, Prev: extensions-to-ansi, Up: compatibility

1.6.2 MySQL Differences from Standard SQL

We try to make MySQL Server follow the ANSI SQL standard and the ODBC SQL standard, but MySQL Server performs operations differently in some cases:

 File: manual.info.tmp, Node: ansi-diff-select-into-table, Next: ansi-diff-update, Prev: differences-from-ansi, Up: differences-from-ansi

1.6.2.1 SELECT INTO TABLE Differences .....................................

MySQL Server does not support the 'SELECT ... INTO TABLE' Sybase SQL extension. Instead, MySQL Server supports the note 'INSERT INTO ... SELECT': insert-select. standard SQL syntax, which is basically the same thing. See note insert-select::. For example:

 INSERT INTO tbl_temp2 (fld_id)
     SELECT tbl_temp1.fld_order_id
     FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

Alternatively, you can use note 'SELECT ... INTO OUTFILE': select-into. or note 'CREATE TABLE ... SELECT': create-table.

You can use note 'SELECT ... INTO': select. with user-defined variables. The same syntax can also be used inside stored routines using cursors and local variables. See note select-into::.

 File: manual.info.tmp, Node: ansi-diff-update, Next: ansi-diff-foreign-keys, Prev: ansi-diff-select-into-table, Up: differences-from-ansi

1.6.2.2 UPDATE Differences ..........................

If you access a column from the table to be updated in an expression, *note 'UPDATE': update. uses the current value of the column. The second assignment in the following statement sets 'col2' to the current (updated) 'col1' value, not the original 'col1' value. The result is that 'col1' and 'col2' have the same value. This behavior differs from standard SQL.

 UPDATE t1 SET col1 = col1 + 1, col2 = col1;

 File: manual.info.tmp, Node: ansi-diff-foreign-keys, Next: ansi-diff-comments, Prev: ansi-diff-update, Up: differences-from-ansi

1.6.2.3 FOREIGN KEY Constraint Differences ..........................................

The MySQL implementation of foreign key constraints differs from the SQL standard in the following key respects:

For information about foreign key constraints, see *note create-table-foreign-keys::.

 File: manual.info.tmp, Node: ansi-diff-comments, Prev: ansi-diff-foreign-keys, Up: differences-from-ansi

1.6.2.4 '-' as the Start of a Comment .....................................

Standard SQL uses the C syntax '/* this is a comment /' for comments, and MySQL Server supports this syntax as well. MySQL also support extensions to this syntax that enable MySQL-specific SQL to be embedded in the comment; see note comments::.

MySQL Server also uses '#' as the start comment character. This is nonstandard.

Standard SQL also uses ''--'' as a start-comment sequence. MySQL Server supports a variant of the '--' comment style; the '--' start-comment sequence is accepted as such, but must be followed by a whitespace character such as a space or newline. The space is intended to prevent problems with generated SQL queries that use constructs such as the following, which updates the balance to reflect a charge:

 UPDATE account SET balance=balance-charge
 WHERE account_id=user_id

Consider what happens when 'charge' has a negative value such as '-1', which might be the case when an amount is credited to the account. In this case, the generated statement looks like this:

 UPDATE account SET balance=balance--1
 WHERE account_id=5752;

'balance--1' is valid standard SQL, but '--' is interpreted as the start of a comment, and part of the expression is discarded. The result is a statement that has a completely different meaning than intended:

 UPDATE account SET balance=balance
 WHERE account_id=5752;

This statement produces no change in value at all. To keep this from happening, MySQL requires a whitespace character following the '--' for it to be recognized as a start-comment sequence in MySQL Server, so that an expression such as 'balance--1' is always safe to use.

 File: manual.info.tmp, Node: constraints, Prev: differences-from-ansi, Up: compatibility

1.6.3 How MySQL Deals with Constraints

MySQL enables you to work both with transactional tables that permit rollback and with nontransactional tables that do not. Because of this, constraint handling is a bit different in MySQL than in other DBMSs. We must handle the case when you have inserted or updated a lot of rows in a nontransactional table for which changes cannot be rolled back when an error occurs.

The basic philosophy is that MySQL Server tries to produce an error for anything that it can detect while parsing a statement to be executed, and tries to recover from any errors that occur while executing the statement. We do this in most cases, but not yet for all.

The options MySQL has when an error occurs are to stop the statement in the middle or to recover as well as possible from the problem and continue. By default, the server follows the latter course. This means, for example, that the server may coerce invalid values to the closest valid values.

Several SQL mode options are available to provide greater control over handling of bad data values and whether to continue statement execution or abort when errors occur. Using these options, you can configure MySQL Server to act in a more traditional fashion that is like other DBMSs that reject improper input. The SQL mode can be set globally at server startup to affect all clients. Individual clients can set the SQL mode at runtime, which enables each client to select the behavior most appropriate for its requirements. See *note sql-mode::.

The following sections describe how MySQL Server handles different types of constraints.

 File: manual.info.tmp, Node: constraint-primary-key, Next: constraint-foreign-key, Prev: constraints, Up: constraints

1.6.3.1 PRIMARY KEY and UNIQUE Index Constraints ................................................

Normally, errors occur for data-change statements (such as note 'INSERT': insert. or note 'UPDATE': update.) that would violate primary-key, unique-key, or foreign-key constraints. If you are using a transactional storage engine such as 'InnoDB', MySQL automatically rolls back the statement. If you are using a nontransactional storage engine, MySQL stops processing the statement at the row for which the error occurred and leaves any remaining rows unprocessed.

MySQL supports an 'IGNORE' keyword for note 'INSERT': insert, note 'UPDATE': update, and so forth. If you use it, MySQL ignores primary-key or unique-key violations and continues processing with the next row. See the section for the statement that you are using (note insert::, note update::, and so forth).

You can get information about the number of rows actually inserted or updated with the 'mysql_info()' (https://dev.mysql.com/doc/c-api/5.7/en/mysql-info.html) C API function. You can also use the note 'SHOW WARNINGS': show-warnings. statement. See mysql_info() (https://dev.mysql.com/doc/c-api/5.7/en/mysql-info.html), and note show-warnings::.

'InnoDB' and 'NDB' tables support foreign keys. See *note constraint-foreign-key::.

 File: manual.info.tmp, Node: constraint-foreign-key, Next: constraint-invalid-data, Prev: constraint-primary-key, Up: constraints

1.6.3.2 FOREIGN KEY Constraints ...............................

Foreign keys let you cross-reference related data across tables, and foreign key constraints help keep this spread-out data consistent.

MySQL supports 'ON UPDATE' and 'ON DELETE' foreign key references in note 'CREATE TABLE': create-table. and note 'ALTER TABLE': alter-table. statements. The available referential actions are 'RESTRICT' (the default), 'CASCADE', 'SET NULL', and 'NO ACTION'.

'SET DEFAULT' is also supported by the MySQL Server but is currently rejected as invalid by note 'InnoDB': innodb-storage-engine. Since MySQL does not support deferred constraint checking, 'NO ACTION' is treated as 'RESTRICT'. For the exact syntax supported by MySQL for foreign keys, see note create-table-foreign-keys::.

'MATCH FULL', 'MATCH PARTIAL', and 'MATCH SIMPLE' are allowed, but their use should be avoided, as they cause the MySQL Server to ignore any 'ON DELETE' or 'ON UPDATE' clause used in the same statement. 'MATCH' options do not have any other effect in MySQL, which in effect enforces 'MATCH SIMPLE' semantics full-time.

MySQL requires that foreign key columns be indexed; if you create a table with a foreign key constraint but no index on a given column, an index is created.

You can obtain information about foreign keys from the Information Schema *note 'KEY_COLUMN_USAGE': information-schema-key-column-usage-table. table. An example of a query against this table is shown here:

 mysql> SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME
      > FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
      > WHERE REFERENCED_TABLE_SCHEMA IS NOT NULL;
 +--------------+---------------+-------------+-----------------+
 | TABLE_SCHEMA | TABLE_NAME    | COLUMN_NAME | CONSTRAINT_NAME |
 +--------------+---------------+-------------+-----------------+
 | fk1          | myuser        | myuser_id   | f               |
 | fk1          | product_order | customer_id | f2              |
 | fk1          | product_order | product_id  | f1              |
 +--------------+---------------+-------------+-----------------+
 3 rows in set (0.01 sec)

Information about foreign keys on 'InnoDB' tables can also be found in the note 'INNODB_SYS_FOREIGN': information-schema-innodb-sys-foreign-table. and note 'INNODB_SYS_FOREIGN_COLS': information-schema-innodb-sys-foreign-cols-table. tables, in the 'INFORMATION_SCHEMA' database.

'InnoDB' and 'NDB' tables support foreign keys.

 File: manual.info.tmp, Node: constraint-invalid-data, Next: constraint-enum, Prev: constraint-foreign-key, Up: constraints

1.6.3.3 Constraints on Invalid Data ...................................

MySQL 5.7.5 and later uses strict SQL mode by default, which treats invalid values such that the server rejects them and aborts the statement in which they occur (see *note sql-mode::). Previously, MySQL was much more forgiving of incorrect values used in data entry; this now requires disabling of strict mode, which is not recommended. The remainder of this section discusses the old behavior followed by MySQL when strict mode has been disabled.

If you are not using strict mode, then whenever you insert an 'incorrect' value into a column, such as a 'NULL' into a 'NOT NULL' column or a too-large numeric value into a numeric column, MySQL sets the column to the 'best possible value' instead of producing an error: The following rules describe in more detail how this works:

The reason for using the preceding rules when strict mode is not in effect is that we cannot check these conditions until the statement has begun executing. We cannot just roll back if we encounter a problem after updating a few rows, because the storage engine may not support rollback. The option of terminating the statement is not that good; in this case, the update would be 'half done,' which is probably the worst possible scenario. In this case, it is better to 'do the best you can' and then continue as if nothing happened.

You can select stricter treatment of input values by using the 'STRICT_TRANS_TABLES' or 'STRICT_ALL_TABLES' SQL modes:

 SET sql_mode = 'STRICT_TRANS_TABLES';
 SET sql_mode = 'STRICT_ALL_TABLES';

'STRICT_TRANS_TABLES' enables strict mode for transactional storage engines, and also to some extent for nontransactional engines. It works like this:

For even stricter checking, enable 'STRICT_ALL_TABLES'. This is the same as 'STRICT_TRANS_TABLES' except that for nontransactional storage engines, errors abort the statement even for bad data in rows following the first row. This means that if an error occurs partway through a multiple-row insert or update for a nontransactional table, a partial update results. Earlier rows are inserted or updated, but those from the point of the error on are not. To avoid this for nontransactional tables, either use single-row statements or else use 'STRICT_TRANS_TABLES' if conversion warnings rather than errors are acceptable. To avoid problems in the first place, do not use MySQL to check column content. It is safest (and often faster) to let the application ensure that it passes only valid values to the database.

With either of the strict mode options, you can cause errors to be treated as warnings by using note 'INSERT IGNORE': insert. or 'UPDATE IGNORE' rather than note 'INSERT': insert. or *note 'UPDATE': update. without 'IGNORE'.

 File: manual.info.tmp, Node: constraint-enum, Prev: constraint-invalid-data, Up: constraints

1.6.3.4 ENUM and SET Constraints ................................

note 'ENUM': enum. and note 'SET': set. columns provide an efficient way to define columns that can contain only a given set of values. See note enum::, and note set::.

Unless strict mode is disabled (not recommended, but see note sql-mode::), the definition of a note 'ENUM': enum. or *note 'SET': set. column acts as a constraint on values entered into the column. An error occurs for values that do not satisfy these conditions:

Errors for invalid values can be suppressed in strict mode if you use note 'INSERT IGNORE': insert. or 'UPDATE IGNORE'. In this case, a warning is generated rather than an error. For note 'ENUM': enum, the value is inserted as the error member ('0'). For *note 'SET': set, the value is inserted as given except that any invalid substrings are deleted. For example, ''a,x,b,y'' results in a value of ''a,b''.

 File: manual.info.tmp, Node: installing, Next: tutorial, Prev: introduction, Up: Top

2 Installing and Upgrading MySQL ********************************

This chapter describes how to obtain and install MySQL. A summary of the procedure follows and later sections provide the details. If you plan to upgrade an existing version of MySQL to a newer version rather than install MySQL for the first time, see *note upgrading::, for information about upgrade procedures and about issues that you should consider before upgrading.

If you are interested in migrating to MySQL from another database system, see *note faqs-migration::, which contains answers to some common questions concerning migration issues.

Installation of MySQL generally follows the steps outlined here:

  1. Determine whether MySQL runs and is supported on your platform.

    Please note that not all platforms are equally suitable for running MySQL, and that not all platforms on which MySQL is known to run are officially supported by Oracle Corporation. For information about those platforms that are officially supported, see https://www.mysql.com/support/supportedplatforms/database.html on the MySQL website.

  2. Choose which distribution to install.

    Several versions of MySQL are available, and most are available in several distribution formats. You can choose from pre-packaged distributions containing binary (precompiled) programs or source code. When in doubt, use a binary distribution. Oracle also provides access to the MySQL source code for those who want to see recent developments and test new code. To determine which version and type of distribution you should use, see *note which-version::.

  3. Download the distribution that you want to install.

    For instructions, see note getting-mysql::. To verify the integrity of the distribution, use the instructions in note verifying-package-integrity::.

  4. Install the distribution.

    To install MySQL from a binary distribution, use the instructions in *note binary-installation::. Alternatively, use the Secure Deployment Guide (https://dev.mysql.com/doc/mysql-secure-deployment-guide/5.7/en/), which provides procedures for deploying a generic binary distribution of MySQL Enterprise Edition Server with features for managing the security of your MySQL installation.

    To install MySQL from a source distribution or from the current development source tree, use the instructions in *note source-installation::.

  5. Perform any necessary postinstallation setup.

    After installing MySQL, see note postinstallation:: for information about making sure the MySQL server is working properly. Also refer to the information provided in note default-privileges::. This section describes how to secure the initial MySQL 'root' user account, which has no password until you assign one. The section applies whether you install MySQL using a binary or source distribution.

  6. If you want to run the MySQL benchmark scripts, Perl support for MySQL must be available. See *note perl-support::.

Instructions for installing MySQL on different platforms and environments is available on a platform by platform basis:

 File: manual.info.tmp, Node: general-installation-issues, Next: binary-installation, Prev: installing, Up: installing