Menu:
truncate-table:: TRUNCATE TABLE Statement
File: manual.info.tmp, Node: alter-database, Next: alter-event, Prev: sql-data-definition-statements, Up: sql-data-definition-statements
ALTER {DATABASE | SCHEMA} [DB_NAME]
ALTER_OPTION ...
ALTER {DATABASE | SCHEMA} DB_NAME
UPGRADE DATA DIRECTORY NAME
ALTER_OPTION: {
[DEFAULT] CHARACTER SET [=] CHARSET_NAME
| [DEFAULT] COLLATE [=] COLLATION_NAME
}
note 'ALTER DATABASE': alter-database. enables you to change the overall characteristics of a database. These characteristics are stored in the 'db.opt' file in the database directory. This statement requires the 'ALTER' privilege on the database. note 'ALTER SCHEMA': alter-database. is a synonym for *note 'ALTER DATABASE': alter-database.
The database name can be omitted from the first syntax, in which case the statement applies to the default database. An error occurs if there is no default database.
*note alter-database-charset::
*note alter-database-upgrading::
Character Set and Collation Options
The 'CHARACTER SET' clause changes the default database character set. The 'COLLATE' clause changes the default database collation. For information about character set and collation names, see *note charset::.
To see the available character sets and collations, use the note 'SHOW CHARACTER SET': show-character-set. and note 'SHOW COLLATION': show-collation. statements, respectively. See note show-character-set::, and note show-collation::.
A stored routine that uses the database defaults when the routine is created includes those defaults as part of its definition. (In a stored routine, variables with character data types use the database defaults if the character set or collation are not specified explicitly. See *note create-procedure::.) If you change the default character set or collation for a database, any stored routines that are to use the new defaults must be dropped and recreated.
Upgrading from Versions Older than MySQL 5.1
The syntax that includes the 'UPGRADE DATA DIRECTORY NAME' clause updates the name of the directory associated with the database to use the encoding implemented in MySQL 5.1 for mapping database names to database directory names (see *note identifier-mapping::). This clause is for use under these conditions:
It is intended when upgrading MySQL to 5.1 or later from older versions.
It is intended to update a database directory name to the current encoding format if the name contains special characters that need encoding.
The statement is used by note 'mysqlcheck': mysqlcheck. (as invoked by note 'mysql_upgrade': mysql-upgrade.).
For example, if a database in MySQL 5.0 has the name 'a-b-c', the name contains instances of the '-' (dash) character. In MySQL 5.0, the database directory is also named 'a-b-c', which is not necessarily safe for all file systems. In MySQL 5.1 and later, the same database name is encoded as 'a@002db@002dc' to produce a file system-neutral directory name.
When a MySQL installation is upgraded to MySQL 5.1 or later from an older version,the server displays a name such as 'a-b-c' (which is in the old format) as '#mysql50#a-b-c', and you must refer to the name using the '#mysql50#' prefix. Use 'UPGRADE DATA DIRECTORY NAME' in this case to explicitly tell the server to re-encode the database directory name to the current encoding format:
ALTER DATABASE `#mysql50#a-b-c` UPGRADE DATA DIRECTORY NAME;
After executing this statement, you can refer to the database as 'a-b-c' without the special '#mysql50#' prefix.
Note:
The 'UPGRADE DATA DIRECTORY NAME' clause is deprecated in MySQL 5.7 and removed in MySQL 8.0. If it is necessary to convert MySQL 5.0 database or table names, a workaround is to upgrade a MySQL 5.0 installation to MySQL 5.1 before upgrading to MySQL 8.0.
File: manual.info.tmp, Node: alter-event, Next: alter-function, Prev: alter-database, Up: sql-data-definition-statements
ALTER
[DEFINER = USER]
EVENT EVENT_NAME
[ON SCHEDULE SCHEDULE]
[ON COMPLETION [NOT] PRESERVE]
[RENAME TO NEW_EVENT_NAME]
[ENABLE | DISABLE | DISABLE ON SLAVE]
[COMMENT 'STRING']
[DO EVENT_BODY]
The note 'ALTER EVENT': alter-event. statement changes one or more of the characteristics of an existing event without the need to drop and recreate it. The syntax for each of the 'DEFINER', 'ON SCHEDULE', 'ON COMPLETION', 'COMMENT', 'ENABLE' / 'DISABLE', and note 'DO': do. clauses is exactly the same as when used with note 'CREATE EVENT': create-event. (See note create-event::.)
Any user can alter an event defined on a database for which that user has the 'EVENT' privilege. When a user executes a successful *note 'ALTER EVENT': alter-event. statement, that user becomes the definer for the affected event.
*note 'ALTER EVENT': alter-event. works only with an existing event:
mysql> ALTER EVENT no_such_event
> ON SCHEDULE
> EVERY '2:3' DAY_HOUR;
ERROR 1517 (HY000): Unknown event 'no_such_event'
In each of the following examples, assume that the event named 'myevent' is defined as shown here:
CREATE EVENT myevent
ON SCHEDULE
EVERY 6 HOUR
COMMENT 'A sample comment.'
DO
UPDATE myschema.mytable SET mycol = mycol + 1;
The following statement changes the schedule for 'myevent' from once every six hours starting immediately to once every twelve hours, starting four hours from the time the statement is run:
ALTER EVENT myevent
ON SCHEDULE
EVERY 12 HOUR
STARTS CURRENT_TIMESTAMP + INTERVAL 4 HOUR;
It is possible to change multiple characteristics of an event in a single statement. This example changes the SQL statement executed by 'myevent' to one that deletes all records from 'mytable'; it also changes the schedule for the event such that it executes once, one day after this *note 'ALTER EVENT': alter-event. statement is run.
ALTER EVENT myevent
ON SCHEDULE
AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
DO
TRUNCATE TABLE myschema.mytable;
Specify the options in an note 'ALTER EVENT': alter-event. statement only for those characteristics that you want to change; omitted options keep their existing values. This includes any default values for note 'CREATE EVENT': create-event. such as 'ENABLE'.
To disable 'myevent', use this *note 'ALTER EVENT': alter-event. statement:
ALTER EVENT myevent
DISABLE;
The 'ON SCHEDULE' clause may use expressions involving built-in MySQL functions and user variables to obtain any of the TIMESTAMP or INTERVAL values which it contains. You cannot use stored routines or loadable functions in such expressions, and you cannot use any table references; however, you can use 'SELECT FROM DUAL'. This is true for both note 'ALTER EVENT': alter-event. and note 'CREATE EVENT': create-event. statements. References to stored routines, loadable functions, and tables in such cases are specifically not permitted, and fail with an error (see Bug #22830).
Although an note 'ALTER EVENT': alter-event. statement that contains another note 'ALTER EVENT': alter-event. statement in its *note 'DO': do. clause appears to succeed, when the server attempts to execute the resulting scheduled event, the execution fails with an error.
To rename an event, use the *note 'ALTER EVENT': alter-event. statement's 'RENAME TO' clause. This statement renames the event 'myevent' to 'yourevent':
ALTER EVENT myevent
RENAME TO yourevent;
You can also move an event to a different database using 'ALTER EVENT ... RENAME TO ...' and 'DB_NAME.EVENT_NAME' notation, as shown here:
ALTER EVENT olddb.myevent
RENAME TO newdb.myevent;
To execute the previous statement, the user executing it must have the 'EVENT' privilege on both the 'olddb' and 'newdb' databases.
Note:
There is no 'RENAME EVENT' statement.
The value 'DISABLE ON SLAVE' is used on a replica instead of 'ENABLE' or 'DISABLE' to indicate an event that was created on the source and replicated to the replica, but that is not executed on the replica. Normally, 'DISABLE ON SLAVE' is set automatically as required; however, there are some circumstances under which you may want or need to change it manually. See *note replication-features-invoked::, for more information.
File: manual.info.tmp, Node: alter-function, Next: alter-instance, Prev: alter-event, Up: sql-data-definition-statements
ALTER FUNCTION FUNC_NAME [CHARACTERISTIC ...]
CHARACTERISTIC: {
COMMENT 'STRING'
| LANGUAGE SQL
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
}
This statement can be used to change the characteristics of a stored function. More than one change may be specified in an note 'ALTER FUNCTION': alter-function. statement. However, you cannot change the parameters or body of a stored function using this statement; to make such changes, you must drop and re-create the function using note 'DROP FUNCTION': drop-function. and *note 'CREATE FUNCTION': create-function.
You must have the 'ALTER ROUTINE' privilege for the function. (That privilege is granted automatically to the function creator.) If binary logging is enabled, the note 'ALTER FUNCTION': alter-function. statement might also require the 'SUPER' privilege, as described in note stored-programs-logging::.
File: manual.info.tmp, Node: alter-instance, Next: alter-logfile-group, Prev: alter-function, Up: sql-data-definition-statements
ALTER INSTANCE ROTATE INNODB MASTER KEY
'ALTER INSTANCE', introduced in MySQL 5.7.11, defines actions applicable to a MySQL server instance. The statement supports these actions:
'ALTER INSTANCE ROTATE INNODB MASTER KEY'
This action rotates the master encryption key used for 'InnoDB' tablespace encryption. Key rotation requires the 'SUPER' privilege. To perform this action, a keyring plugin must be installed and configured. For instructions, see *note keyring::.
'ALTER INSTANCE ROTATE INNODB MASTER KEY' supports concurrent DML. However, it cannot be run concurrently with note 'CREATE TABLE ... ENCRYPTION': create-table. or note 'ALTER TABLE ... ENCRYPTION': alter-table. operations, and locks are taken to prevent conflicts that could arise from concurrent execution of these statements. If one of the conflicting statements is running, it must complete before another can proceed.
'ALTER INSTANCE' actions are written to the binary log so that they can be executed on replicated servers.
For additional 'ALTER INSTANCE ROTATE INNODB MASTER KEY' usage information, see note innodb-data-encryption::. For information about keyring plugins, see note keyring::.
File: manual.info.tmp, Node: alter-logfile-group, Next: alter-procedure, Prev: alter-instance, Up: sql-data-definition-statements
ALTER LOGFILE GROUP LOGFILE_GROUP
ADD UNDOFILE 'FILE_NAME'
[INITIAL_SIZE [=] SIZE]
[WAIT]
ENGINE [=] ENGINE_NAME
This statement adds an 'UNDO' file named 'FILE_NAME' to an existing log file group LOGFILE_GROUP. An *note 'ALTER LOGFILE GROUP': alter-logfile-group. statement has one and only one 'ADD UNDOFILE' clause. No 'DROP UNDOFILE' clause is currently supported.
Note:
All NDB Cluster Disk Data objects share the same namespace. This means that each Disk Data object must be uniquely named (and not merely each Disk Data object of a given type). For example, you cannot have a tablespace and an undo log file with the same name, or an undo log file and a data file with the same name.
The optional 'INITIAL_SIZE' parameter sets the 'UNDO' file's initial size in bytes; if not specified, the initial size defaults to 134217728 (128 MB). You may optionally follow SIZE with a one-letter abbreviation for an order of magnitude, similar to those used in 'my.cnf'. Generally, this is one of the letters 'M' (megabytes) or 'G' (gigabytes). (Bug #13116514, Bug #16104705, Bug #62858)
On 32-bit systems, the maximum supported value for 'INITIAL_SIZE' is 4294967296 (4 GB). (Bug #29186)
The minimum allowed value for 'INITIAL_SIZE' is 1048576 (1 MB). (Bug #29574)
Note:
'WAIT' is parsed but otherwise ignored. This keyword currently has no effect, and is intended for future expansion.
The 'ENGINE' parameter (required) determines the storage engine which is used by this log file group, with ENGINE_NAME being the name of the storage engine. Currently, the only accepted values for ENGINE_NAME are 'note 'NDBCLUSTER': mysql-cluster.' and 'note 'NDB': mysql-cluster.'. The two values are equivalent.
Here is an example, which assumes that the log file group 'lg_3' has already been created using note 'CREATE LOGFILE GROUP': create-logfile-group. (see note create-logfile-group::):
ALTER LOGFILE GROUP lg_3
ADD UNDOFILE 'undo_10.dat'
INITIAL_SIZE=32M
ENGINE=NDBCLUSTER;
When note 'ALTER LOGFILE GROUP': alter-logfile-group. is used with 'ENGINE = NDBCLUSTER' (alternatively, 'ENGINE = NDB'), an 'UNDO' log file is created on each NDB Cluster data node. You can verify that the 'UNDO' files were created and obtain information about them by querying the Information Schema note 'FILES': information-schema-files-table. table. For example:
mysql> SELECT FILE_NAME, LOGFILE_GROUP_NUMBER, EXTRA
-> FROM INFORMATION_SCHEMA.FILES
-> WHERE LOGFILE_GROUP_NAME = 'lg_3';
+-------------+----------------------+----------------+
| FILE_NAME | LOGFILE_GROUP_NUMBER | EXTRA |
+-------------+----------------------+----------------+
| newdata.dat | 0 | CLUSTER_NODE=3 |
| newdata.dat | 0 | CLUSTER_NODE=4 |
| undo_10.dat | 11 | CLUSTER_NODE=3 |
| undo_10.dat | 11 | CLUSTER_NODE=4 |
+-------------+----------------------+----------------+
4 rows in set (0.01 sec)
(See *note information-schema-files-table::.)
Memory used for 'UNDO_BUFFER_SIZE' comes from the global pool whose size is determined by the value of the 'SharedGlobalMemory' data node configuration parameter. This includes any default value implied for this option by the setting of the 'InitialLogFileGroup' data node configuration parameter.
note 'ALTER LOGFILE GROUP': alter-logfile-group. is useful only with Disk Data storage for NDB Cluster. For more information, see note mysql-cluster-disk-data::.
File: manual.info.tmp, Node: alter-procedure, Next: alter-server, Prev: alter-logfile-group, Up: sql-data-definition-statements
ALTER PROCEDURE PROC_NAME [CHARACTERISTIC ...]
CHARACTERISTIC: {
COMMENT 'STRING'
| LANGUAGE SQL
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
}
This statement can be used to change the characteristics of a stored procedure. More than one change may be specified in an note 'ALTER PROCEDURE': alter-procedure. statement. However, you cannot change the parameters or body of a stored procedure using this statement; to make such changes, you must drop and re-create the procedure using note 'DROP PROCEDURE': drop-procedure. and *note 'CREATE PROCEDURE': create-procedure.
You must have the 'ALTER ROUTINE' privilege for the procedure. By default, that privilege is granted automatically to the procedure creator. This behavior can be changed by disabling the 'automatic_sp_privileges' system variable. See *note stored-routines-privileges::.
File: manual.info.tmp, Node: alter-server, Next: alter-table, Prev: alter-procedure, Up: sql-data-definition-statements
ALTER SERVER SERVER_NAME
OPTIONS (OPTION [, OPTION] ...)
Alters the server information for 'SERVER_NAME', adjusting any of the options permitted in the *note 'CREATE SERVER': create-server. statement. The corresponding fields in the 'mysql.servers' table are updated accordingly. This statement requires the 'SUPER' privilege.
For example, to update the 'USER' option:
ALTER SERVER s OPTIONS (USER 'sally');
'ALTER SERVER' causes an implicit commit. See *note implicit-commit::.
'ALTER SERVER' is not written to the binary log, regardless of the logging format that is in use.
File: manual.info.tmp, Node: alter-table, Next: alter-tablespace, Prev: alter-server, Up: sql-data-definition-statements
Menu:
alter-table-examples:: ALTER TABLE Examples
ALTER TABLE TBL_NAME [ALTER_OPTION [, ALTER_OPTION] ...][PARTITION_OPTIONS]
ALTER_OPTION: { TABLE_OPTIONS | ADD [COLUMN] COL_NAME COLUMN_DEFINITION [FIRST | AFTER COL_NAME] | ADD [COLUMN] (COL_NAME COLUMN_DEFINITION,...) | ADD {INDEX | KEY} [INDEX_NAME][INDEX_TYPE] (KEY_PART,...) [INDEX_OPTION] ... | ADD {FULLTEXT | SPATIAL} [INDEX | KEY] [INDEX_NAME] (KEY_PART,...) [INDEX_OPTION] ... | ADD [CONSTRAINT [SYMBOL]] PRIMARY KEY [INDEX_TYPE] (KEY_PART,...) [INDEX_OPTION] ... | ADD [CONSTRAINT [SYMBOL]] UNIQUE [INDEX | KEY][INDEX_NAME] [INDEX_TYPE] (KEY_PART,...) [INDEX_OPTION] ... | ADD [CONSTRAINT [SYMBOL]] FOREIGN KEY [INDEX_NAME] (COL_NAME,...) REFERENCE_DEFINITION | ADD CHECK (EXPR) | ALGORITHM [=] {DEFAULT | INPLACE | COPY} | ALTER [COLUMN] COL_NAME { SET DEFAULT {LITERAL | (EXPR)} | DROP DEFAULT } | CHANGE [COLUMN] OLD_COL_NAME NEW_COL_NAME COLUMN_DEFINITION [FIRST | AFTER COL_NAME] | [DEFAULT] CHARACTER SET [=] CHARSET_NAME [COLLATE [=] COLLATION_NAME] | CONVERT TO CHARACTER SET CHARSET_NAME [COLLATE COLLATION_NAME] | {DISABLE | ENABLE} KEYS | {DISCARD | IMPORT} TABLESPACE | DROP [COLUMN] COL_NAME | DROP {INDEX | KEY} INDEX_NAME | DROP PRIMARY KEY | DROP FOREIGN KEY FK_SYMBOL | FORCE | LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE} | MODIFY [COLUMN] COL_NAME COLUMN_DEFINITION [FIRST | AFTER COL_NAME] | ORDER BY COL_NAME [, COL_NAME] ... | RENAME {INDEX | KEY} OLD_INDEX_NAME TO NEW_INDEX_NAME | RENAME [TO | AS] NEW_TBL_NAME | {WITHOUT | WITH} VALIDATION }
PARTITION_OPTIONS: PARTITION_OPTION [PARTITION_OPTION] ...
PARTITION_OPTION: { ADD PARTITION (PARTITION_DEFINITION) | DROP PARTITION PARTITION_NAMES | DISCARD PARTITION {PARTITION_NAMES | ALL} TABLESPACE | IMPORT PARTITION {PARTITION_NAMES | ALL} TABLESPACE | TRUNCATE PARTITION {PARTITION_NAMES | ALL} | COALESCE PARTITION NUMBER | REORGANIZE PARTITION PARTITION_NAMES INTO (PARTITION_DEFINITIONS) | EXCHANGE PARTITION PARTITION_NAME WITH TABLE TBL_NAME [{WITH | WITHOUT} VALIDATION] | ANALYZE PARTITION {PARTITION_NAMES | ALL} | CHECK PARTITION {PARTITION_NAMES | ALL} | OPTIMIZE PARTITION {PARTITION_NAMES | ALL} | REBUILD PARTITION {PARTITION_NAMES | ALL} | REPAIR PARTITION {PARTITION_NAMES | ALL} | REMOVE PARTITIONING | UPGRADE PARTITIONING }
KEY_PART: COL_NAME [(LENGTH)] [ASC | DESC]
INDEX_TYPE: USING {BTREE | HASH}
INDEX_OPTION: { KEY_BLOCK_SIZE [=] VALUE | INDEX_TYPE | WITH PARSER PARSER_NAME | COMMENT 'STRING' }
TABLE_OPTIONS: TABLE_OPTION [[,] TABLE_OPTION] ...
TABLE_OPTION: { AUTO_INCREMENT [=] VALUE | AVG_ROW_LENGTH [=] VALUE | [DEFAULT] CHARACTER SET [=] CHARSET_NAME | CHECKSUM [=] {0 | 1} | [DEFAULT] COLLATE [=] COLLATION_NAME | COMMENT [=] 'STRING' | COMPRESSION [=] {'ZLIB' | 'LZ4' | 'NONE'} | CONNECTION [=] 'CONNECT_STRING' | {DATA | INDEX} DIRECTORY [=] 'ABSOLUTE PATH TO DIRECTORY' | DELAY_KEY_WRITE [=] {0 | 1} | ENCRYPTION [=] {'Y' | 'N'} | ENGINE [=] ENGINE_NAME | INSERT_METHOD [=] { NO | FIRST | LAST } | KEY_BLOCK_SIZE [=] VALUE | MAX_ROWS [=] VALUE | MIN_ROWS [=] VALUE | PACK_KEYS [=] {0 | 1 | DEFAULT} | PASSWORD [=] 'STRING' | ROW_FORMAT [=] {DEFAULT | DYNAMIC | FIXED | COMPRESSED | REDUNDANT | COMPACT} | STATS_AUTO_RECALC [=] {DEFAULT | 0 | 1} | STATS_PERSISTENT [=] {DEFAULT | 0 | 1} | STATS_SAMPLE_PAGES [=] VALUE | TABLESPACE TABLESPACE_NAME [STORAGE {DISK | MEMORY}] | UNION [=] (TBL_NAME[,TBL_NAME]...) }
PARTITION_OPTIONS: (see *note CREATE TABLE: create-table. options)
*note 'ALTER TABLE': alter-table. changes the structure of a table. For example, you can add or delete columns, create or destroy indexes, change the type of existing columns, or rename columns or the table itself. You can also change characteristics such as the storage engine used for the table or the table comment.
To use *note 'ALTER TABLE': alter-table, you need 'ALTER', 'CREATE', and 'INSERT' privileges for the table. Renaming a table requires 'ALTER' and 'DROP' on the old table, 'ALTER', 'CREATE', and 'INSERT' on the new table.
Following the table name, specify the alterations to be made. If none are given, *note 'ALTER TABLE': alter-table. does nothing.
The syntax for many of the permissible alterations is similar to clauses of the note 'CREATE TABLE': create-table. statement. COLUMN_DEFINITION clauses use the same syntax for 'ADD' and 'CHANGE' as for note 'CREATE TABLE': create-table. For more information, see *note create-table::.
The word 'COLUMN' is optional and can be omitted.
Multiple 'ADD', 'ALTER', 'DROP', and 'CHANGE' clauses are permitted in a single note 'ALTER TABLE': alter-table. statement, separated by commas. This is a MySQL extension to standard SQL, which permits only one of each clause per note 'ALTER TABLE': alter-table. statement. For example, to drop multiple columns in a single statement, do this:
ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;
If a storage engine does not support an attempted note 'ALTER TABLE': alter-table. operation, a warning may result. Such warnings can be displayed with note 'SHOW WARNINGS': show-warnings. See note show-warnings::. For information on troubleshooting note 'ALTER TABLE': alter-table, see *note alter-table-problems::.
For information about generated columns, see *note alter-table-generated-columns::.
For usage examples, see *note alter-table-examples::.
With the 'mysql_info()' (https://dev.mysql.com/doc/c-api/5.7/en/mysql-info.html) C API function, you can find out how many rows were copied by *note 'ALTER TABLE': alter-table. See mysql_info() (https://dev.mysql.com/doc/c-api/5.7/en/mysql-info.html).
There are several additional aspects to the 'ALTER TABLE' statement, described under the following topics in this section:
*note alter-table-options::
*note alter-table-performance::
*note alter-table-concurrency::
*note alter-table-add-drop-column::
*note alter-table-redefine-column::
*note alter-table-index::
*note alter-table-foreign-key::
*note alter-table-character-set::
*note alter-table-discard-import::
*note alter-table-row-order::
*note alter-table-partition-options::
Table Options
TABLE_OPTIONS signifies table options of the kind that can be used in the *note 'CREATE TABLE': create-table. statement, such as 'ENGINE', 'AUTO_INCREMENT', 'AVG_ROW_LENGTH', 'MAX_ROWS', 'ROW_FORMAT', or 'TABLESPACE'.
For descriptions of all table options, see note create-table::. However, note 'ALTER TABLE': alter-table. ignores 'DATA DIRECTORY' and 'INDEX DIRECTORY' when given as table options. *note 'ALTER TABLE': alter-table. permits them only as partitioning options, and, as of MySQL 5.7.17, requires that you have the 'FILE' privilege.
Use of table options with *note 'ALTER TABLE': alter-table. provides a convenient way of altering single table characteristics. For example:
If 't1' is currently not an 'InnoDB' table, this statement changes its storage engine to 'InnoDB':
ALTER TABLE t1 ENGINE = InnoDB;
* See *note converting-tables-to-innodb:: for considerations
when switching tables to the 'InnoDB' storage engine.
* When you specify an 'ENGINE' clause, *note 'ALTER TABLE':
alter-table. rebuilds the table. This is true even if the
table already has the specified storage engine.
* Running *note 'ALTER TABLE TBL_NAME ENGINE=INNODB':
alter-table. on an existing 'InnoDB' table performs a 'null'
*note 'ALTER TABLE': alter-table. operation, which can be used
to defragment an 'InnoDB' table, as described in *note
innodb-file-defragmenting::. Running *note 'ALTER TABLE
TBL_NAME FORCE': alter-table. on an 'InnoDB' table performs
the same function.
* *note 'ALTER TABLE TBL_NAME ENGINE=INNODB': alter-table. and
*note 'ALTER TABLE TBL_NAME FORCE': alter-table. use *note
online DDL: innodb-online-ddl. For more information, see
*note innodb-online-ddl::.
* The outcome of attempting to change the storage engine of a
table is affected by whether the desired storage engine is
available and the setting of the 'NO_ENGINE_SUBSTITUTION' SQL
mode, as described in *note sql-mode::.
* To prevent inadvertent loss of data, *note 'ALTER TABLE':
alter-table. cannot be used to change the storage engine of a
table to 'MERGE' or 'BLACKHOLE'.
To change the 'InnoDB' table to use compressed row-storage format:
ALTER TABLE t1 ROW_FORMAT = COMPRESSED;
To enable or disable encryption for an 'InnoDB' table in a file-per-table tablespace:
ALTER TABLE t1 ENCRYPTION='Y';
ALTER TABLE t1 ENCRYPTION='N';
A keyring plugin must be installed and configured to use the 'ENCRYPTION' option. For more information, see *note innodb-data-encryption::.
The 'ENCRYPTION' option is supported only by the 'InnoDB' storage engine; thus it works only if the table already uses 'InnoDB' (and you do not change the table's storage engine), or if the 'ALTER TABLE' statement also specifies 'ENGINE=InnoDB'. Otherwise the statement is rejected with 'ER_CHECK_NOT_IMPLEMENTED' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_check_not_implemented).
To reset the current auto-increment value:
ALTER TABLE t1 AUTO_INCREMENT = 13;
You cannot reset the counter to a value less than or equal to the value that is currently in use. For both 'InnoDB' and 'MyISAM', if the value is less than or equal to the maximum value currently in the 'AUTO_INCREMENT' column, the value is reset to the current maximum 'AUTO_INCREMENT' column value plus one.
To change the default table character set:
ALTER TABLE t1 CHARACTER SET = utf8;
See also *note alter-table-character-set::.
To add (or change) a table comment:
ALTER TABLE t1 COMMENT = 'New table comment';
Use 'ALTER TABLE' with the 'TABLESPACE' option to move 'InnoDB' tables between existing general tablespaces, file-per-table tablespaces, and the system tablespace. See *note general-tablespaces-moving-non-partitioned-tables::.
* 'ALTER TABLE ... TABLESPACE' operations always cause a full
table rebuild, even if the 'TABLESPACE' attribute has not
changed from its previous value.
* 'ALTER TABLE ... TABLESPACE' syntax does not support moving a
table from a temporary tablespace to a persistent tablespace.
* The 'DATA DIRECTORY' clause, which is supported with *note
'CREATE TABLE ... TABLESPACE': create-table, is not supported
with 'ALTER TABLE ... TABLESPACE', and is ignored if
specified.
* For more information about the capabilities and limitations of
the 'TABLESPACE' option, see *note 'CREATE TABLE':
create-table.
MySQL NDB Cluster 7.5.2 and later supports setting 'NDB_TABLE' options for controlling a table's partition balance (fragment count type), read-from-any-replica capability, full replication, or any combination of these, as part of the table comment for an 'ALTER TABLE' statement in the same manner as for *note 'CREATE TABLE': create-table, as shown in this example:
ALTER TABLE t1 COMMENT = "NDB_TABLE=READ_BACKUP=0,PARTITION_BALANCE=FOR_RA_BY_NODE";
It is also possible to set 'NDB_COMMENT' options for columns of *note 'NDB': mysql-cluster. tables as part of an 'ALTER TABLE' statement, like this one:
ALTER TABLE t1
CHANGE COLUMN c1 c1 BLOB
COMMENT = 'NDB_COLUMN=MAX_BLOB_PART_SIZE';
Bear in mind that 'ALTER TABLE ... COMMENT ...' discards any existing comment for the table. See *note create-table-comment-ndb-table-options::, for additional information and examples.
To verify that the table options were changed as intended, use note 'SHOW CREATE TABLE': show-create-table, or query the Information Schema note 'TABLES': information-schema-tables-table. table.
Performance and Space Requirements
*note 'ALTER TABLE': alter-table. operations are processed using one of the following algorithms:
'COPY': Operations are performed on a copy of the original table, and table data is copied from the original table to the new table row by row. Concurrent DML is not permitted.
'INPLACE': Operations avoid copying table data but may rebuild the table in place. An exclusive metadata lock on the table may be taken briefly during preparation and execution phases of the operation. Typically, concurrent DML is supported.
For tables using the *note 'NDB': mysql-cluster. storage engine, these algorithms work as follows:
'COPY': 'NDB' creates a copy of the table and alters it; the NDB Cluster handler then copies the data between the old and new versions of the table. Subsequently, 'NDB' deletes the old table and renames the new one.
This is sometimes also referred to as a 'copying' or 'offline' 'ALTER TABLE'.
'INPLACE': The data nodes make the required changes; the NDB Cluster handler does not copy data or otherwise take part.
This is sometimes also referred to as a 'non-copying' or 'online' 'ALTER TABLE'.
See *note mysql-cluster-online-operations::, for more information.
The 'ALGORITHM' clause is optional. If the 'ALGORITHM' clause is omitted, MySQL uses 'ALGORITHM=INPLACE' for storage engines and *note 'ALTER TABLE': alter-table. clauses that support it. Otherwise, 'ALGORITHM=COPY' is used.
Specifying an 'ALGORITHM' clause requires the operation to use the specified algorithm for clauses and storage engines that support it, or fail with an error otherwise. Specifying 'ALGORITHM=DEFAULT' is the same as omitting the 'ALGORITHM' clause.
note 'ALTER TABLE': alter-table. operations that use the 'COPY' algorithm wait for other operations that are modifying the table to complete. After alterations are applied to the table copy, data is copied over, the original table is deleted, and the table copy is renamed to the name of the original table. While the note 'ALTER TABLE': alter-table. operation executes, the original table is readable by other sessions (with the exception noted shortly). Updates and writes to the table started after the *note 'ALTER TABLE': alter-table. operation begins are stalled until the new table is ready, then are automatically redirected to the new table. The temporary copy of the table is created in the database directory of the original table unless it is a 'RENAME TO' operation that moves the table to a database that resides in a different directory.
The exception referred to earlier is that *note 'ALTER TABLE': alter-table. blocks reads (not just writes) at the point where it is ready to install a new version of the table '.frm' file, discard the old file, and clear outdated table structures from the table and table definition caches. At this point, it must acquire an exclusive lock. To do so, it waits for current readers to finish, and blocks new reads and writes.
An note 'ALTER TABLE': alter-table. operation that uses the 'COPY' algorithm prevents concurrent DML operations. Concurrent queries are still allowed. That is, a table-copying operation always includes at least the concurrency restrictions of 'LOCK=SHARED' (allow queries but not DML). You can further restrict concurrency for operations that support the 'LOCK' clause by specifying 'LOCK=EXCLUSIVE', which prevents DML and queries. For more information, see note alter-table-concurrency::.
To force use of the 'COPY' algorithm for an *note 'ALTER TABLE': alter-table. operation that would otherwise not use it, enable the 'old_alter_table' system variable or specify 'ALGORITHM=COPY'. If there is a conflict between the 'old_alter_table' setting and an 'ALGORITHM' clause with a value other than 'DEFAULT', the 'ALGORITHM' clause takes precedence.
For 'InnoDB' tables, an *note 'ALTER TABLE': alter-table. operation that uses the 'COPY' algorithm on a table that resides in a shared tablespace can increase the amount of space used by the tablespace. Such operations require as much additional space as the data in the table plus indexes. For a table residing in a shared tablespace, the additional space used during the operation is not released back to the operating system as it is for a table that resides in a file-per-table tablespace.
For information about space requirements for online DDL operations, see *note innodb-online-ddl-space-requirements::.
*note 'ALTER TABLE': alter-table. operations that support the 'INPLACE' algorithm include:
'ALTER TABLE' operations supported by the 'InnoDB' online DDL feature. See *note innodb-online-ddl-operations::.
Renaming a table. MySQL renames files that correspond to the table TBL_NAME without making a copy. (You can also use the note 'RENAME TABLE': rename-table. statement to rename tables. See note rename-table::.) Privileges granted specifically for the renamed table are not migrated to the new name. They must be changed manually.
Operations that only modify table metadata. These operations are immediate because the server only alters the table '.frm' file, not touch table contents. Metadata-only operations include:
* Renaming a column.
* Changing the default value of a column (except for *note
'NDB': mysql-cluster. tables).
* Modifying the definition of an *note 'ENUM': enum. or *note
'SET': set. column by adding new enumeration or set members to
the _end_ of the list of valid member values, as long as the
storage size of the data type does not change. For example,
adding a member to a *note 'SET': set. column that has 8
members changes the required storage per value from 1 byte to
2 bytes; this requires a table copy. Adding members in the
middle of the list causes renumbering of existing members,
which requires a table copy.
Renaming an index.
Adding or dropping a secondary index, for note 'InnoDB': innodb-storage-engine. and note 'NDB': mysql-cluster. tables. See *note innodb-online-ddl::.
For note 'NDB': mysql-cluster. tables, operations that add and drop indexes on variable-width columns. These operations occur online, without table copying and without blocking concurrent DML actions for most of their duration. See note mysql-cluster-online-operations::.
*note 'ALTER TABLE': alter-table. upgrades MySQL 5.5 temporal columns to 5.6 format for 'ADD COLUMN', 'CHANGE COLUMN', 'MODIFY COLUMN', 'ADD INDEX', and 'FORCE' operations. This conversion cannot be done using the 'INPLACE' algorithm because the table must be rebuilt, so specifying 'ALGORITHM=INPLACE' in these cases results in an error. Specify 'ALGORITHM=COPY' if necessary.
If an 'ALTER TABLE' operation on a multicolumn index used to partition a table by 'KEY' changes the order of the columns, it can only be performed using 'ALGORITHM=COPY'.
The 'WITHOUT VALIDATION' and 'WITH VALIDATION' clauses affect whether note 'ALTER TABLE': alter-table. performs an in-place operation for virtual generated column modifications. See note alter-table-generated-columns::.
NDB Cluster formerly supported online 'ALTER TABLE' operations using the 'ONLINE' and 'OFFLINE' keywords. These keywords are no longer supported; their use causes a syntax error. MySQL NDB Cluster 7.5 (and later) supports online operations using the same 'ALGORITHM=INPLACE' syntax used with the standard MySQL Server. 'NDB' does not support changing a tablespace online. See *note mysql-cluster-online-operations::, for more information.
'ALTER TABLE' with 'DISCARD ... PARTITION ... TABLESPACE' or 'IMPORT ... PARTITION ... TABLESPACE' does not create any temporary tables or temporary partition files.
'ALTER TABLE' with 'ADD PARTITION', 'DROP PARTITION', 'COALESCE PARTITION', 'REBUILD PARTITION', or 'REORGANIZE PARTITION' does not create temporary tables (except when used with *note 'NDB': mysql-cluster. tables); however, these operations can and do create temporary partition files.
'ADD' or 'DROP' operations for 'RANGE' or 'LIST' partitions are immediate operations or nearly so. 'ADD' or 'COALESCE' operations for 'HASH' or 'KEY' partitions copy data between all partitions, unless 'LINEAR HASH' or 'LINEAR KEY' was used; this is effectively the same as creating a new table, although the 'ADD' or 'COALESCE' operation is performed partition by partition. 'REORGANIZE' operations copy only changed partitions and do not touch unchanged ones.
For 'MyISAM' tables, you can speed up index re-creation (the slowest part of the alteration process) by setting the 'myisam_sort_buffer_size' system variable to a high value.
Concurrency Control
For *note 'ALTER TABLE': alter-table. operations that support it, you can use the 'LOCK' clause to control the level of concurrent reads and writes on a table while it is being altered. Specifying a non-default value for this clause enables you to require a certain amount of concurrent access or exclusivity during the alter operation, and halts the operation if the requested degree of locking is not available. The parameters for the 'LOCK' clause are:
'LOCK = DEFAULT'
Maximum level of concurrency for the given 'ALGORITHM' clause (if any) and 'ALTER TABLE' operation: Permit concurrent reads and writes if supported. If not, permit concurrent reads if supported. If not, enforce exclusive access.
'LOCK = NONE'
If supported, permit concurrent reads and writes. Otherwise, an error occurs.
'LOCK = SHARED'
If supported, permit concurrent reads but block writes. Writes are blocked even if concurrent writes are supported by the storage engine for the given 'ALGORITHM' clause (if any) and 'ALTER TABLE' operation. If concurrent reads are not supported, an error occurs.
'LOCK = EXCLUSIVE'
Enforce exclusive access. This is done even if concurrent reads/writes are supported by the storage engine for the given 'ALGORITHM' clause (if any) and 'ALTER TABLE' operation.
Adding and Dropping Columns
Use 'ADD' to add new columns to a table, and 'DROP' to remove existing columns. 'DROP COL_NAME' is a MySQL extension to standard SQL.
To add a column at a specific position within a table row, use 'FIRST' or 'AFTER COL_NAME'. The default is to add the column last.
If a table contains only one column, the column cannot be dropped. If what you intend is to remove the table, use the *note 'DROP TABLE': drop-table. statement instead.
If columns are dropped from a table, the columns are also removed from any index of which they are a part. If all columns that make up an index are dropped, the index is dropped as well.
Renaming, Redefining, and Reordering Columns
The 'CHANGE', 'MODIFY', and 'ALTER' clauses enable the names and definitions of existing columns to be altered. They have these comparative characteristics:
'CHANGE':
* Can rename a column and change its definition, or both.
* Has more capability than 'MODIFY', but at the expense of
convenience for some operations. 'CHANGE' requires naming the
column twice if not renaming it.
* With 'FIRST' or 'AFTER', can reorder columns.
'MODIFY':
* Can change a column definition but not its name.
* More convenient than 'CHANGE' to change a column definition
without renaming it.
* With 'FIRST' or 'AFTER', can reorder columns.
'ALTER': Used only to change a column default value.
'CHANGE' is a MySQL extension to standard SQL. 'MODIFY' is a MySQL extension for Oracle compatibility.
To alter a column to change both its name and definition, use 'CHANGE', specifying the old and new names and the new definition. For example, to rename an 'INT NOT NULL' column from 'a' to 'b' and change its definition to use the 'BIGINT' data type while retaining the 'NOT NULL' attribute, do this:
ALTER TABLE t1 CHANGE a b BIGINT NOT NULL;
To change a column definition but not its name, use 'CHANGE' or 'MODIFY'. With 'CHANGE', the syntax requires two column names, so you must specify the same name twice to leave the name unchanged. For example, to change the definition of column 'b', do this:
ALTER TABLE t1 CHANGE b b INT NOT NULL;
'MODIFY' is more convenient to change the definition without changing the name because it requires the column name only once:
ALTER TABLE t1 MODIFY b INT NOT NULL;
To change a column name but not its definition, use 'CHANGE'. The syntax requires a column definition, so to leave the definition unchanged, you must respecify the definition the column currently has. For example, to rename an 'INT NOT NULL' column from 'b' to 'a', do this:
ALTER TABLE t1 CHANGE b a INT NOT NULL;
For column definition changes using 'CHANGE' or 'MODIFY', the definition must include the data type and all attributes that should apply to the new column, other than index attributes such as 'PRIMARY KEY' or 'UNIQUE'. Attributes present in the original definition but not specified for the new definition are not carried forward. Suppose that a column 'col1' is defined as 'INT UNSIGNED DEFAULT 1 COMMENT 'my column'' and you modify the column as follows, intending to change only 'INT' to 'BIGINT':
ALTER TABLE t1 MODIFY col1 BIGINT;
That statement changes the data type from 'INT' to 'BIGINT', but it also drops the 'UNSIGNED', 'DEFAULT', and 'COMMENT' attributes. To retain them, the statement must include them explicitly:
ALTER TABLE t1 MODIFY col1 BIGINT UNSIGNED DEFAULT 1 COMMENT 'my column';
For data type changes using 'CHANGE' or 'MODIFY', MySQL tries to convert existing column values to the new type as well as possible.
Warning:
This conversion may result in alteration of data. For example, if you shorten a string column, values may be truncated. To prevent the operation from succeeding if conversions to the new data type would result in loss of data, enable strict SQL mode before using note 'ALTER TABLE': alter-table. (see note sql-mode::).
If you use 'CHANGE' or 'MODIFY' to shorten a column for which an index exists on the column, and the resulting column length is less than the index length, MySQL shortens the index automatically.
For columns renamed by 'CHANGE', MySQL automatically renames these references to the renamed column:
Indexes that refer to the old column, including indexes and disabled 'MyISAM' indexes.
Foreign keys that refer to the old column.
For columns renamed by 'CHANGE', MySQL does not automatically rename these references to the renamed column:
Generated column and partition expressions that refer to the renamed column. You must use 'CHANGE' to redefine such expressions in the same *note 'ALTER TABLE': alter-table. statement as the one that renames the column.
Views and stored programs that refer to the renamed column. You must manually alter the definition of these objects to refer to the new column name.
To reorder columns within a table, use 'FIRST' and 'AFTER' in 'CHANGE' or 'MODIFY' operations.
'ALTER ... SET DEFAULT' or 'ALTER ... DROP DEFAULT' specify a new default value for a column or remove the old default value, respectively. If the old default is removed and the column can be 'NULL', the new default is 'NULL'. If the column cannot be 'NULL', MySQL assigns a default value as described in *note data-type-defaults::.
'ALTER ... SET DEFAULT' cannot be used with the 'CURRENT_TIMESTAMP' function.
Primary Keys and Indexes
'DROP PRIMARY KEY' drops the primary key. If there is no primary key, an error occurs. For information about the performance characteristics of primary keys, especially for 'InnoDB' tables, see *note primary-key-optimization::.
If you add a 'UNIQUE INDEX' or 'PRIMARY KEY' to a table, MySQL stores it before any nonunique index to permit detection of duplicate keys as early as possible.
note 'DROP INDEX': drop-index. removes an index. This is a MySQL extension to standard SQL. See note drop-index::. To determine index names, use 'SHOW INDEX FROM TBL_NAME'.
Some storage engines permit you to specify an index type when creating an index. The syntax for the INDEX_TYPE specifier is 'USING TYPE_NAME'. For details about 'USING', see *note create-index::. The preferred position is after the column list. You should expect support for use of the option before the column list to be removed in a future MySQL release.
INDEX_OPTION values specify additional options for an index. For details about permissible INDEX_OPTION values, see *note create-index::.
'RENAME INDEX OLD_INDEX_NAME TO NEW_INDEX_NAME' renames an index. This is a MySQL extension to standard SQL. The content of the table remains unchanged. OLD_INDEX_NAME must be the name of an existing index in the table that is not dropped by the same *note 'ALTER TABLE': alter-table. statement. NEW_INDEX_NAME is the new index name, which cannot duplicate the name of an index in the resulting table after changes have been applied. Neither index name can be 'PRIMARY'.
If you use note 'ALTER TABLE': alter-table. on a 'MyISAM' table, all nonunique indexes are created in a separate batch (as for note 'REPAIR TABLE': repair-table.). This should make *note 'ALTER TABLE': alter-table. much faster when you have many indexes.
For 'MyISAM' tables, key updating can be controlled explicitly. Use 'ALTER TABLE ... DISABLE KEYS' to tell MySQL to stop updating nonunique indexes. Then use 'ALTER TABLE ... ENABLE KEYS' to re-create missing indexes. 'MyISAM' does this with a special algorithm that is much faster than inserting keys one by one, so disabling keys before performing bulk insert operations should give a considerable speedup. Using 'ALTER TABLE ... DISABLE KEYS' requires the 'INDEX' privilege in addition to the privileges mentioned earlier.
While the nonunique indexes are disabled, they are ignored for statements such as note 'SELECT': select. and note 'EXPLAIN': explain. that otherwise would use them.
After an note 'ALTER TABLE': alter-table. statement, it may be necessary to run note 'ANALYZE TABLE': analyze-table. to update index cardinality information. See *note show-index::.
Foreign Keys and Other Constraints
The 'FOREIGN KEY' and 'REFERENCES' clauses are supported by the 'InnoDB' and 'NDB' storage engines, which implement 'ADD [CONSTRAINT [SYMBOL]] FOREIGN KEY [INDEX_NAME] (...) REFERENCES ... (...)'. See *note constraint-foreign-key::. For other storage engines, the clauses are parsed but ignored.
The 'CHECK' constraint clause is parsed but ignored by all storage engines. See note create-table::. The reason for accepting but ignoring syntax clauses is for compatibility, to make it easier to port code from other SQL servers, and to run applications that create tables with references. See note differences-from-ansi::.
For note 'ALTER TABLE': alter-table, unlike note 'CREATE TABLE': create-table, 'ADD FOREIGN KEY' ignores INDEX_NAME if given and uses an automatically generated foreign key name. As a workaround, include the 'CONSTRAINT' clause to specify the foreign key name:
ADD CONSTRAINT NAME FOREIGN KEY (....) ...
Important:
MySQL silently ignores inline 'REFERENCES' specifications, where the references are defined as part of the column specification. MySQL accepts only 'REFERENCES' clauses defined as part of a separate 'FOREIGN KEY' specification.
Note:
Partitioned 'InnoDB' tables do not support foreign keys. This restriction does not apply to 'NDB' tables, including those explicitly partitioned by '[LINEAR] KEY'. For more information, see *note partitioning-limitations-storage-engines::.
MySQL Server and NDB Cluster both support the use of *note 'ALTER TABLE': alter-table. to drop foreign keys:
ALTER TABLE TBL_NAME DROP FOREIGN KEY FK_SYMBOL;
Adding and dropping a foreign key in the same note 'ALTER TABLE': alter-table. statement is supported for note 'ALTER TABLE ... ALGORITHM=INPLACE': alter-table. but not for *note 'ALTER TABLE ... ALGORITHM=COPY': alter-table.
The server prohibits changes to foreign key columns that have the potential to cause loss of referential integrity. A workaround is to use note 'ALTER TABLE ... DROP FOREIGN KEY': alter-table. before changing the column definition and note 'ALTER TABLE ... ADD FOREIGN KEY': alter-table. afterward. Examples of prohibited changes include:
Changes to the data type of foreign key columns that may be unsafe. For example, changing note 'VARCHAR(20)': char. to note 'VARCHAR(30)': char. is permitted, but changing it to *note 'VARCHAR(1024)': char. is not because that alters the number of length bytes required to store individual values.
Changing a 'NULL' column to 'NOT NULL' in non-strict mode is prohibited to prevent converting 'NULL' values to default non-'NULL' values, for which there are no corresponding values in the referenced table. The operation is permitted in strict mode, but an error is returned if any such conversion is required.
'ALTER TABLE TBL_NAME RENAME NEW_TBL_NAME' changes internally generated foreign key constraint names and user-defined foreign key constraint names that begin with the string 'TBL_NAME_ibfk_' to reflect the new table name. 'InnoDB' interprets foreign key constraint names that begin with the string 'TBL_NAME_ibfk_' as internally generated names.
Changing the Character Set
To change the table default character set and all character columns (note 'CHAR': char, note 'VARCHAR': char, *note 'TEXT': blob.) to a new character set, use a statement like this:
ALTER TABLE TBL_NAME CONVERT TO CHARACTER SET CHARSET_NAME;
The statement also changes the collation of all character columns. If you specify no 'COLLATE' clause to indicate which collation to use, the statement uses default collation for the character set. If this collation is inappropriate for the intended table use (for example, if it would change from a case-sensitive collation to a case-insensitive collation), specify a collation explicitly.
For a column that has a data type of note 'VARCHAR': char. or one of the note 'TEXT': blob. types, 'CONVERT TO CHARACTER SET' changes the data type as necessary to ensure that the new column is long enough to store as many characters as the original column. For example, a note 'TEXT': blob. column has two length bytes, which store the byte-length of values in the column, up to a maximum of 65,535. For a 'latin1' note 'TEXT': blob. column, each character requires a single byte, so the column can store up to 65,535 characters. If the column is converted to 'utf8', each character might require up to three bytes, for a maximum possible length of 3 x 65,535 = 196,605 bytes. That length does not fit in a note 'TEXT': blob. column's length bytes, so MySQL converts the data type to note 'MEDIUMTEXT': blob, which is the smallest string type for which the length bytes can record a value of 196,605. Similarly, a note 'VARCHAR': char. column might be converted to note 'MEDIUMTEXT': blob.
To avoid data type changes of the type just described, do not use 'CONVERT TO CHARACTER SET'. Instead, use 'MODIFY' to change individual columns. For example:
ALTER TABLE t MODIFY latin1_text_col TEXT CHARACTER SET utf8;
ALTER TABLE t MODIFY latin1_varchar_col VARCHAR(M) CHARACTER SET utf8;
If you specify 'CONVERT TO CHARACTER SET binary', the note 'CHAR': char, note 'VARCHAR': char, and note 'TEXT': blob. columns are converted to their corresponding binary string types (note 'BINARY': binary-varbinary, note 'VARBINARY': binary-varbinary, note 'BLOB': blob.). This means that the columns no longer have a character set and a subsequent 'CONVERT TO' operation does not apply to them.
If CHARSET_NAME is 'DEFAULT' in a 'CONVERT TO CHARACTER SET' operation, the character set named by the 'character_set_database' system variable is used.
Warning:
The 'CONVERT TO' operation converts column values between the original and named character sets. This is not what you want if you have a column in one character set (like 'latin1') but the stored values actually use some other, incompatible character set (like 'utf8'). In this case, you have to do the following for each such column:
ALTER TABLE t1 CHANGE c1 c1 BLOB;
ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8;
The reason this works is that there is no conversion when you convert to or from *note 'BLOB': blob. columns.
To change only the default character set for a table, use this statement:
ALTER TABLE TBL_NAME DEFAULT CHARACTER SET CHARSET_NAME;
The word 'DEFAULT' is optional. The default character set is the character set that is used if you do not specify the character set for columns that you add to a table later (for example, with 'ALTER TABLE ... ADD column').
When the 'foreign_key_checks' system variable is enabled, which is the default setting, character set conversion is not permitted on tables that include a character string column used in a foreign key constraint. The workaround is to disable 'foreign_key_checks' before performing the character set conversion. You must perform the conversion on both tables involved in the foreign key constraint before re-enabling 'foreign_key_checks'. If you re-enable 'foreign_key_checks' after converting only one of the tables, an 'ON DELETE CASCADE' or 'ON UPDATE CASCADE' operation could corrupt data in the referencing table due to implicit conversion that occurs during these operations (Bug #45290, Bug #74816).
Discarding and Importing InnoDB Tablespaces
An 'InnoDB' table created in its own file-per-table tablespace can be imported from a backup or from another MySQL server instance using 'DISCARD TABLEPACE' and 'IMPORT TABLESPACE' clauses. See *note innodb-table-import::.
Row Order for MyISAM Tables
'ORDER BY' enables you to create the new table with the rows in a specific order. This option is useful primarily when you know that you query the rows in a certain order most of the time. By using this option after major changes to the table, you might be able to get higher performance. In some cases, it might make sorting easier for MySQL if the table is in order by the column that you want to order it by later.
Note:
The table does not remain in the specified order after inserts and deletes.
'ORDER BY' syntax permits one or more column names to be specified for sorting, each of which optionally can be followed by 'ASC' or 'DESC' to indicate ascending or descending sort order, respectively. The default is ascending order. Only column names are permitted as sort criteria; arbitrary expressions are not permitted. This clause should be given last after any other clauses.
'ORDER BY' does not make sense for 'InnoDB' tables because 'InnoDB' always orders table rows according to the clustered index.
When used on a partitioned table, 'ALTER TABLE ... ORDER BY' orders rows within each partition only.
Partitioning Options
PARTITION_OPTIONS signifies options that can be used with partitioned tables for repartitioning, to add, drop, discard, import, merge, and split partitions, and to perform partitioning maintenance.
It is possible for an *note 'ALTER TABLE': alter-table. statement to contain a 'PARTITION BY' or 'REMOVE PARTITIONING' clause in an addition to other alter specifications, but the 'PARTITION BY' or 'REMOVE PARTITIONING' clause must be specified last after any other specifications. The 'ADD PARTITION', 'DROP PARTITION', 'DISCARD PARTITION', 'IMPORT PARTITION', 'COALESCE PARTITION', 'REORGANIZE PARTITION', 'EXCHANGE PARTITION', 'ANALYZE PARTITION', 'CHECK PARTITION', and 'REPAIR PARTITION' options cannot be combined with other alter specifications in a single 'ALTER TABLE', since the options just listed act on individual partitions.
For more information about partition options, see note create-table::, and note alter-table-partition-operations::. For information about and examples of 'ALTER TABLE ... EXCHANGE PARTITION' statements, see *note partitioning-management-exchange::.
Prior to MySQL 5.7.6, partitioned 'InnoDB' tables used the generic 'ha_partition' partitioning handler employed by 'MyISAM' and other storage engines not supplying their own partitioning handlers; in MySQL 5.7.6 and later, such tables are created using the 'InnoDB' storage engine's own (or 'native') partitioning handler. Beginning with MySQL 5.7.9, you can upgrade an 'InnoDB' table that was created in MySQL 5.7.6 or earlier (that is, created using 'ha_partition') to the 'InnoDB' native partition handler using 'ALTER TABLE ... UPGRADE PARTITIONING'. (Bug #76734, Bug #20727344) This 'ALTER TABLE' syntax does not accept any other options and can be used only on a single table at a time. You can also use *note 'mysql_upgrade': mysql-upgrade. in MySQL 5.7.9 or later to upgrade older partitioned 'InnoDB' tables to the native partitioning handler.
File: manual.info.tmp, Node: alter-table-partition-operations, Next: alter-table-generated-columns, Prev: alter-table, Up: alter-table
13.1.8.1 ALTER TABLE Partition Operations .........................................
Partitioning-related clauses for *note 'ALTER TABLE': alter-table. can be used with partitioned tables for repartitioning, to add, drop, discard, import, merge, and split partitions, and to perform partitioning maintenance.
Simply using a PARTITION_OPTIONS clause with note 'ALTER TABLE': alter-table. on a partitioned table repartitions the table according to the partitioning scheme defined by the PARTITION_OPTIONS. This clause always begins with 'PARTITION BY', and follows the same syntax and other rules as apply to the PARTITION_OPTIONS clause for note 'CREATE TABLE': create-table. (for more detailed information, see *note create-table::), and can also be used to partition an existing table that is not already partitioned. For example, consider a (nonpartitioned) table defined as shown here:
CREATE TABLE t1 (
id INT,
year_col INT
);
This table can be partitioned by 'HASH', using the 'id' column as the partitioning key, into 8 partitions by means of this statement:
ALTER TABLE t1
PARTITION BY HASH(id)
PARTITIONS 8;
MySQL supports an 'ALGORITHM' option with '[SUB]PARTITION BY [LINEAR] KEY'. 'ALGORITHM=1' causes the server to use the same key-hashing functions as MySQL 5.1 when computing the placement of rows in partitions; 'ALGORITHM=2' means that the server employs the key-hashing functions implemented and used by default for new 'KEY' partitioned tables in MySQL 5.5 and later. (Partitioned tables created with the key-hashing functions employed in MySQL 5.5 and later cannot be used by a MySQL 5.1 server.) Not specifying the option has the same effect as using 'ALGORITHM=2'. This option is intended for use chiefly when upgrading or downgrading '[LINEAR] KEY' partitioned tables between MySQL 5.1 and later MySQL versions, or for creating tables partitioned by 'KEY' or 'LINEAR KEY' on a MySQL 5.5 or later server which can be used on a MySQL 5.1 server.
To upgrade a 'KEY' partitioned table that was created in MySQL 5.1, first execute note 'SHOW CREATE TABLE': show-create-table. and note the exact columns and number of partitions shown. Now execute an 'ALTER TABLE' statement using exactly the same column list and number of partitions as in the 'CREATE TABLE' statement, while adding 'ALGORITHM=2' immediately following the 'PARTITION BY' keywords. (You should also include the 'LINEAR' keyword if it was used for the original table definition.) An example from a session in the note 'mysql': mysql. client is shown here:
mysql> SHOW CREATE TABLE p\G
*************************** 1. row ***************************
Table: p
Create Table: CREATE TABLE `p` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cd` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
/*!50100 _PARTITION BY LINEAR KEY (id)
PARTITIONS 32_ */
1 row in set (0.00 sec)
mysql> ALTER TABLE p _PARTITION BY LINEAR KEY ALGORITHM=2 (id) PARTITIONS 32_;
Query OK, 0 rows affected (5.34 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW CREATE TABLE p\G
*************************** 1. row ***************************
Table: p
Create Table: CREATE TABLE `p` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cd` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
/*!50100 PARTITION BY LINEAR KEY (id)
PARTITIONS 32 */
1 row in set (0.00 sec)
Downgrading a table created using the default key-hashing used in MySQL 5.5 and later to enable its use by a MySQL 5.1 server is similar, except in this case you should use 'ALGORITHM=1' to force the table's partitions to be rebuilt using the MySQL 5.1 key-hashing functions. It is recommended that you not do this except when necessary for compatibility with a MySQL 5.1 server, as the improved 'KEY' hashing functions used by default in MySQL 5.5 and later provide fixes for a number of issues found in the older implementation.
Note:
A table upgraded by means of 'ALTER TABLE ... PARTITION BY ALGORITHM=2 [LINEAR] KEY ...' can no longer be used by a MySQL 5.1 server. (Such a table would need to be downgraded with 'ALTER TABLE ... PARTITION BY ALGORITHM=1 [LINEAR] KEY ...' before it could be used again by a MySQL 5.1 server.)
The table that results from using an 'ALTER TABLE ... PARTITION BY' statement must follow the same rules as one created using 'CREATE TABLE ... PARTITION BY'. This includes the rules governing the relationship between any unique keys (including any primary key) that the table might have, and the column or columns used in the partitioning expression, as discussed in *note partitioning-limitations-partitioning-keys-unique-keys::. The 'CREATE TABLE ... PARTITION BY' rules for specifying the number of partitions also apply to 'ALTER TABLE ... PARTITION BY'.
The PARTITION_DEFINITION clause for 'ALTER TABLE ADD PARTITION' supports the same options as the clause of the same name for the note 'CREATE TABLE': create-table. statement. (See note create-table::, for the syntax and description.) Suppose that you have the partitioned table created as shown here:
CREATE TABLE t1 (
id INT,
year_col INT
)
PARTITION BY RANGE (year_col) (
PARTITION p0 VALUES LESS THAN (1991),
PARTITION p1 VALUES LESS THAN (1995),
PARTITION p2 VALUES LESS THAN (1999)
);
You can add a new partition 'p3' to this table for storing values less than '2002' as follows:
ALTER TABLE t1 ADD PARTITION (PARTITION p3 VALUES LESS THAN (2002));
'DROP PARTITION' can be used to drop one or more 'RANGE' or 'LIST' partitions. This statement cannot be used with 'HASH' or 'KEY' partitions; instead, use 'COALESCE PARTITION' (see below). Any data that was stored in the dropped partitions named in the PARTITION_NAMES list is discarded. For example, given the table 't1' defined previously, you can drop the partitions named 'p0' and 'p1' as shown here:
ALTER TABLE t1 DROP PARTITION p0, p1;
Note:
'DROP PARTITION' does not work with tables that use the note 'NDB': mysql-cluster. storage engine. See note partitioning-management-range-list::, and *note mysql-cluster-limitations::.
'ADD PARTITION' and 'DROP PARTITION' do not currently support 'IF [NOT] EXISTS'.
note 'DISCARD PARTITION ... TABLESPACE': alter-table. and note 'IMPORT PARTITION ... TABLESPACE': alter-table. options extend the Transportable Tablespace feature to individual 'InnoDB' table partitions. Each 'InnoDB' table partition has its own tablespace file ('.ibd' file). The Transportable Tablespace feature makes it easy to copy the tablespaces from a running MySQL server instance to another running instance, or to perform a restore on the same instance. Both options take a list of one or more comma-separated partition names. For example:
ALTER TABLE t1 DISCARD PARTITION p2, p3 TABLESPACE;
ALTER TABLE t1 IMPORT PARTITION p2, p3 TABLESPACE;
When running note 'DISCARD PARTITION ... TABLESPACE': alter-table. and note 'IMPORT PARTITION ... TABLESPACE': alter-table. on subpartitioned tables, both partition and subpartition names are allowed. When a partition name is specified, subpartitions of that partition are included.
The Transportable Tablespace feature also supports copying or restoring partitioned 'InnoDB' tables. For more information, see *note innodb-table-import::.
Renames of partitioned tables are supported. You can rename individual partitions indirectly using 'ALTER TABLE ... REORGANIZE PARTITION'; however, this operation copies the partition's data.
To delete rows from selected partitions, use the 'TRUNCATE PARTITION' option. This option takes a comma-separated list of one or more partition names. For example, consider the table 't1' as defined here:
CREATE TABLE t1 (
id INT,
year_col INT
)
PARTITION BY RANGE (year_col) (
PARTITION p0 VALUES LESS THAN (1991),
PARTITION p1 VALUES LESS THAN (1995),
PARTITION p2 VALUES LESS THAN (1999),
PARTITION p3 VALUES LESS THAN (2003),
PARTITION p4 VALUES LESS THAN (2007)
);
To delete all rows from partition 'p0', use the following statement:
ALTER TABLE t1 TRUNCATE PARTITION p0;
The statement just shown has the same effect as the following *note 'DELETE': delete. statement:
DELETE FROM t1 WHERE year_col < 1991;
When truncating multiple partitions, the partitions do not have to be contiguous: This can greatly simplify delete operations on partitioned tables that would otherwise require very complex 'WHERE' conditions if done with *note 'DELETE': delete. statements. For example, this statement deletes all rows from partitions 'p1' and 'p3':
ALTER TABLE t1 TRUNCATE PARTITION p1, p3;
An equivalent *note 'DELETE': delete. statement is shown here:
DELETE FROM t1 WHERE
(year_col >= 1991 AND year_col < 1995)
OR
(year_col >= 2003 AND year_col < 2007);
If you use the 'ALL' keyword in place of the list of partition names, the statement acts on all table partitions.
'TRUNCATE PARTITION' merely deletes rows; it does not alter the definition of the table itself, or of any of its partitions.
To verify that the rows were dropped, check the 'INFORMATION_SCHEMA.PARTITIONS' table, using a query such as this one:
SELECT PARTITION_NAME, TABLE_ROWS
FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME = 't1';
'TRUNCATE PARTITION' is supported only for partitioned tables that use the note 'MyISAM': myisam-storage-engine, note 'InnoDB': innodb-storage-engine, or note 'MEMORY': memory-storage-engine. storage engine. It also works on note 'BLACKHOLE': blackhole-storage-engine. tables (but has no effect). It is not supported for *note 'ARCHIVE': archive-storage-engine. tables.
'COALESCE PARTITION' can be used with a table that is partitioned by 'HASH' or 'KEY' to reduce the number of partitions by NUMBER. Suppose that you have created table 't2' as follows:
CREATE TABLE t2 (
name VARCHAR (30),
started DATE
)
PARTITION BY HASH( YEAR(started) )
PARTITIONS 6;
To reduce the number of partitions used by 't2' from 6 to 4, use the following statement:
ALTER TABLE t2 COALESCE PARTITION 2;
The data contained in the last NUMBER partitions are merged into the remaining partitions. In this case, partitions 4 and 5 are merged into the first 4 partitions (the partitions numbered 0, 1, 2, and 3).
To change some but not all the partitions used by a partitioned table, you can use 'REORGANIZE PARTITION'. This statement can be used in several ways:
* To merge a set of partitions into a single partition. This is
done by naming several partitions in the PARTITION_NAMES list
and supplying a single definition for PARTITION_DEFINITION.
* To split an existing partition into several partitions.
Accomplish this by naming a single partition for
PARTITION_NAMES and providing multiple PARTITION_DEFINITIONS.
* To change the ranges for a subset of partitions defined using
'VALUES LESS THAN' or the value lists for a subset of
partitions defined using 'VALUES IN'.
* This statement may also be used without the 'PARTITION_NAMES
INTO (PARTITION_DEFINITIONS)' option on tables that are
automatically partitioned using 'HASH' partitioning to force
redistribution of data. (Currently, only *note 'NDB':
mysql-cluster. tables are automatically partitioned in this
way.) This is useful in NDB Cluster where, after you have
added new NDB Cluster data nodes online to an existing NDB
Cluster, you wish to redistribute existing NDB Cluster table
data to the new data nodes. In such cases, you should invoke
the statement with the 'ALGORITHM=INPLACE' option; in other
words, as shown here:
ALTER TABLE TABLE ALGORITHM=INPLACE, REORGANIZE PARTITION;
You cannot perform other DDL concurrently with online table
reorganization--that is, no other DDL statements can be issued
while an 'ALTER TABLE ... ALGORITHM=INPLACE, REORGANIZE
PARTITION' statement is executing. For more information about
adding NDB Cluster data nodes online, see *note
mysql-cluster-online-add-node::.
*Note*:
'ALTER TABLE ... ALGORITHM=INPLACE, REORGANIZE PARTITION' does
not work with tables which were created using the 'MAX_ROWS'
option, because it uses the constant 'MAX_ROWS' value
specified in the original *note 'CREATE TABLE': create-table.
statement to determine the number of partitions required, so
no new partitions are created. Instead, you can use 'ALTER
TABLE ... ALGORITHM=INPLACE, MAX_ROWS=ROWS' to increase the
maximum number of rows for such a table; in this case, 'ALTER
TABLE ... ALGORITHM=INPLACE, REORGANIZE PARTITION' is not
needed (and causes an error if executed). The value of ROWS
must be greater than the value specified for 'MAX_ROWS' in the
original 'CREATE TABLE' statement for this to work.
Employing 'MAX_ROWS' to force the number of table partitions
is deprecated in NDB 7.5.4 and later; use 'PARTITION_BALANCE'
instead (see *note create-table-comment-ndb-table-options::).
Attempting to use 'REORGANIZE PARTITION' without the
'PARTITION_NAMES INTO (PARTITION_DEFINITIONS)' option on
explicitly partitioned tables results in the error 'REORGANIZE
PARTITION without parameters can only be used on
auto-partitioned tables using HASH partitioning'.
Note:
For partitions that have not been explicitly named, MySQL automatically provides the default names 'p0', 'p1', 'p2', and so on. The same is true with regard to subpartitions.
For more detailed information about and examples of 'ALTER TABLE ... REORGANIZE PARTITION' statements, see *note partitioning-management-range-list::.
To exchange a table partition or subpartition with a table, use the *note 'ALTER TABLE ... EXCHANGE PARTITION': alter-table. statement--that is, to move any existing rows in the partition or subpartition to the nonpartitioned table, and any existing rows in the nonpartitioned table to the table partition or subpartition.
For usage information and examples, see *note partitioning-management-exchange::.
Several options provide partition maintenance and repair functionality analogous to that implemented for nonpartitioned tables by statements such as note 'CHECK TABLE': check-table. and note 'REPAIR TABLE': repair-table. (which are also supported for partitioned tables; for more information, see note table-maintenance-statements::). These include 'ANALYZE PARTITION', 'CHECK PARTITION', 'OPTIMIZE PARTITION', 'REBUILD PARTITION', and 'REPAIR PARTITION'. Each of these options takes a PARTITION_NAMES clause consisting of one or more names of partitions, separated by commas. The partitions must already exist in the table to be altered. You can also use the 'ALL' keyword in place of PARTITION_NAMES, in which case the statement acts on all table partitions. For more information and examples, see note partitioning-maintenance::.
Some MySQL storage engines, such as *note 'InnoDB': innodb-storage-engine, do not support per-partition optimization. For a partitioned table using such a storage engine, 'ALTER TABLE ... OPTIMIZE PARTITION' causes the entire table to rebuilt and analyzed, and an appropriate warning to be issued. (Bug #11751825, Bug #42822)
To work around this problem, use the statements 'ALTER TABLE ... REBUILD PARTITION' and 'ALTER TABLE ... ANALYZE PARTITION' instead.
The 'ANALYZE PARTITION', 'CHECK PARTITION', 'OPTIMIZE PARTITION', and 'REPAIR PARTITION' options are not permitted for tables which are not partitioned.
In MySQL 5.7.9 and later, you can use 'ALTER TABLE ... UPGRADE PARTITIONING' to upgrade a partitioned note 'InnoDB': innodb-storage-engine. table that was created with the old generic partitioning handler to the 'InnoDB' native partitioning employed in MySQL 5.7.6 and later. Also beginning with MySQL 5.7.9, the note 'mysql_upgrade': mysql-upgrade. utility checks for such partitioned 'InnoDB' tables and attempts to upgrade them to native partitioning as part of its normal operations.
Important:
Partitioned 'InnoDB' tables that do not use the 'InnoDB' native partitioning handler cannot be used in MySQL 8.0 or later. 'ALTER TABLE ... UPGRADE PARTITIONING' is not supported in MySQL 8.0 or later; therefore, any partitioned 'InnoDB' tables that employ the generic handler must be upgraded to the InnoDB native handler before upgrading your MySQL installation to MySQL 8.0 or later.
'REMOVE PARTITIONING' enables you to remove a table's partitioning without otherwise affecting the table or its data. This option can be combined with other *note 'ALTER TABLE': alter-table. options such as those used to add, drop, or rename columns or indexes.
Using the 'ENGINE' option with *note 'ALTER TABLE': alter-table. changes the storage engine used by the table without affecting the partitioning.
When 'ALTER TABLE ... EXCHANGE PARTITION' or 'ALTER TABLE ... TRUNCATE PARTITION' is run against a partitioned table that uses note 'MyISAM': myisam-storage-engine. (or another storage engine that makes use of table-level locking), only those partitions that are actually read from are locked. (This does not apply to partitioned tables using a storage enginethat employs row-level locking, such as note 'InnoDB': innodb-storage-engine.) See *note partitioning-limitations-locking::.
It is possible for an *note 'ALTER TABLE': alter-table. statement to contain a 'PARTITION BY' or 'REMOVE PARTITIONING' clause in an addition to other alter specifications, but the 'PARTITION BY' or 'REMOVE PARTITIONING' clause must be specified last after any other specifications.
The 'ADD PARTITION', 'DROP PARTITION', 'COALESCE PARTITION', 'REORGANIZE PARTITION', 'ANALYZE PARTITION', 'CHECK PARTITION', and 'REPAIR PARTITION' options cannot be combined with other alter specifications in a single 'ALTER TABLE', since the options just listed act on individual partitions. For more information, see *note alter-table-partition-operations::.
Only a single instance of any one of the following options can be used in a given *note 'ALTER TABLE': alter-table. statement: 'PARTITION BY', 'ADD PARTITION', 'DROP PARTITION', 'TRUNCATE PARTITION', 'EXCHANGE PARTITION', 'REORGANIZE PARTITION', or 'COALESCE PARTITION', 'ANALYZE PARTITION', 'CHECK PARTITION', 'OPTIMIZE PARTITION', 'REBUILD PARTITION', 'REMOVE PARTITIONING'.
For example, the following two statements are invalid:
ALTER TABLE t1 ANALYZE PARTITION p1, ANALYZE PARTITION p2;
ALTER TABLE t1 ANALYZE PARTITION p1, CHECK PARTITION p2;
In the first case, you can analyze partitions 'p1' and 'p2' of table 't1' concurrently using a single statement with a single 'ANALYZE PARTITION' option that lists both of the partitions to be analyzed, like this:
ALTER TABLE t1 ANALYZE PARTITION p1, p2;
In the second case, it is not possible to perform 'ANALYZE' and 'CHECK' operations on different partitions of the same table concurrently. Instead, you must issue two separate statements, like this:
ALTER TABLE t1 ANALYZE PARTITION p1;
ALTER TABLE t1 CHECK PARTITION p2;
'REBUILD' operations are currently unsupported for subpartitions. The 'REBUILD' keyword is expressly disallowed with subpartitions, and causes 'ALTER TABLE' to fail with an error if so used.
'CHECK PARTITION 'and' REPAIR PARTITION' operations fail when the partition to be checked or repaired contains any duplicate key errors.
For more information about these statements, see *note partitioning-maintenance::.
File: manual.info.tmp, Node: alter-table-generated-columns, Next: alter-table-examples, Prev: alter-table-partition-operations, Up: alter-table
13.1.8.2 ALTER TABLE and Generated Columns ..........................................
'ALTER TABLE' operations permitted for generated columns are 'ADD', 'MODIFY', and 'CHANGE'.
Generated columns can be added.
CREATE TABLE t1 (c1 INT);
ALTER TABLE t1 ADD COLUMN c2 INT GENERATED ALWAYS AS (c1 + 1) STORED;
The data type and expression of generated columns can be modified.
CREATE TABLE t1 (c1 INT, c2 INT GENERATED ALWAYS AS (c1 + 1) STORED);
ALTER TABLE t1 MODIFY COLUMN c2 TINYINT GENERATED ALWAYS AS (c1 + 5) STORED;
Generated columns can be renamed or dropped, if no other column refers to them.
CREATE TABLE t1 (c1 INT, c2 INT GENERATED ALWAYS AS (c1 + 1) STORED);
ALTER TABLE t1 CHANGE c2 c3 INT GENERATED ALWAYS AS (c1 + 1) STORED;
ALTER TABLE t1 DROP COLUMN c3;
Virtual generated columns cannot be altered to stored generated columns, or vice versa. To work around this, drop the column, then add it with the new definition.
CREATE TABLE t1 (c1 INT, c2 INT GENERATED ALWAYS AS (c1 + 1) VIRTUAL);
ALTER TABLE t1 DROP COLUMN c2;
ALTER TABLE t1 ADD COLUMN c2 INT GENERATED ALWAYS AS (c1 + 1) STORED;
Nongenerated columns can be altered to stored but not virtual generated columns.
CREATE TABLE t1 (c1 INT, c2 INT);
ALTER TABLE t1 MODIFY COLUMN c2 INT GENERATED ALWAYS AS (c1 + 1) STORED;
Stored but not virtual generated columns can be altered to nongenerated columns. The stored generated values become the values of the nongenerated column.
CREATE TABLE t1 (c1 INT, c2 INT GENERATED ALWAYS AS (c1 + 1) STORED);
ALTER TABLE t1 MODIFY COLUMN c2 INT;
'ADD COLUMN' is not an in-place operation for stored columns (done without using a temporary table) because the expression must be evaluated by the server. For stored columns, indexing changes are done in place, and expression changes are not done in place. Changes to column comments are done in place.
For non-partitioned tables, 'ADD COLUMN' and 'DROP COLUMN' are in-place operations for virtual columns. However, adding or dropping a virtual column cannot be performed in place in combination with other *note 'ALTER TABLE': alter-table. operations.
For partitioned tables, 'ADD COLUMN' and 'DROP COLUMN' are not in-place operations for virtual columns.
'InnoDB' supports secondary indexes on virtual generated columns. Adding or dropping a secondary index on a virtual generated column is an in-place operation. For more information, see *note create-table-secondary-indexes::.
When a 'VIRTUAL' generated column is added to a table or modified, it is not ensured that values being calculated by the generated column expression are not out of range for the column. This can lead to inconsistent data being returned and unexpectedly failed statements. To permit control over whether validation occurs for such columns, 'ALTER TABLE' supports 'WITHOUT VALIDATION' and 'WITH VALIDATION' clauses:
* With 'WITHOUT VALIDATION' (the default if neither clause is
specified), an in-place operation is performed (if possible),
data integrity is not checked, and the statement finishes more
quickly. However, later reads from the table might report
warnings or errors for the column if values are out of range.
* With 'WITH VALIDATION', 'ALTER TABLE' copies the table. If an
out-of-range or any other error occurs, the statement fails.
Because a table copy is performed, the statement takes longer.
'WITHOUT VALIDATION' and 'WITH VALIDATION' are permitted only with 'ADD COLUMN', 'CHANGE COLUMN', and 'MODIFY COLUMN' operations. Otherwise, an 'ER_WRONG_USAGE' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_wrong_usage) error occurs.
As of MySQL 5.7.10, if expression evaluation causes truncation or provides incorrect input to a function, the *note 'ALTER TABLE': alter-table. statement terminates with an error and the DDL operation is rejected.
An note 'ALTER TABLE': alter-table. statement that changes the default value of a column COL_NAME may also change the value of a generated column expression that refers to the column using 'DEFAULT(COL_NAME)'. For this reason, as of MySQL 5.7.13, note 'ALTER TABLE': alter-table. operations that change the definition of a column cause a table rebuild if any generated column expression uses 'DEFAULT()'.
File: manual.info.tmp, Node: alter-table-examples, Prev: alter-table-generated-columns, Up: alter-table
13.1.8.3 ALTER TABLE Examples .............................
Begin with a table 't1' created as shown here:
CREATE TABLE t1 (a INTEGER, b CHAR(10));
To rename the table from 't1' to 't2':
ALTER TABLE t1 RENAME t2;
To change column 'a' from *note 'INTEGER': integer-types. to 'TINYINT NOT NULL' (leaving the name the same), and to change column 'b' from 'CHAR(10)' to 'CHAR(20)' as well as renaming it from 'b' to 'c':
ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c CHAR(20);
To add a new *note 'TIMESTAMP': datetime. column named 'd':
ALTER TABLE t2 ADD d TIMESTAMP;
To add an index on column 'd' and a 'UNIQUE' index on column 'a':
ALTER TABLE t2 ADD INDEX (d), ADD UNIQUE (a);
To remove column 'c':
ALTER TABLE t2 DROP COLUMN c;
To add a new 'AUTO_INCREMENT' integer column named 'c':
ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT,
ADD PRIMARY KEY (c);
We indexed 'c' (as a 'PRIMARY KEY') because 'AUTO_INCREMENT' columns must be indexed, and we declare 'c' as 'NOT NULL' because primary key columns cannot be 'NULL'.
For note 'NDB': mysql-cluster. tables, it is also possible to change the storage type used for a table or column. For example, consider an note 'NDB': mysql-cluster. table created as shown here:
mysql> CREATE TABLE t1 (c1 INT) TABLESPACE ts_1 ENGINE NDB;
Query OK, 0 rows affected (1.27 sec)
To convert this table to disk-based storage, you can use the following *note 'ALTER TABLE': alter-table. statement:
mysql> ALTER TABLE t1 TABLESPACE ts_1 STORAGE DISK;
Query OK, 0 rows affected (2.99 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW CREATE TABLE t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`c1` int(11) DEFAULT NULL
) /*!50100 TABLESPACE ts_1 STORAGE DISK */
ENGINE=ndbcluster DEFAULT CHARSET=latin1
1 row in set (0.01 sec)
It is not necessary that the tablespace was referenced when the table was originally created; however, the tablespace must be referenced by the *note 'ALTER TABLE': alter-table.:
mysql> CREATE TABLE t2 (c1 INT) ts_1 ENGINE NDB;
Query OK, 0 rows affected (1.00 sec)
mysql> ALTER TABLE t2 STORAGE DISK;
ERROR 1005 (HY000): Can't create table 'c.#sql-1750_3' (errno: 140)
mysql> ALTER TABLE t2 TABLESPACE ts_1 STORAGE DISK;
Query OK, 0 rows affected (3.42 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW CREATE TABLE t2\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t2` (
`c1` int(11) DEFAULT NULL
) /*!50100 TABLESPACE ts_1 STORAGE DISK */
ENGINE=ndbcluster DEFAULT CHARSET=latin1
1 row in set (0.01 sec)
To change the storage type of an individual column, you can use 'ALTER TABLE ... MODIFY [COLUMN]'. For example, suppose you create an NDB Cluster Disk Data table with two columns, using this *note 'CREATE TABLE': create-table. statement:
mysql> CREATE TABLE t3 (c1 INT, c2 INT)
-> TABLESPACE ts_1 STORAGE DISK ENGINE NDB;
Query OK, 0 rows affected (1.34 sec)
To change column 'c2' from disk-based to in-memory storage, include a STORAGE MEMORY clause in the column definition used by the ALTER TABLE statement, as shown here:
mysql> ALTER TABLE t3 MODIFY c2 INT STORAGE MEMORY;
Query OK, 0 rows affected (3.14 sec)
Records: 0 Duplicates: 0 Warnings: 0
You can make an in-memory column into a disk-based column by using 'STORAGE DISK' in a similar fashion.
Column 'c1' uses disk-based storage, since this is the default for the table (determined by the table-level 'STORAGE DISK' clause in the note 'CREATE TABLE': create-table. statement). However, column 'c2' uses in-memory storage, as can be seen here in the output of SHOW note 'CREATE TABLE': create-table.:
mysql> SHOW CREATE TABLE t3\G
*************************** 1. row ***************************
Table: t3
Create Table: CREATE TABLE `t3` (
`c1` int(11) DEFAULT NULL,
`c2` int(11) /*!50120 STORAGE MEMORY */ DEFAULT NULL
) /*!50100 TABLESPACE ts_1 STORAGE DISK */ ENGINE=ndbcluster DEFAULT CHARSET=latin1
1 row in set (0.02 sec)
When you add an 'AUTO_INCREMENT' column, column values are filled in with sequence numbers automatically. For 'MyISAM' tables, you can set the first sequence number by executing 'SET INSERT_ID=VALUE' before *note 'ALTER TABLE': alter-table. or by using the 'AUTO_INCREMENT=VALUE' table option.
With 'MyISAM' tables, if you do not change the 'AUTO_INCREMENT' column, the sequence number is not affected. If you drop an 'AUTO_INCREMENT' column and then add another 'AUTO_INCREMENT' column, the numbers are resequenced beginning with 1.
When replication is used, adding an 'AUTO_INCREMENT' column to a table might not produce the same ordering of the rows on the replica and the source. This occurs because the order in which the rows are numbered depends on the specific storage engine used for the table and the order in which the rows were inserted. If it is important to have the same order on the source and replica, the rows must be ordered before assigning an 'AUTO_INCREMENT' number. Assuming that you want to add an 'AUTO_INCREMENT' column to the table 't1', the following statements produce a new table 't2' identical to 't1' but with an 'AUTO_INCREMENT' column:
CREATE TABLE t2 (id INT AUTO_INCREMENT PRIMARY KEY)
SELECT * FROM t1 ORDER BY col1, col2;
This assumes that the table 't1' has columns 'col1' and 'col2'.
This set of statements also produces a new table 't2' identical to 't1', with the addition of an 'AUTO_INCREMENT' column:
CREATE TABLE t2 LIKE t1;
ALTER TABLE t2 ADD id INT AUTO_INCREMENT PRIMARY KEY;
INSERT INTO t2 SELECT * FROM t1 ORDER BY col1, col2;
Important:
To guarantee the same ordering on both source and replica, all columns of 't1' must be referenced in the 'ORDER BY' clause.
Regardless of the method used to create and populate the copy having the 'AUTO_INCREMENT' column, the final step is to drop the original table and then rename the copy:
DROP TABLE t1;
ALTER TABLE t2 RENAME t1;
File: manual.info.tmp, Node: alter-tablespace, Next: alter-view, Prev: alter-table, Up: sql-data-definition-statements
ALTER TABLESPACE TABLESPACE_NAME
{ADD | DROP} DATAFILE 'FILE_NAME'
[INITIAL_SIZE [=] SIZE]
[WAIT]
ENGINE [=] ENGINE_NAME
This statement is used either to add a new data file, or to drop a data file from a tablespace.
The 'ADD DATAFILE' variant enables you to specify an initial size using an 'INITIAL_SIZE' clause, where SIZE is measured in bytes; the default value is 134217728 (128 MB). You may optionally follow SIZE with a one-letter abbreviation for an order of magnitude, similar to those used in 'my.cnf'. Generally, this is one of the letters 'M' (megabytes) or 'G' (gigabytes).
Note:
All NDB Cluster Disk Data objects share the same namespace. This means that each Disk Data object must be uniquely named (and not merely each Disk Data object of a given type). For example, you cannot have a tablespace and a data file with the same name, or an undo log file and a tablespace with the same name.
On 32-bit systems, the maximum supported value for 'INITIAL_SIZE' is 4294967296 (4 GB). (Bug #29186)
'INITIAL_SIZE' is rounded, explicitly, as for *note 'CREATE TABLESPACE': create-tablespace.
Once a data file has been created, its size cannot be changed; however, you can add more data files to the tablespace using additional 'ALTER TABLESPACE ... ADD DATAFILE' statements.
Using 'DROP DATAFILE' with note 'ALTER TABLESPACE': alter-tablespace. drops the data file 'FILE_NAME' from the tablespace. You cannot drop a data file from a tablespace which is in use by any table; in other words, the data file must be empty (no extents used). See note mysql-cluster-disk-data-objects::. In addition, any data file to be dropped must previously have been added to the tablespace with note 'CREATE TABLESPACE': create-tablespace. or note 'ALTER TABLESPACE': alter-tablespace.
Both 'ALTER TABLESPACE ... ADD DATAFILE' and 'ALTER TABLESPACE ... DROP DATAFILE' require an 'ENGINE' clause which specifies the storage engine used by the tablespace. Currently, the only accepted values for ENGINE_NAME are note 'NDB': mysql-cluster. and note 'NDBCLUSTER': mysql-cluster.
'WAIT' is parsed but otherwise ignored, and so has no effect in MySQL 5.7. It is intended for future expansion.
When 'ALTER TABLESPACE ... ADD DATAFILE' is used with 'ENGINE = NDB', a data file is created on each Cluster data node. You can verify that the data files were created and obtain information about them by querying the Information Schema *note 'FILES': information-schema-files-table. table. For example, the following query shows all data files belonging to the tablespace named 'newts':
mysql> SELECT LOGFILE_GROUP_NAME, FILE_NAME, EXTRA
-> FROM INFORMATION_SCHEMA.FILES
-> WHERE TABLESPACE_NAME = 'newts' AND FILE_TYPE = 'DATAFILE';
+--------------------+--------------+----------------+
| LOGFILE_GROUP_NAME | FILE_NAME | EXTRA |
+--------------------+--------------+----------------+
| lg_3 | newdata.dat | CLUSTER_NODE=3 |
| lg_3 | newdata.dat | CLUSTER_NODE=4 |
| lg_3 | newdata2.dat | CLUSTER_NODE=3 |
| lg_3 | newdata2.dat | CLUSTER_NODE=4 |
+--------------------+--------------+----------------+
2 rows in set (0.03 sec)
See *note information-schema-files-table::.
note 'ALTER TABLESPACE': alter-tablespace. is useful only with Disk Data storage for NDB Cluster. See note mysql-cluster-disk-data::.
File: manual.info.tmp, Node: alter-view, Next: create-database, Prev: alter-tablespace, Up: sql-data-definition-statements
ALTER
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = USER]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW VIEW_NAME [(COLUMN_LIST)]
AS SELECT_STATEMENT
[WITH [CASCADED | LOCAL] CHECK OPTION]
This statement changes the definition of a view, which must exist. The syntax is similar to that for note 'CREATE VIEW': create-view. see note create-view::). This statement requires the 'CREATE VIEW' and 'DROP' privileges for the view, and some privilege for each column referred to in the note 'SELECT': select. statement. note 'ALTER VIEW': alter-view. is permitted only to the definer or users with the 'SUPER' privilege.
File: manual.info.tmp, Node: create-database, Next: create-event, Prev: alter-view, Up: sql-data-definition-statements
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] DB_NAME
[CREATE_OPTION] ...
CREATE_OPTION: [DEFAULT] {
CHARACTER SET [=] CHARSET_NAME
| COLLATE [=] COLLATION_NAME
}
note 'CREATE DATABASE': create-database. creates a database with the given name. To use this statement, you need the 'CREATE' privilege for the database. note 'CREATE SCHEMA': create-database. is a synonym for *note 'CREATE DATABASE': create-database.
An error occurs if the database exists and you did not specify 'IF NOT EXISTS'.
note 'CREATE DATABASE': create-database. is not permitted within a session that has an active note 'LOCK TABLES': lock-tables. statement.
Each CREATE_OPTION specifies a database characteristic. Database characteristics are stored in the 'db.opt' file in the database directory. The 'CHARACTER SET' option specifies the default database character set. The 'COLLATE' option specifies the default database collation. For information about character set and collation names, see *note charset::.
To see the available character sets and collations, use the note 'SHOW CHARACTER SET': show-character-set. and note 'SHOW COLLATION': show-collation. statements, respectively. See note show-character-set::, and note show-collation::.
A database in MySQL is implemented as a directory containing files that correspond to tables in the database. Because there are no tables in a database when it is initially created, the note 'CREATE DATABASE': create-database. statement creates only a directory under the MySQL data directory and the 'db.opt' file. Rules for permissible database names are given in note identifiers::. If a database name contains special characters, the name for the database directory contains encoded versions of those characters as described in *note identifier-mapping::.
If you manually create a directory under the data directory (for example, with 'mkdir'), the server considers it a database directory and it shows up in the output of *note 'SHOW DATABASES': show-databases.
When you create a database, let the server manage the directory and the files in it. Manipulating database directories and files directly can cause inconsistencies and unexpected results.
MySQL has no limit on the number of databases. The underlying file system may have a limit on the number of directories.
You can also use the note 'mysqladmin': mysqladmin. program to create databases. See note mysqladmin::.
File: manual.info.tmp, Node: create-event, Next: create-function, Prev: create-database, Up: sql-data-definition-statements
CREATE
[DEFINER = USER]
EVENT
[IF NOT EXISTS]
EVENT_NAME
ON SCHEDULE SCHEDULE
[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE | DISABLE ON SLAVE]
[COMMENT 'STRING']
DO EVENT_BODY;
SCHEDULE: {
AT TIMESTAMP [+ INTERVAL INTERVAL] ...
| EVERY INTERVAL
[STARTS TIMESTAMP [+ INTERVAL INTERVAL] ...]
[ENDS TIMESTAMP [+ INTERVAL INTERVAL] ...]
}
INTERVAL:
QUANTITY {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |
WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |
DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}
This statement creates and schedules a new event. The event does not run unless the Event Scheduler is enabled. For information about checking Event Scheduler status and enabling it if necessary, see *note events-configuration::.
note 'CREATE EVENT': create-event. requires the 'EVENT' privilege for the schema in which the event is to be created. If the 'DEFINER' clause is present, the privileges required depend on the USER value, as discussed in note stored-objects-security::.
The minimum requirements for a valid *note 'CREATE EVENT': create-event. statement are as follows:
The keywords *note 'CREATE EVENT': create-event. plus an event name, which uniquely identifies the event in a database schema.
An 'ON SCHEDULE' clause, which determines when and how often the event executes.
A *note 'DO': do. clause, which contains the SQL statement to be executed by an event.
This is an example of a minimal *note 'CREATE EVENT': create-event. statement:
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
UPDATE myschema.mytable SET mycol = mycol + 1;
The previous statement creates an event named 'myevent'. This event executes once--one hour following its creation--by running an SQL statement that increments the value of the 'myschema.mytable' table's 'mycol' column by 1.
The EVENT_NAME must be a valid MySQL identifier with a maximum length of 64 characters. Event names are not case-sensitive, so you cannot have two events named 'myevent' and 'MyEvent' in the same schema. In general, the rules governing event names are the same as those for names of stored routines. See *note identifiers::.
An event is associated with a schema. If no schema is indicated as part of EVENT_NAME, the default (current) schema is assumed. To create an event in a specific schema, qualify the event name with a schema using 'SCHEMA_NAME.EVENT_NAME' syntax.
The 'DEFINER' clause specifies the MySQL account to be used when checking access privileges at event execution time. If the 'DEFINER' clause is present, the USER value should be a MySQL account specified as ''USER_NAME'@'HOST_NAME'', 'CURRENT_USER', or 'CURRENT_USER()'. The permitted USER values depend on the privileges you hold, as discussed in *note stored-objects-security::. Also see that section for additional information about event security.
If the 'DEFINER' clause is omitted, the default definer is the user who executes the *note 'CREATE EVENT': create-event. statement. This is the same as specifying 'DEFINER = CURRENT_USER' explicitly.
Within an event body, the 'CURRENT_USER' function returns the account used to check privileges at event execution time, which is the 'DEFINER' user. For information about user auditing within events, see *note account-activity-auditing::.
'IF NOT EXISTS' has the same meaning for note 'CREATE EVENT': create-event. as for note 'CREATE TABLE': create-table.: If an event named EVENT_NAME already exists in the same schema, no action is taken, and no error results. (However, a warning is generated in such cases.)
The 'ON SCHEDULE' clause determines when, how often, and for how long the EVENT_BODY defined for the event repeats. This clause takes one of two forms:
'AT TIMESTAMP' is used for a one-time event. It specifies that the event executes one time only at the date and time given by TIMESTAMP, which must include both the date and time, or must be an expression that resolves to a datetime value. You may use a value of either the note 'DATETIME': datetime. or note 'TIMESTAMP': datetime. type for this purpose. If the date is in the past, a warning occurs, as shown here:
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2006-02-10 23:59:01 |
+---------------------+
1 row in set (0.04 sec)
mysql> CREATE EVENT e_totals
-> ON SCHEDULE AT '2006-02-10 23:59:00'
-> DO INSERT INTO test.totals VALUES (NOW());
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> SHOW WARNINGS\G
*************************** 1. row ***************************
Level: Note
Code: 1588
Message: Event execution time is in the past and ON COMPLETION NOT
PRESERVE is set. The event was dropped immediately after
creation.
*note 'CREATE EVENT': create-event. statements which are themselves invalid--for whatever reason--fail with an error.
You may use 'CURRENT_TIMESTAMP' to specify the current date and time. In such a case, the event acts as soon as it is created.
To create an event which occurs at some point in the future relative to the current date and time--such as that expressed by the phrase 'three weeks from now'--you can use the optional clause '+ INTERVAL INTERVAL'. The INTERVAL portion consists of two parts, a quantity and a unit of time, and follows the syntax rules described in *note temporal-intervals::, except that you cannot use any units keywords that involving microseconds when defining an event. With some interval types, complex time units may be used. For example, 'two minutes and ten seconds' can be expressed as '+ INTERVAL '2:10' MINUTE_SECOND'.
You can also combine intervals. For example, 'AT CURRENT_TIMESTAMPTo repeat actions at a regular interval, use an 'EVERY' clause. The 'EVERY' keyword is followed by an INTERVAL as described in the previous discussion of the 'AT' keyword. ('+ INTERVAL' is not used with 'EVERY'.) For example, 'EVERY 6 WEEK' means 'every six weeks'.
Although '+ INTERVAL' clauses are not permitted in an 'EVERY' clause, you can use the same complex time units permitted in a '+ INTERVAL'.
An 'EVERY' clause may contain an optional 'STARTS' clause. 'STARTS' is followed by a TIMESTAMP value that indicates when the action should begin repeating, and may also use '+ INTERVAL INTERVAL' to specify an amount of time 'from now'. For example, 'EVERY 3 MONTH STARTS CURRENT_TIMESTAMP + INTERVAL 1 WEEK' means 'every three months, beginning one week from now'. Similarly, you can express 'every two weeks, beginning six hours and fifteen minutes from now' as 'EVERY 2 WEEK STARTS CURRENT_TIMESTAMP + INTERVAL '6:15' HOUR_MINUTE'. Not specifying 'STARTS' is the same as using 'STARTS CURRENT_TIMESTAMP'--that is, the action specified for the event begins repeating immediately upon creation of the event.
An 'EVERY' clause may contain an optional 'ENDS' clause. The 'ENDS' keyword is followed by a TIMESTAMP value that tells MySQL when the event should stop repeating. You may also use '+ INTERVAL INTERVAL' with 'ENDS'; for instance, 'EVERY 12 HOUR STARTS CURRENT_TIMESTAMP + INTERVAL 30 MINUTE ENDS CURRENT_TIMESTAMP + INTERVAL 4 WEEK' is equivalent to 'every twelve hours, beginning thirty minutes from now, and ending four weeks from now'. Not using 'ENDS' means that the event continues executing indefinitely.
'ENDS' supports the same syntax for complex time units as 'STARTS' does.
You may use 'STARTS', 'ENDS', both, or neither in an 'EVERY' clause.
If a repeating event does not terminate within its scheduling interval, the result may be multiple instances of the event executing simultaneously. If this is undesirable, you should institute a mechanism to prevent simultaneous instances. For example, you could use the 'GET_LOCK()' function, or row or table locking.
The 'ON SCHEDULE' clause may use expressions involving built-in MySQL functions and user variables to obtain any of the TIMESTAMP or INTERVAL values which it contains. You may not use stored functions or loadable functions in such expressions, nor may you use any table references; however, you may use 'SELECT FROM DUAL'. This is true for both note 'CREATE EVENT': create-event. and note 'ALTER EVENT': alter-event. statements. References to stored functions, loadable functions, and tables in such cases are specifically not permitted, and fail with an error (see Bug #22830).
Times in the 'ON SCHEDULE' clause are interpreted using the current session 'time_zone' value. This becomes the event time zone; that is, the time zone that is used for event scheduling and is in effect within the event as it executes. These times are converted to UTC and stored along with the event time zone in the 'mysql.event' table. This enables event execution to proceed as defined regardless of any subsequent changes to the server time zone or daylight saving time effects. For additional information about representation of event times, see note events-metadata::. See also note show-events::, and *note information-schema-events-table::.
Normally, once an event has expired, it is immediately dropped. You can override this behavior by specifying 'ON COMPLETION PRESERVE'. Using 'ON COMPLETION NOT PRESERVE' merely makes the default nonpersistent behavior explicit.
You can create an event but prevent it from being active using the 'DISABLE' keyword. Alternatively, you can use 'ENABLE' to make explicit the default status, which is active. This is most useful in conjunction with note 'ALTER EVENT': alter-event. (see note alter-event::).
A third value may also appear in place of 'ENABLE' or 'DISABLE'; 'DISABLE ON SLAVE' is set for the status of an event on a replica to indicate that the event was created on the source and replicated to the replica, but is not executed on the replica. See *note replication-features-invoked::.
You may supply a comment for an event using a 'COMMENT' clause. COMMENT may be any string of up to 64 characters that you wish to use for describing the event. The comment text, being a string literal, must be surrounded by quotation marks.
The note 'DO': do. clause specifies an action carried by the event, and consists of an SQL statement. Nearly any valid MySQL statement that can be used in a stored routine can also be used as the action statement for a scheduled event. (See note stored-program-restrictions::.) For example, the following event 'e_hourly' deletes all rows from the 'sessions' table once per hour, where this table is part of the 'site_activity' schema:
CREATE EVENT e_hourly
ON SCHEDULE
EVERY 1 HOUR
COMMENT 'Clears out sessions table each hour.'
DO
DELETE FROM site_activity.sessions;
MySQL stores the 'sql_mode' system variable setting in effect when an event is created or altered, and always executes the event with this setting in force, regardless of the current server SQL mode when the event begins executing.
A note 'CREATE EVENT': create-event. statement that contains an note 'ALTER EVENT': alter-event. statement in its *note 'DO': do. clause appears to succeed; however, when the server attempts to execute the resulting scheduled event, the execution fails with an error.
Note:
Statements such as note 'SELECT': select. or note 'SHOW': show. that merely return a result set have no effect when used in an event; the output from these is not sent to the MySQL Monitor, nor is it stored anywhere. However, you can use statements such as note 'SELECT ... INTO': select. and note 'INSERT INTO ... SELECT': insert-select. that store a result. (See the next example in this section for an instance of the latter.)
The schema to which an event belongs is the default schema for table references in the *note 'DO': do. clause. Any references to tables in other schemas must be qualified with the proper schema name.
As with stored routines, you can use compound-statement syntax in the *note 'DO': do. clause by using the 'BEGIN' and 'END' keywords, as shown here:
delimiter |
CREATE EVENT e_daily
ON SCHEDULE
EVERY 1 DAY
COMMENT 'Saves total number of sessions then clears the table each day'
DO
BEGIN
INSERT INTO site_activity.totals (time, total)
SELECT CURRENT_TIMESTAMP, COUNT(*)
FROM site_activity.sessions;
DELETE FROM site_activity.sessions;
END |
delimiter ;
This example uses the 'delimiter' command to change the statement delimiter. See *note stored-programs-defining::.
More complex compound statements, such as those used in stored routines, are possible in an event. This example uses local variables, an error handler, and a flow control construct:
delimiter |
CREATE EVENT e
ON SCHEDULE
EVERY 5 SECOND
DO
BEGIN
DECLARE v INTEGER;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END;
SET v = 0;
WHILE v < 5 DO
INSERT INTO t1 VALUES (0);
UPDATE t2 SET s1 = s1 + 1;
SET v = v + 1;
END WHILE;
END |
delimiter ;
There is no way to pass parameters directly to or from events; however, it is possible to invoke a stored routine with parameters within an event:
CREATE EVENT e_call_myproc
ON SCHEDULE
AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
DO CALL myproc(5, 27);
If an event's definer has privileges sufficient to set global system variables (see *note system-variable-privileges::), the event can read and write global variables. As granting such privileges entails a potential for abuse, extreme care must be taken in doing so.
Generally, any statements that are valid in stored routines may be used for action statements executed by events. For more information about statements permissible within stored routines, see *note stored-routines-syntax::. You can create an event as part of a stored routine, but an event cannot be created by another event.
File: manual.info.tmp, Node: create-function, Next: create-index, Prev: create-event, Up: sql-data-definition-statements
The *note 'CREATE FUNCTION': create-function. statement is used to create stored functions and loadable functions:
For information about creating stored functions, see *note create-procedure::.
For information about creating loadable functions, see *note create-function-loadable::.
File: manual.info.tmp, Node: create-index, Next: create-logfile-group, Prev: create-function, Up: sql-data-definition-statements
CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX INDEX_NAME
[INDEX_TYPE]
ON TBL_NAME (KEY_PART,...)
[INDEX_OPTION]
[ALGORITHM_OPTION | LOCK_OPTION] ...
KEY_PART:
COL_NAME [(LENGTH)] [ASC | DESC]
INDEX_OPTION: {
KEY_BLOCK_SIZE [=] VALUE
| INDEX_TYPE
| WITH PARSER PARSER_NAME
| COMMENT 'STRING'
}
INDEX_TYPE:
USING {BTREE | HASH}
ALGORITHM_OPTION:
ALGORITHM [=] {DEFAULT | INPLACE | COPY}
LOCK_OPTION:
LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}
Normally, you create all indexes on a table at the time the table itself is created with note 'CREATE TABLE': create-table. See note create-table::. This guideline is especially important for note 'InnoDB': innodb-storage-engine. tables, where the primary key determines the physical layout of rows in the data file. note 'CREATE INDEX': create-index. enables you to add indexes to existing tables.
note 'CREATE INDEX': create-index. is mapped to an note 'ALTER TABLE': alter-table. statement to create indexes. See note alter-table::. note 'CREATE INDEX': create-index. cannot be used to create a 'PRIMARY KEY'; use note 'ALTER TABLE': alter-table. instead. For more information about indexes, see note mysql-indexes::.
note 'InnoDB': innodb-storage-engine. supports secondary indexes on virtual columns. For more information, see note create-table-secondary-indexes::.
When the 'innodb_stats_persistent' setting is enabled, run the note 'ANALYZE TABLE': analyze-table. statement for an note 'InnoDB': innodb-storage-engine. table after creating an index on that table.
An index specification of the form '(KEY_PART1, KEY_PART2, ...)' creates an index with multiple key parts. Index key values are formed by concatenating the values of the given key parts. For example '(col1, col2, col3)' specifies a multiple-column index with index keys consisting of values from 'col1', 'col2', and 'col3'.
A KEY_PART specification can end with 'ASC' or 'DESC'. These keywords are permitted for future extensions for specifying ascending or descending index value storage. Currently, they are parsed but ignored; index values are always stored in ascending order.
The following sections describe different aspects of the *note 'CREATE INDEX': create-index. statement:
*note create-index-column-prefixes::
*note create-index-unique::
*note create-index-fulltext::
*note create-index-spatial::
*note create-index-options::
*note create-index-copying::
Column Prefix Key Parts
For string columns, indexes can be created that use only the leading part of column values, using 'COL_NAME(LENGTH)' syntax to specify an index prefix length:
Prefixes can be specified for note 'CHAR': char, note 'VARCHAR': char, note 'BINARY': binary-varbinary, and note 'VARBINARY': binary-varbinary. key parts.
Prefixes must be specified for note 'BLOB': blob. and note 'TEXT': blob. key parts. Additionally, note 'BLOB': blob. and note 'TEXT': blob. columns can be indexed only for 'InnoDB', 'MyISAM', and 'BLACKHOLE' tables.
Prefix limits are measured in bytes. However, prefix lengths for index specifications in note 'CREATE TABLE': create-table, note 'ALTER TABLE': alter-table, and note 'CREATE INDEX': create-index. statements are interpreted as number of characters for nonbinary string types (note 'CHAR': char, note 'VARCHAR': char, note 'TEXT': blob.) and number of bytes for binary string types (note 'BINARY': binary-varbinary, note 'VARBINARY': binary-varbinary, *note 'BLOB': blob.). Take this into account when specifying a prefix length for a nonbinary string column that uses a multibyte character set.
Prefix support and lengths of prefixes (where supported) are storage engine dependent. For example, a prefix can be up to 767 bytes long for note 'InnoDB': innodb-storage-engine. tables or 3072 bytes if the 'innodb_large_prefix' option is enabled. For note 'MyISAM': myisam-storage-engine. tables, the prefix length limit is 1000 bytes. The note 'NDB': mysql-cluster. storage engine does not support prefixes (see note mysql-cluster-limitations-unsupported::).
As of MySQL 5.7.17, if a specified index prefix exceeds the maximum column data type size, *note 'CREATE INDEX': create-index. handles the index as follows:
For a nonunique index, either an error occurs (if strict SQL mode is enabled), or the index length is reduced to lie within the maximum column data type size and a warning is produced (if strict SQL mode is not enabled).
For a unique index, an error occurs regardless of SQL mode because reducing the index length might enable insertion of nonunique entries that do not meet the specified uniqueness requirement.
The statement shown here creates an index using the first 10 characters of the 'name' column (assuming that 'name' has a nonbinary string type):
CREATE INDEX part_of_name ON customer (name(10));
If names in the column usually differ in the first 10 characters, lookups performed using this index should not be much slower than using an index created from the entire 'name' column. Also, using column prefixes for indexes can make the index file much smaller, which could save a lot of disk space and might also speed up *note 'INSERT': insert. operations.
Unique Indexes
A 'UNIQUE' index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. If you specify a prefix value for a column in a 'UNIQUE' index, the column values must be unique within the prefix length. A 'UNIQUE' index permits multiple 'NULL' values for columns that can contain 'NULL'.
If a table has a 'PRIMARY KEY' or 'UNIQUE NOT NULL' index that consists of a single column that has an integer type, you can use '_rowid' to refer to the indexed column in *note 'SELECT': select. statements, as follows:
'_rowid' refers to the 'PRIMARY KEY' column if there is a 'PRIMARY KEY' consisting of a single integer column. If there is a 'PRIMARY KEY' but it does not consist of a single integer column, '_rowid' cannot be used.
Otherwise, '_rowid' refers to the column in the first 'UNIQUE NOT NULL' index if that index consists of a single integer column. If the first 'UNIQUE NOT NULL' index does not consist of a single integer column, '_rowid' cannot be used.
Full-Text Indexes
'FULLTEXT' indexes are supported only for note 'InnoDB': innodb-storage-engine. and note 'MyISAM': myisam-storage-engine. tables and can include only note 'CHAR': char, note 'VARCHAR': char, and note 'TEXT': blob. columns. Indexing always happens over the entire column; column prefix indexing is not supported and any prefix length is ignored if specified. See note fulltext-search::, for details of operation.
Spatial Indexes
The note 'MyISAM': myisam-storage-engine, note 'InnoDB': innodb-storage-engine, note 'NDB': mysql-cluster, and note 'ARCHIVE': archive-storage-engine. storage engines support spatial columns such as note 'POINT': spatial-type-overview. and note 'GEOMETRY': spatial-type-overview. (*note spatial-types::, describes the spatial data types.) However, support for spatial column indexing varies among engines. Spatial and nonspatial indexes on spatial columns are available according to the following rules.
Spatial indexes on spatial columns (created using 'SPATIAL INDEX') have these characteristics:
Available only for note 'MyISAM': myisam-storage-engine. and note 'InnoDB': innodb-storage-engine. tables. Specifying 'SPATIAL INDEX' for other storage engines results in an error.
Indexed columns must be 'NOT NULL'.
Column prefix lengths are prohibited. The full width of each column is indexed.
Nonspatial indexes on spatial columns (created with 'INDEX', 'UNIQUE', or 'PRIMARY KEY') have these characteristics:
Permitted for any storage engine that supports spatial columns except *note 'ARCHIVE': archive-storage-engine.
Columns can be 'NULL' unless the index is a primary key.
For each spatial column in a non-'SPATIAL' index except note 'POINT': spatial-type-overview. columns, a column prefix length must be specified. (This is the same requirement as for indexed note 'BLOB': blob. columns.) The prefix length is given in bytes.
The index type for a non-'SPATIAL' index depends on the storage engine. Currently, B-tree is used.
Permitted for a column that can have 'NULL' values only for note 'InnoDB': innodb-storage-engine, note 'MyISAM': myisam-storage-engine, and *note 'MEMORY': memory-storage-engine. tables.
Index Options
Following the key part list, index options can be given. An INDEX_OPTION value can be any of the following:
'KEY_BLOCK_SIZE [=] VALUE'
For *note 'MyISAM': myisam-storage-engine. tables, 'KEY_BLOCK_SIZE' optionally specifies the size in bytes to use for index key blocks. The value is treated as a hint; a different size could be used if necessary. A 'KEY_BLOCK_SIZE' value specified for an individual index definition overrides a table-level 'KEY_BLOCK_SIZE' value.
'KEY_BLOCK_SIZE' is not supported at the index level for note 'InnoDB': innodb-storage-engine. tables. See note create-table::.
INDEX_TYPE
Some storage engines permit you to specify an index type when creating an index. For example:
CREATE TABLE lookup (id INT) ENGINE = MEMORY;
CREATE INDEX id_index ON lookup (id) USING BTREE;
*note create-index-storage-engine-index-types:: shows the permissible index type values supported by different storage engines. Where multiple index types are listed, the first one is the default when no index type specifier is given. Storage engines not listed in the table do not support an INDEX_TYPE clause in index definitions.
Index Types Per Storage Engine
Storage Permissible Index Types Engine
*note 'InnoDB': innodb-storage-engine.'BTREE'
*note 'MyISAM': myisam-storage-engine.'BTREE'
*note 'MEMORY': memory-storage-engine./'HEAP''HASH', 'BTREE'
*note 'NDB': mysql-cluster.'HASH', 'BTREE' (see note in text)
The INDEX_TYPE clause cannot be used for 'FULLTEXT INDEX' or 'SPATIAL INDEX' specifications. Full-text index implementation is storage engine dependent. Spatial indexes are implemented as R-tree indexes.
'BTREE' indexes are implemented by the *note 'NDB': mysql-cluster. storage engine as T-tree indexes.
Note:
For indexes on note 'NDB': mysql-cluster. table columns, the 'USING' option can be specified only for a unique index or primary key. 'USING HASH' prevents the creation of an ordered index; otherwise, creating a unique index or primary key on an note 'NDB': mysql-cluster. table automatically results in the creation of both an ordered index and a hash index, each of which indexes the same set of columns.
For unique indexes that include one or more 'NULL' columns of an *note 'NDB': mysql-cluster. table, the hash index can be used only to look up literal values, which means that 'IS [NOT] NULL' conditions require a full scan of the table. One workaround is to make sure that a unique index using one or more 'NULL' columns on such a table is always created in such a way that it includes the ordered index; that is, avoid employing 'USING HASH' when creating the index.
If you specify an index type that is not valid for a given storage engine, but another index type is available that the engine can use without affecting query results, the engine uses the available type. The parser recognizes 'RTREE' as a type name, but currently this cannot be specified for any storage engine.
Note:
Use of the INDEX_TYPE option before the 'ON TBL_NAME' clause is deprecated; you should expect support for use of the option in this position to be removed in a future MySQL release. If an INDEX_TYPE option is given in both the earlier and later positions, the final option applies.
'TYPE TYPE_NAME' is recognized as a synonym for 'USING TYPE_NAME'. However, 'USING' is the preferred form.
The following tables show index characteristics for the storage engines that support the INDEX_TYPE option.
InnoDB Storage Engine Index Characteristics
Index Index Stores Permits IS NULL Scan IS NOT NULL Class Type NULL Multiple Type Scan Type VALUES NULL Values
Primary key
'BTREE' No No N/A N/A
Unique
'BTREE' Yes Yes Index Index
Key
'BTREE' Yes Yes Index Index
'FULLTEXT'
N/A Yes Yes Table Table
'SPATIAL'
N/A No No N/A N/A
MyISAM Storage Engine Index Characteristics
Index Index Stores Permits IS NULL Scan IS NOT NULL Class Type NULL Multiple Type Scan Type VALUES NULL Values
Primary key
'BTREE' No No N/A N/A
Unique
'BTREE' Yes Yes Index Index
Key
'BTREE' Yes Yes Index Index
'FULLTEXT'
N/A Yes Yes Table Table
'SPATIAL'
N/A No No N/A N/A
MEMORY Storage Engine Index Characteristics
Index Index Stores Permits IS NULL Scan IS NOT NULL Class Type NULL Multiple Type Scan Type VALUES NULL Values
Primary key
'BTREE' No No N/A N/A
Unique
'BTREE' Yes Yes Index Index
Key
'BTREE' Yes Yes Index Index
Primary key
'HASH' No No N/A N/A
Unique
'HASH' Yes Yes Index Index
Key
'HASH' Yes Yes Index Index
NDB Storage Engine Index Characteristics
Index Index Stores Permits IS NULL Scan IS NOT NULL Class Type NULL Multiple Type Scan Type VALUES NULL Values
Primary key
'BTREE' No No Index Index
Unique
'BTREE' Yes Yes Index Index
Key
'BTREE' Yes Yes Index Index
Primary key
'HASH' No No Table (see Table (see note 1) note 1)
Unique
'HASH' Yes Yes Table (see Table (see note 1) note 1)
Key
'HASH' Yes Yes Table (see Table (see note 1) note 1)
Table note:
'WITH PARSER PARSER_NAME'
This option can be used only with 'FULLTEXT' indexes. It associates a parser plugin with the index if full-text indexing and searching operations need special handling. note 'InnoDB': innodb-storage-engine. and note 'MyISAM': myisam-storage-engine. support full-text parser plugins. If you have a *note 'MyISAM': myisam-storage-engine. table with an associated full-text parser plugin, you can convert the table to 'InnoDB' using 'ALTER TABLE'. See Full-Text Parser Plugins (https://dev.mysql.com/doc/extending-mysql/5.7/en/plugin-types.html#full-text-plugin-type) and Writing Full-Text Parser Plugins (https://dev.mysql.com/doc/extending-mysql/5.7/en/writing-full-text-plugins.html) for more information.
'COMMENT 'STRING''
Index definitions can include an optional comment of up to 1024 characters.
The note 'MERGE_THRESHOLD': index-page-merge-threshold. for index pages can be configured for individual indexes using the INDEX_OPTION 'COMMENT' clause of the note 'CREATE INDEX': create-index. statement. For example:
CREATE TABLE t1 (id INT);
CREATE INDEX id_index ON t1 (id) COMMENT 'MERGE_THRESHOLD=40';
If the page-full percentage for an index page falls below the 'MERGE_THRESHOLD' value when a row is deleted or when a row is shortened by an update operation, *note 'InnoDB': innodb-storage-engine. attempts to merge the index page with a neighboring index page. The default 'MERGE_THRESHOLD' value is 50, which is the previously hardcoded value.
'MERGE_THRESHOLD' can also be defined at the index level and table level using note 'CREATE TABLE': create-table. and note 'ALTER TABLE': alter-table. statements. For more information, see *note index-page-merge-threshold::.
Table Copying and Locking Options
'ALGORITHM' and 'LOCK' clauses may be given to influence the table copying method and level of concurrency for reading and writing the table while its indexes are being modified. They have the same meaning as for the note 'ALTER TABLE': alter-table. statement. For more information, see note alter-table::
NDB Cluster formerly supported online 'CREATE INDEX' operations using an alternative syntax that is no longer supported. NDB Cluster now supports online operations using the same 'ALGORITHM=INPLACE' syntax used with the standard MySQL Server. See *note mysql-cluster-online-operations::, for more information.
File: manual.info.tmp, Node: create-logfile-group, Next: create-procedure, Prev: create-index, Up: sql-data-definition-statements
CREATE LOGFILE GROUP LOGFILE_GROUP
ADD UNDOFILE 'UNDO_FILE'
[INITIAL_SIZE [=] INITIAL_SIZE]
[UNDO_BUFFER_SIZE [=] UNDO_BUFFER_SIZE]
[REDO_BUFFER_SIZE [=] REDO_BUFFER_SIZE]
[NODEGROUP [=] NODEGROUP_ID]
[WAIT]
[COMMENT [=] 'STRING']
ENGINE [=] ENGINE_NAME
This statement creates a new log file group named LOGFILE_GROUP having a single 'UNDO' file named 'UNDO_FILE'. A note 'CREATE LOGFILE GROUP': create-logfile-group. statement has one and only one 'ADD UNDOFILE' clause. For rules covering the naming of log file groups, see note identifiers::.
Note:
All NDB Cluster Disk Data objects share the same namespace. This means that each Disk Data object must be uniquely named (and not merely each Disk Data object of a given type). For example, you cannot have a tablespace and a log file group with the same name, or a tablespace and a data file with the same name.
There can be only one log file group per NDB Cluster instance at any given time.
The optional 'INITIAL_SIZE' parameter sets the 'UNDO' file's initial size; if not specified, it defaults to '128M' (128 megabytes). The optional 'UNDO_BUFFER_SIZE' parameter sets the size used by the 'UNDO' buffer for the log file group; The default value for 'UNDO_BUFFER_SIZE' is '8M' (eight megabytes); this value cannot exceed the amount of system memory available. Both of these parameters are specified in bytes. You may optionally follow either or both of these with a one-letter abbreviation for an order of magnitude, similar to those used in 'my.cnf'. Generally, this is one of the letters 'M' (for megabytes) or 'G' (for gigabytes).
Memory used for 'UNDO_BUFFER_SIZE' comes from the global pool whose size is determined by the value of the 'SharedGlobalMemory' data node configuration parameter. This includes any default value implied for this option by the setting of the 'InitialLogFileGroup' data node configuration parameter.
The maximum permitted for 'UNDO_BUFFER_SIZE' is 629145600 (600 MB).
On 32-bit systems, the maximum supported value for 'INITIAL_SIZE' is 4294967296 (4 GB). (Bug #29186)
The minimum allowed value for 'INITIAL_SIZE' is 1048576 (1 MB).
The 'ENGINE' option determines the storage engine to be used by this log file group, with ENGINE_NAME being the name of the storage engine. In MySQL 5.7, this must be note 'NDB': mysql-cluster. (or note 'NDBCLUSTER': mysql-cluster.). If 'ENGINE' is not set, MySQL tries to use the engine specified by the 'default_storage_engine' server system variable (formerly 'storage_engine'). In any case, if the engine is not specified as note 'NDB': mysql-cluster. or note 'NDBCLUSTER': mysql-cluster, the 'CREATE LOGFILE GROUP' statement appears to succeed but actually fails to create the log file group, as shown here:
mysql> CREATE LOGFILE GROUP lg1
-> ADD UNDOFILE 'undo.dat' INITIAL_SIZE = 10M;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> SHOW WARNINGS;
+-------+------+------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------------------------------------------------------------+
| Error | 1478 | Table storage engine 'InnoDB' does not support the create option 'TABLESPACE or LOGFILE GROUP' |
+-------+------+------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> DROP LOGFILE GROUP lg1 ENGINE = NDB;
ERROR 1529 (HY000): Failed to drop LOGFILE GROUP
mysql> CREATE LOGFILE GROUP lg1
-> ADD UNDOFILE 'undo.dat' INITIAL_SIZE = 10M
-> ENGINE = NDB;
Query OK, 0 rows affected (2.97 sec)
The fact that the 'CREATE LOGFILE GROUP' statement does not actually return an error when a non-'NDB' storage engine is named, but rather appears to succeed, is a known issue which we hope to address in a future release of NDB Cluster.
REDO_BUFFER_SIZE, 'NODEGROUP', 'WAIT', and 'COMMENT' are parsed but ignored, and so have no effect in MySQL 5.7. These options are intended for future expansion.
When used with 'ENGINE [=] NDB', a log file group and associated 'UNDO' log file are created on each Cluster data node. You can verify that the 'UNDO' files were created and obtain information about them by querying the Information Schema *note 'FILES': information-schema-files-table. table. For example:
mysql> SELECT LOGFILE_GROUP_NAME, LOGFILE_GROUP_NUMBER, EXTRA
-> FROM INFORMATION_SCHEMA.FILES
-> WHERE FILE_NAME = 'undo_10.dat';
+--------------------+----------------------+----------------+
| LOGFILE_GROUP_NAME | LOGFILE_GROUP_NUMBER | EXTRA |
+--------------------+----------------------+----------------+
| lg_3 | 11 | CLUSTER_NODE=3 |
| lg_3 | 11 | CLUSTER_NODE=4 |
+--------------------+----------------------+----------------+
2 rows in set (0.06 sec)
note 'CREATE LOGFILE GROUP': create-logfile-group. is useful only with Disk Data storage for NDB Cluster. See note mysql-cluster-disk-data::.
File: manual.info.tmp, Node: create-procedure, Next: create-server, Prev: create-logfile-group, Up: sql-data-definition-statements
CREATE
[DEFINER = USER]
PROCEDURE SP_NAME ([PROC_PARAMETER[,...]])
[CHARACTERISTIC ...] ROUTINE_BODY
CREATE
[DEFINER = USER]
FUNCTION SP_NAME ([FUNC_PARAMETER[,...]])
RETURNS TYPE
[CHARACTERISTIC ...] ROUTINE_BODY
PROC_PARAMETER:
[ IN | OUT | INOUT ] PARAM_NAME TYPE
FUNC_PARAMETER:
PARAM_NAME TYPE
TYPE:
ANY VALID MYSQL DATA TYPE
CHARACTERISTIC: {
COMMENT 'STRING'
| LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
}
ROUTINE_BODY:
VALID SQL ROUTINE STATEMENT
These statements are used to create a stored routine (a stored procedure or function). That is, the specified routine becomes known to the server. By default, a stored routine is associated with the default database. To associate the routine explicitly with a given database, specify the name as DB_NAME.SP_NAME when you create it.
The 'CREATE FUNCTION' statement is also used in MySQL to support loadable functions. See note create-function-loadable::. A loadable function can be regarded as an external stored function. Stored functions share their namespace with loadable functions. See note function-resolution::, for the rules describing how the server interprets references to different kinds of functions.
To invoke a stored procedure, use the note 'CALL': call. statement (see note call::). To invoke a stored function, refer to it in an expression. The function returns a value during expression evaluation.
note 'CREATE PROCEDURE': create-procedure. and note 'CREATE FUNCTION': create-function. require the 'CREATE ROUTINE' privilege. If the 'DEFINER' clause is present, the privileges required depend on the USER value, as discussed in note stored-objects-security::. If binary logging is enabled, note 'CREATE FUNCTION': create-function. might require the 'SUPER' privilege, as discussed in *note stored-programs-logging::.
By default, MySQL automatically grants the 'ALTER ROUTINE' and 'EXECUTE' privileges to the routine creator. This behavior can be changed by disabling the 'automatic_sp_privileges' system variable. See *note stored-routines-privileges::.
The 'DEFINER' and 'SQL SECURITY' clauses specify the security context to be used when checking access privileges at routine execution time, as described later in this section.
If the routine name is the same as the name of a built-in SQL function, a syntax error occurs unless you use a space between the name and the following parenthesis when defining the routine or invoking it later. For this reason, avoid using the names of existing SQL functions for your own stored routines.
The 'IGNORE_SPACE' SQL mode applies to built-in functions, not to stored routines. It is always permissible to have spaces after a stored routine name, regardless of whether 'IGNORE_SPACE' is enabled.
The parameter list enclosed within parentheses must always be present. If there are no parameters, an empty parameter list of '()' should be used. Parameter names are not case-sensitive.
Each parameter is an 'IN' parameter by default. To specify otherwise for a parameter, use the keyword 'OUT' or 'INOUT' before the parameter name.
Note:
Specifying a parameter as 'IN', 'OUT', or 'INOUT' is valid only for a 'PROCEDURE'. For a 'FUNCTION', parameters are always regarded as 'IN' parameters.
An 'IN' parameter passes a value into a procedure. The procedure might modify the value, but the modification is not visible to the caller when the procedure returns. An 'OUT' parameter passes a value from the procedure back to the caller. Its initial value is 'NULL' within the procedure, and its value is visible to the caller when the procedure returns. An 'INOUT' parameter is initialized by the caller, can be modified by the procedure, and any change made by the procedure is visible to the caller when the procedure returns.
For each 'OUT' or 'INOUT' parameter, pass a user-defined variable in the *note 'CALL': call. statement that invokes the procedure so that you can obtain its value when the procedure returns. If you are calling the procedure from within another stored procedure or function, you can also pass a routine parameter or local routine variable as an 'OUT' or 'INOUT' parameter. If you are calling the procedure from within a trigger, you can also pass 'NEW.COL_NAME' as an 'OUT' or 'INOUT' parameter.
For information about the effect of unhandled conditions on procedure parameters, see *note conditions-and-parameters::.
Routine parameters cannot be referenced in statements prepared within the routine; see *note stored-program-restrictions::.
The following example shows a simple stored procedure that, given a country code, counts the number of cities for that country that appear in the 'city' table of the 'world' database. The country code is passed using an 'IN' parameter, and the city count is returned using an 'OUT' parameter:
mysql> delimiter //
mysql> CREATE PROCEDURE citycount (IN country CHAR(3), OUT cities INT)
BEGIN
SELECT COUNT(*) INTO cities FROM world.city
WHERE CountryCode = country;
END//
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
mysql> CALL citycount('JPN', @cities); -- cities in Japan
Query OK, 1 row affected (0.00 sec)
mysql> SELECT @cities;
+---------+
| @cities |
+---------+
| 248 |
+---------+
1 row in set (0.00 sec)
mysql> CALL citycount('FRA', @cities); -- cities in France
Query OK, 1 row affected (0.00 sec)
mysql> SELECT @cities;
+---------+
| @cities |
+---------+
| 40 |
+---------+
1 row in set (0.00 sec)
The example uses the note 'mysql': mysql. client 'delimiter' command to change the statement delimiter from ';' to '//' while the procedure is being defined. This enables the ';' delimiter used in the procedure body to be passed through to the server rather than being interpreted by note 'mysql': mysql. itself. See *note stored-programs-defining::.
The 'RETURNS' clause may be specified only for a 'FUNCTION', for which it is mandatory. It indicates the return type of the function, and the function body must contain a 'RETURN VALUE' statement. If the note 'RETURN': return. statement returns a value of a different type, the value is coerced to the proper type. For example, if a function specifies an note 'ENUM': enum. or note 'SET': set. value in the 'RETURNS' clause, but the note 'RETURN': return. statement returns an integer, the value returned from the function is the string for the corresponding note 'ENUM': enum. member of set of note 'SET': set. members.
The following example function takes a parameter, performs an operation using an SQL function, and returns the result. In this case, it is unnecessary to use 'delimiter' because the function definition contains no internal ';' statement delimiters:
mysql> CREATE FUNCTION hello (s CHAR(20))
-> RETURNS CHAR(50) DETERMINISTIC
-> RETURN CONCAT('Hello, ',s,'!');
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT hello('world');
+----------------+
| hello('world') |
+----------------+
| Hello, world! |
+----------------+
1 row in set (0.00 sec)
Parameter types and function return types can be declared to use any valid data type. The 'COLLATE' attribute can be used if preceded by a 'CHARACTER SET' specification.
The ROUTINE_BODY consists of a valid SQL routine statement. This can be a simple statement such as note 'SELECT': select. or note 'INSERT': insert, or a compound statement written using 'BEGIN' and 'END'. Compound statements can contain declarations, loops, and other control structure statements. The syntax for these statements is described in note sql-compound-statements::. In practice, stored functions tend to use compound statements, unless the body consists of a single note 'RETURN': return. statement.
MySQL permits routines to contain DDL statements, such as 'CREATE' and 'DROP'. MySQL also permits stored procedures (but not stored functions) to contain SQL transaction statements such as *note 'COMMIT': commit. Stored functions may not contain statements that perform explicit or implicit commit or rollback. Support for these statements is not required by the SQL standard, which states that each DBMS vendor may decide whether to permit them.
Statements that return a result set can be used within a stored procedure but not within a stored function. This prohibition includes note 'SELECT': select. statements that do not have an 'INTO VAR_LIST' clause and other statements such as note 'SHOW': show, note 'EXPLAIN': explain, and note 'CHECK TABLE': check-table. For statements that can be determined at function definition time to return a result set, a 'Not allowed to return a result set from a function' error occurs ('ER_SP_NO_RETSET' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_sp_no_retset)). For statements that can be determined only at runtime to return a result set, a 'PROCEDURE %s can't return a result set in the given context' error occurs ('ER_SP_BADSELECT' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_sp_badselect)).
*note 'USE': use. statements within stored routines are not permitted. When a routine is invoked, an implicit 'USE DB_NAME' is performed (and undone when the routine terminates). The causes the routine to have the given default database while it executes. References to objects in databases other than the routine default database should be qualified with the appropriate database name.
For additional information about statements that are not permitted in stored routines, see *note stored-program-restrictions::.
For information about invoking stored procedures from within programs written in a language that has a MySQL interface, see *note call::.
MySQL stores the 'sql_mode' system variable setting in effect when a routine is created or altered, and always executes the routine with this setting in force, regardless of the current server SQL mode when the routine begins executing.
The switch from the SQL mode of the invoker to that of the routine occurs after evaluation of arguments and assignment of the resulting values to routine parameters. If you define a routine in strict SQL mode but invoke it in nonstrict mode, assignment of arguments to routine parameters does not take place in strict mode. If you require that expressions passed to a routine be assigned in strict SQL mode, you should invoke the routine with strict mode in effect.
The 'COMMENT' characteristic is a MySQL extension, and may be used to describe the stored routine. This information is displayed by the note 'SHOW CREATE PROCEDURE': show-create-procedure. and note 'SHOW CREATE FUNCTION': show-create-function. statements.
The 'LANGUAGE' characteristic indicates the language in which the routine is written. The server ignores this characteristic; only SQL routines are supported.
A routine is considered 'deterministic' if it always produces the same result for the same input parameters, and 'not deterministic' otherwise. If neither 'DETERMINISTIC' nor 'NOT DETERMINISTIC' is given in the routine definition, the default is 'NOT DETERMINISTIC'. To declare that a function is deterministic, you must specify 'DETERMINISTIC' explicitly.
Assessment of the nature of a routine is based on the 'honesty' of the creator: MySQL does not check that a routine declared 'DETERMINISTIC' is free of statements that produce nondeterministic results. However, misdeclaring a routine might affect results or affect performance. Declaring a nondeterministic routine as 'DETERMINISTIC' might lead to unexpected results by causing the optimizer to make incorrect execution plan choices. Declaring a deterministic routine as 'NONDETERMINISTIC' might diminish performance by causing available optimizations not to be used.
If binary logging is enabled, the 'DETERMINISTIC' characteristic affects which routine definitions MySQL accepts. See *note stored-programs-logging::.
A routine that contains the 'NOW()' function (or its synonyms) or 'RAND()' is nondeterministic, but it might still be replication-safe. For 'NOW()', the binary log includes the timestamp and replicates correctly. 'RAND()' also replicates correctly as long as it is called only a single time during the execution of a routine. (You can consider the routine execution timestamp and random number seed as implicit inputs that are identical on the source and replica.)
Several characteristics provide information about the nature of data use by the routine. In MySQL, these characteristics are advisory only. The server does not use them to constrain what kinds of statements a routine is permitted to execute.
'CONTAINS SQL' indicates that the routine does not contain statements that read or write data. This is the default if none of these characteristics is given explicitly. Examples of such statements are 'SET @x = 1' or 'DO RELEASE_LOCK('abc')', which execute but neither read nor write data.
'NO SQL' indicates that the routine contains no SQL statements.
'READS SQL DATA' indicates that the routine contains statements that read data (for example, *note 'SELECT': select.), but not statements that write data.
'MODIFIES SQL DATA' indicates that the routine contains statements that may write data (for example, note 'INSERT': insert. or note 'DELETE': delete.).
The 'SQL SECURITY' characteristic can be 'DEFINER' or 'INVOKER' to specify the security context; that is, whether the routine executes using the privileges of the account named in the routine 'DEFINER' clause or the user who invokes it. This account must have permission to access the database with which the routine is associated. The default value is 'DEFINER'. The user who invokes the routine must have the 'EXECUTE' privilege for it, as must the 'DEFINER' account if the routine executes in definer security context.
The 'DEFINER' clause specifies the MySQL account to be used when checking access privileges at routine execution time for routines that have the 'SQL SECURITY DEFINER' characteristic.
If the 'DEFINER' clause is present, the USER value should be a MySQL account specified as ''USER_NAME'@'HOST_NAME'', 'CURRENT_USER', or 'CURRENT_USER()'. The permitted USER values depend on the privileges you hold, as discussed in *note stored-objects-security::. Also see that section for additional information about stored routine security.
If the 'DEFINER' clause is omitted, the default definer is the user who executes the note 'CREATE PROCEDURE': create-procedure. or note 'CREATE FUNCTION': create-function. statement. This is the same as specifying 'DEFINER = CURRENT_USER' explicitly.
Within the body of a stored routine that is defined with the 'SQL SECURITY DEFINER' characteristic, the 'CURRENT_USER' function returns the routine's 'DEFINER' value. For information about user auditing within stored routines, see *note account-activity-auditing::.
Consider the following procedure, which displays a count of the number of MySQL accounts listed in the 'mysql.user' system table:
CREATE DEFINER = 'admin'@'localhost' PROCEDURE account_count()
BEGIN
SELECT 'Number of accounts:', COUNT(*) FROM mysql.user;
END;
The procedure is assigned a 'DEFINER' account of ''admin'@'localhost'' no matter which user defines it. It executes with the privileges of that account no matter which user invokes it (because the default security characteristic is 'DEFINER'). The procedure succeeds or fails depending on whether invoker has the 'EXECUTE' privilege for it and ''admin'@'localhost'' has the 'SELECT' privilege for the 'mysql.user' table.
Now suppose that the procedure is defined with the 'SQL SECURITY INVOKER' characteristic:
CREATE DEFINER = 'admin'@'localhost' PROCEDURE account_count()
SQL SECURITY INVOKER
BEGIN
SELECT 'Number of accounts:', COUNT(*) FROM mysql.user;
END;
The procedure still has a 'DEFINER' of ''admin'@'localhost'', but in this case, it executes with the privileges of the invoking user. Thus, the procedure succeeds or fails depending on whether the invoker has the 'EXECUTE' privilege for it and the 'SELECT' privilege for the 'mysql.user' table.
The server handles the data type of a routine parameter, local routine variable created with *note 'DECLARE': declare, or function return value as follows:
Assignments are checked for data type mismatches and overflow. Conversion and overflow problems result in warnings, or errors in strict SQL mode.
Only scalar values can be assigned. For example, a statement such as 'SET x = (SELECT 1, 2)' is invalid.
For character data types, if 'CHARACTER SET' is includedd in the declaration, the specified character set and its default collation is used. If the 'COLLATE' attribute is also present, that collation is used rather than the default collation.
If 'CHARACTER SET' and 'COLLATE' are not present, the database character set and collation in effect at routine creation time are used. To avoid having the server use the database character set and collation, provide an explicit 'CHARACTER SET' and a 'COLLATE' attribute for character data parameters.
If you alter the database default character set or collation, stored routines that are to use the new database defaults must be dropped and recreated.
The database character set and collation are given by the value of the 'character_set_database' and 'collation_database' system variables. For more information, see *note charset-database::.
File: manual.info.tmp, Node: create-server, Next: create-table, Prev: create-procedure, Up: sql-data-definition-statements
CREATE SERVER SERVER_NAME
FOREIGN DATA WRAPPER WRAPPER_NAME
OPTIONS (OPTION [, OPTION] ...)
OPTION: {
HOST CHARACTER-LITERAL
| DATABASE CHARACTER-LITERAL
| USER CHARACTER-LITERAL
| PASSWORD CHARACTER-LITERAL
| SOCKET CHARACTER-LITERAL
| OWNER CHARACTER-LITERAL
| PORT NUMERIC-LITERAL
}
This statement creates the definition of a server for use with the 'FEDERATED' storage engine. The 'CREATE SERVER' statement creates a new row in the 'servers' table in the 'mysql' database. This statement requires the 'SUPER' privilege.
The 'SERVER_NAME' should be a unique reference to the server. Server definitions are global within the scope of the server, it is not possible to qualify the server definition to a specific database. 'SERVER_NAME' has a maximum length of 64 characters (names longer than 64 characters are silently truncated), and is case-insensitive. You may specify the name as a quoted string.
The 'WRAPPER_NAME' is an identifier and may be quoted with single quotation marks.
For each 'OPTION' you must specify either a character literal or numeric literal. Character literals are UTF-8, support a maximum length of 64 characters and default to a blank (empty) string. String literals are silently truncated to 64 characters. Numeric literals must be a number between 0 and 9999, default value is 0.
Note:
The 'OWNER' option is currently not applied, and has no effect on the ownership or operation of the server connection that is created.
The 'CREATE SERVER' statement creates an entry in the 'mysql.servers' table that can later be used with the *note 'CREATE TABLE': create-table. statement when creating a 'FEDERATED' table. The options that you specify are used to populate the columns in the 'mysql.servers' table. The table columns are 'Server_name', 'Host', 'Db', 'Username', 'Password', 'Port' and 'Socket'.
For example:
CREATE SERVER s
FOREIGN DATA WRAPPER mysql
OPTIONS (USER 'Remote', HOST '198.51.100.106', DATABASE 'test');
Be sure to specify all options necessary to establish a connection to the server. The user name, host name, and database name are mandatory. Other options might be required as well, such as password.
The data stored in the table can be used when creating a connection to a 'FEDERATED' table:
CREATE TABLE t (s1 INT) ENGINE=FEDERATED CONNECTION='s';
For more information, see *note federated-storage-engine::.
'CREATE SERVER' causes an implicit commit. See *note implicit-commit::.
'CREATE SERVER' is not written to the binary log, regardless of the logging format that is in use.
File: manual.info.tmp, Node: create-table, Next: create-tablespace, Prev: create-server, Up: sql-data-definition-statements
Menu:
create-table-ndb-comment-options:: Setting NDB Comment Options
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] TBL_NAME (CREATE_DEFINITION,...) [TABLE_OPTIONS][PARTITION_OPTIONS]
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] TBL_NAME [(CREATE_DEFINITION,...)][TABLE_OPTIONS] [PARTITION_OPTIONS][IGNORE | REPLACE] [AS] QUERY_EXPRESSION
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] TBL_NAME { LIKE OLD_TBL_NAME | (LIKE OLD_TBL_NAME) }
CREATE_DEFINITION: { COL_NAME COLUMN_DEFINITION | {INDEX | KEY} [INDEX_NAME] [INDEX_TYPE] (KEY_PART,...) [INDEX_OPTION] ... | {FULLTEXT | SPATIAL} [INDEX | KEY] [INDEX_NAME] (KEY_PART,...) [INDEX_OPTION] ... | [CONSTRAINT [SYMBOL]] PRIMARY KEY [INDEX_TYPE] (KEY_PART,...) [INDEX_OPTION] ... | [CONSTRAINT [SYMBOL]] UNIQUE [INDEX | KEY][INDEX_NAME] [INDEX_TYPE] (KEY_PART,...) [INDEX_OPTION] ... | [CONSTRAINT [SYMBOL]] FOREIGN KEY [INDEX_NAME] (COL_NAME,...) REFERENCE_DEFINITION | CHECK (EXPR) }
COLUMN_DEFINITION: { DATA_TYPE [NOT NULL | NULL] [DEFAULT DEFAULT_VALUE] [AUTO_INCREMENT] [UNIQUE [KEY]] [[PRIMARY] KEY][COMMENT 'STRING'] [COLLATE COLLATION_NAME][COLUMN_FORMAT {FIXED | DYNAMIC | DEFAULT}] [STORAGE {DISK | MEMORY}][REFERENCE_DEFINITION] | DATA_TYPE [COLLATE COLLATION_NAME][GENERATED ALWAYS] AS (EXPR) [VIRTUAL | STORED] [NOT NULL | NULL] [UNIQUE [KEY]] [[PRIMARY] KEY] [COMMENT 'STRING'][REFERENCE_DEFINITION] }
DATA_TYPE: (see *note data-types::)
KEY_PART: COL_NAME [(LENGTH)] [ASC | DESC]
INDEX_TYPE: USING {BTREE | HASH}
INDEX_OPTION: { KEY_BLOCK_SIZE [=] VALUE | INDEX_TYPE | WITH PARSER PARSER_NAME | COMMENT 'STRING' }
REFERENCE_DEFINITION: REFERENCES TBL_NAME (KEY_PART,...) [MATCH FULL | MATCH PARTIAL | MATCH SIMPLE][ON DELETE REFERENCE_OPTION] [ON UPDATE REFERENCE_OPTION]
REFERENCE_OPTION: RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT
TABLE_OPTIONS: TABLE_OPTION [[,] TABLE_OPTION] ...
TABLE_OPTION: { AUTO_INCREMENT [=] VALUE | AVG_ROW_LENGTH [=] VALUE | [DEFAULT] CHARACTER SET [=] CHARSET_NAME | CHECKSUM [=] {0 | 1} | [DEFAULT] COLLATE [=] COLLATION_NAME | COMMENT [=] 'STRING' | COMPRESSION [=] {'ZLIB' | 'LZ4' | 'NONE'} | CONNECTION [=] 'CONNECT_STRING' | {DATA | INDEX} DIRECTORY [=] 'ABSOLUTE PATH TO DIRECTORY' | DELAY_KEY_WRITE [=] {0 | 1} | ENCRYPTION [=] {'Y' | 'N'} | ENGINE [=] ENGINE_NAME | INSERT_METHOD [=] { NO | FIRST | LAST } | KEY_BLOCK_SIZE [=] VALUE | MAX_ROWS [=] VALUE | MIN_ROWS [=] VALUE | PACK_KEYS [=] {0 | 1 | DEFAULT} | PASSWORD [=] 'STRING' | ROW_FORMAT [=] {DEFAULT | DYNAMIC | FIXED | COMPRESSED | REDUNDANT | COMPACT} | STATS_AUTO_RECALC [=] {DEFAULT | 0 | 1} | STATS_PERSISTENT [=] {DEFAULT | 0 | 1} | STATS_SAMPLE_PAGES [=] VALUE | TABLESPACE_OPTION | UNION [=] (TBL_NAME[,TBL_NAME]...) }
PARTITION_OPTIONS: PARTITION BY { [LINEAR] HASH(EXPR) | [LINEAR] KEY [ALGORITHM={1 | 2}] (COLUMN_LIST) | RANGE{(EXPR) | COLUMNS(COLUMN_LIST)} | LIST{(EXPR) | COLUMNS(COLUMN_LIST)} } [PARTITIONS NUM][SUBPARTITION BY { [LINEAR] HASH(EXPR) | [LINEAR] KEY [ALGORITHM={1 | 2}] (COLUMN_LIST) } [SUBPARTITIONS NUM] ] [(PARTITION_DEFINITION [, PARTITION_DEFINITION] ...)]
PARTITION_DEFINITION: PARTITION PARTITION_NAME [VALUES {LESS THAN {(EXPR | VALUE_LIST) | MAXVALUE} | IN (VALUE_LIST)}][[STORAGE] ENGINE [=] ENGINE_NAME] [COMMENT [=] 'STRING' ][DATA DIRECTORY [=] 'DATA_DIR'] [INDEX DIRECTORY [=] 'INDEX_DIR'][MAX_ROWS [=] MAX_NUMBER_OF_ROWS] [MIN_ROWS [=] MIN_NUMBER_OF_ROWS][TABLESPACE [=] tablespace_name] [(SUBPARTITION_DEFINITION [, SUBPARTITION_DEFINITION] ...)]
SUBPARTITION_DEFINITION: SUBPARTITION LOGICAL_NAME [[STORAGE] ENGINE [=] ENGINE_NAME][COMMENT [=] 'STRING' ] [DATA DIRECTORY [=] 'DATA_DIR'][INDEX DIRECTORY [=] 'INDEX_DIR'] [MAX_ROWS [=] MAX_NUMBER_OF_ROWS][MIN_ROWS [=] MIN_NUMBER_OF_ROWS] [TABLESPACE [=] tablespace_name]
TABLESPACE_OPTION: TABLESPACE TABLESPACE_NAME [STORAGE DISK] | [TABLESPACE TABLESPACE_NAME] STORAGE MEMORY
QUERY_EXPRESSION: SELECT ... (SOME VALID SELECT OR UNION STATEMENT)
*note 'CREATE TABLE': create-table. creates a table with the given name. You must have the 'CREATE' privilege for the table.
By default, tables are created in the default database, using the *note 'InnoDB': innodb-storage-engine. storage engine. An error occurs if the table exists, if there is no default database, or if the database does not exist.
MySQL has no limit on the number of tables. The underlying file system may have a limit on the number of files that represent tables. Individual storage engines may impose engine-specific constraints. 'InnoDB' permits up to 4 billion tables.
For information about the physical representation of a table, see *note create-table-files::.
There are several aspects to the *note 'CREATE TABLE': create-table. statement, described under the following topics in this section:
*note create-table-name::
*note create-table-temporary-tables::
*note create-table-clone-copy::
*note create-table-types-attributes::
*note create-table-indexes-keys::
*note create-table-options::
*note create-table-partitioning::
Table Name
'TBL_NAME'
The table name can be specified as DB_NAME.TBL_NAME to create the table in a specific database. This works regardless of whether there is a default database, assuming that the database exists. If you use quoted identifiers, quote the database and table names separately. For example, write 'mydb
.mytbl
', not 'mydb.mytbl
'.
Rules for permissible table names are given in *note identifiers::.
'IF NOT EXISTS'
Prevents an error from occurring if the table exists. However, there is no verification that the existing table has a structure identical to that indicated by the *note 'CREATE TABLE': create-table. statement.
Temporary Tables
You can use the 'TEMPORARY' keyword when creating a table. A 'TEMPORARY' table is visible only within the current session, and is dropped automatically when the session is closed. For more information, see *note create-temporary-table::.
Table Cloning and Copying
'LIKE'
Use 'CREATE TABLE ... LIKE' to create an empty table based on the definition of another table, including any column attributes and indexes defined in the original table:
CREATE TABLE NEW_TBL LIKE ORIG_TBL;
For more information, see *note create-table-like::.
'[AS] QUERY_EXPRESSION'
To create one table from another, add a note 'SELECT': select. statement at the end of the note 'CREATE TABLE': create-table. statement:
CREATE TABLE NEW_TBL AS SELECT * FROM ORIG_TBL;
For more information, see *note create-table-select::.
'IGNORE | REPLACE'
The 'IGNORE' and 'REPLACE' options indicate how to handle rows that duplicate unique key values when copying a table using a *note 'SELECT': select. statement.
For more information, see *note create-table-select::.
Column Data Types and Attributes
There is a hard limit of 4096 columns per table, but the effective maximum may be less for a given table and depends on the factors discussed in *note column-count-limit::.
'DATA_TYPE'
DATA_TYPE represents the data type in a column definition. For a full description of the syntax available for specifying column data types, as well as information about the properties of each type, see *note data-types::.
* Some attributes do not apply to all data types.
'AUTO_INCREMENT' applies only to integer and floating-point
types. 'DEFAULT' does not apply to the *note 'BLOB': blob,
*note 'TEXT': blob, 'GEOMETRY', and *note 'JSON': json. types.
* Character data types (*note 'CHAR': char, *note 'VARCHAR':
char, the *note 'TEXT': blob. types, *note 'ENUM': enum, *note
'SET': set, and any synonyms) can include 'CHARACTER SET' to
specify the character set for the column. 'CHARSET' is a
synonym for 'CHARACTER SET'. A collation for the character
set can be specified with the 'COLLATE' attribute, along with
any other attributes. For details, see *note charset::.
Example:
CREATE TABLE t (c CHAR(20) CHARACTER SET utf8 COLLATE utf8_bin);
MySQL 5.7 interprets length specifications in character column
definitions in characters. Lengths for *note 'BINARY':
binary-varbinary. and *note 'VARBINARY': binary-varbinary. are
in bytes.
* For *note 'CHAR': char, *note 'VARCHAR': char, *note 'BINARY':
binary-varbinary, and *note 'VARBINARY': binary-varbinary.
columns, indexes can be created that use only the leading part
of column values, using 'COL_NAME(LENGTH)' syntax to specify
an index prefix length. *note 'BLOB': blob. and *note 'TEXT':
blob. columns also can be indexed, but a prefix length _must_
be given. Prefix lengths are given in characters for
nonbinary string types and in bytes for binary string types.
That is, index entries consist of the first LENGTH characters
of each column value for *note 'CHAR': char, *note 'VARCHAR':
char, and *note 'TEXT': blob. columns, and the first LENGTH
bytes of each column value for *note 'BINARY':
binary-varbinary, *note 'VARBINARY': binary-varbinary, and
*note 'BLOB': blob. columns. Indexing only a prefix of column
values like this can make the index file much smaller. For
additional information about index prefixes, see *note
create-index::.
Only the 'InnoDB' and 'MyISAM' storage engines support
indexing on *note 'BLOB': blob. and *note 'TEXT': blob.
columns. For example:
CREATE TABLE test (blob_col BLOB, INDEX(blob_col(10)));
As of MySQL 5.7.17, if a specified index prefix exceeds the
maximum column data type size, *note 'CREATE TABLE':
create-table. handles the index as follows:
* For a nonunique index, either an error occurs (if strict
SQL mode is enabled), or the index length is reduced to
lie within the maximum column data type size and a
warning is produced (if strict SQL mode is not enabled).
* For a unique index, an error occurs regardless of SQL
mode because reducing the index length might enable
insertion of nonunique entries that do not meet the
specified uniqueness requirement.
* *note 'JSON': json. columns cannot be indexed. You can work
around this restriction by creating an index on a generated
column that extracts a scalar value from the 'JSON' column.
See *note json-column-indirect-index::, for a detailed
example.
'NOT NULL | NULL'
If neither 'NULL' nor 'NOT NULL' is specified, the column is treated as though 'NULL' had been specified.
In MySQL 5.7, only the 'InnoDB', 'MyISAM', and 'MEMORY' storage engines support indexes on columns that can have 'NULL' values. In other cases, you must declare indexed columns as 'NOT NULL' or an error results.
'DEFAULT'
Specifies a default value for a column. For more information about default value handling, including the case that a column definition includes no explicit 'DEFAULT' value, see *note data-type-defaults::.
If the 'NO_ZERO_DATE' or 'NO_ZERO_IN_DATE' SQL mode is enabled and a date-valued default is not correct according to that mode, *note 'CREATE TABLE': create-table. produces a warning if strict SQL mode is not enabled and an error if strict mode is enabled. For example, with 'NO_ZERO_IN_DATE' enabled, 'c1 DATE DEFAULT '2010-00-00'' produces a warning.
'AUTO_INCREMENT'
An integer or floating-point column can have the additional attribute 'AUTO_INCREMENT'. When you insert a value of 'NULL' (recommended) or '0' into an indexed 'AUTO_INCREMENT' column, the column is set to the next sequence value. Typically this is 'VALUE+1', where VALUE is the largest value for the column currently in the table. 'AUTO_INCREMENT' sequences begin with '1'.
To retrieve an 'AUTO_INCREMENT' value after inserting a row, use the 'LAST_INSERT_ID()' SQL function or the 'mysql_insert_id()' (https://dev.mysql.com/doc/c-api/5.7/en/mysql-insert-id.html) C API function. See *note information-functions::, and mysql_insert_id() (https://dev.mysql.com/doc/c-api/5.7/en/mysql-insert-id.html).
If the 'NO_AUTO_VALUE_ON_ZERO' SQL mode is enabled, you can store '0' in 'AUTO_INCREMENT' columns as '0' without generating a new sequence value. See *note sql-mode::.
There can be only one 'AUTO_INCREMENT' column per table, it must be indexed, and it cannot have a 'DEFAULT' value. An 'AUTO_INCREMENT' column works properly only if it contains only positive values. Inserting a negative number is regarded as inserting a very large positive number. This is done to avoid precision problems when numbers 'wrap' over from positive to negative and also to ensure that you do not accidentally get an 'AUTO_INCREMENT' column that contains '0'.
For 'MyISAM' tables, you can specify an 'AUTO_INCREMENT' secondary column in a multiple-column key. See *note example-auto-increment::.
To make MySQL compatible with some ODBC applications, you can find the 'AUTO_INCREMENT' value for the last inserted row with the following query:
SELECT * FROM TBL_NAME WHERE AUTO_COL IS NULL
This method requires that 'sql_auto_is_null' variable is not set to
For information about 'InnoDB' and 'AUTO_INCREMENT', see note innodb-auto-increment-handling::. For information about 'AUTO_INCREMENT' and MySQL Replication, see note replication-features-auto-increment::.
'COMMENT'
A comment for a column can be specified with the 'COMMENT' option, up to 1024 characters long. The comment is displayed by the note 'SHOW CREATE TABLE': show-create-table. and note 'SHOW FULL COLUMNS': show-columns. statements. It is also shown in the 'COLUMN_COMMENT' column of the Information Schema *note 'COLUMNS': information-schema-columns-table. table.
'COLUMN_FORMAT'
In NDB Cluster, it is also possible to specify a data storage format for individual columns of *note 'NDB': mysql-cluster. tables using 'COLUMN_FORMAT'. Permissible column formats are 'FIXED', 'DYNAMIC', and 'DEFAULT'. 'FIXED' is used to specify fixed-width storage, 'DYNAMIC' permits the column to be variable-width, and 'DEFAULT' causes the column to use fixed-width or variable-width storage as determined by the column's data type (possibly overridden by a 'ROW_FORMAT' specifier).
Beginning with MySQL NDB Cluster 7.5.4, for *note 'NDB': mysql-cluster. tables, the default value for 'COLUMN_FORMAT' is 'FIXED'. (The default had been switched to 'DYNAMIC' in MySQL NDB Cluster 7.5.1, but this change was reverted to maintain backwards compatibility with existing GA release series.) (Bug #24487363)
In NDB Cluster, the maximum possible offset for a column defined with 'COLUMN_FORMAT=FIXED' is 8188 bytes. For more information and possible workarounds, see *note mysql-cluster-limitations-database-objects::.
'COLUMN_FORMAT' currently has no effect on columns of tables using storage engines other than *note 'NDB': mysql-cluster. In MySQL 5.7 and later, 'COLUMN_FORMAT' is silently ignored.
'STORAGE'
For note 'NDB': mysql-cluster. tables, it is possible to specify whether the column is stored on disk or in memory by using a 'STORAGE' clause. 'STORAGE DISK' causes the column to be stored on disk, and 'STORAGE MEMORY' causes in-memory storage to be used. The note 'CREATE TABLE': create-table. statement used must still include a 'TABLESPACE' clause:
mysql> CREATE TABLE t1 (
-> c1 INT STORAGE DISK,
-> c2 INT STORAGE MEMORY
-> ) ENGINE NDB;
ERROR 1005 (HY000): Can't create table 'c.t1' (errno: 140)
mysql> CREATE TABLE t1 (
-> c1 INT STORAGE DISK,
-> c2 INT STORAGE MEMORY
-> ) TABLESPACE ts_1 ENGINE NDB;
Query OK, 0 rows affected (1.06 sec)
For *note 'NDB': mysql-cluster. tables, 'STORAGE DEFAULT' is equivalent to 'STORAGE MEMORY'.
The 'STORAGE' clause has no effect on tables using storage engines other than note 'NDB': mysql-cluster. The 'STORAGE' keyword is supported only in the build of note 'mysqld': mysqld. that is supplied with NDB Cluster; it is not recognized in any other version of MySQL, where any attempt to use the 'STORAGE' keyword causes a syntax error.
'GENERATED ALWAYS'
Used to specify a generated column expression. For information about generated columns, see *note create-table-generated-columns::.
Stored generated columns can be indexed. 'InnoDB' supports secondary indexes on virtual generated columns. See *note create-table-secondary-indexes::.
Indexes and Foreign Keys
Several keywords apply to creation of indexes and foreign keys. For general background in addition to the following descriptions, see note create-index::, and note create-table-foreign-keys::.
'CONSTRAINT SYMBOL'
The 'CONSTRAINT SYMBOL' clause may be given to name a constraint. If the clause is not given, or a SYMBOL is not included following the 'CONSTRAINT' keyword, MySQL automatically generates a constraint name, with the exception noted below. The SYMBOL value, if used, must be unique per schema (database), per constraint type. A duplicate SYMBOL results in an error. See also the discussion about length limits of generated constraint identifiers at *note identifier-length::.
Note:
If the 'CONSTRAINT SYMBOL' clause is not given in a foreign key definition, or a SYMBOL is not included following the 'CONSTRAINT' keyword, *note 'NDB': mysql-cluster. uses the foreign key index name.
The SQL standard specifies that all types of constraints (primary key, unique index, foreign key, check) belong to the same namespace. In MySQL, each constraint type has its own namespace per schema. Consequently, names for each type of constraint must be unique per schema.
'PRIMARY KEY'
A unique index where all key columns must be defined as 'NOT NULL'. If they are not explicitly declared as 'NOT NULL', MySQL declares them so implicitly (and silently). A table can have only one 'PRIMARY KEY'. The name of a 'PRIMARY KEY' is always 'PRIMARY', which thus cannot be used as the name for any other kind of index.
If you do not have a 'PRIMARY KEY' and an application asks for the 'PRIMARY KEY' in your tables, MySQL returns the first 'UNIQUE' index that has no 'NULL' columns as the 'PRIMARY KEY'.
In 'InnoDB' tables, keep the 'PRIMARY KEY' short to minimize storage overhead for secondary indexes. Each secondary index entry contains a copy of the primary key columns for the corresponding row. (See *note innodb-index-types::.)
In the created table, a 'PRIMARY KEY' is placed first, followed by all 'UNIQUE' indexes, and then the nonunique indexes. This helps the MySQL optimizer to prioritize which index to use and also more quickly to detect duplicated 'UNIQUE' keys.
A 'PRIMARY KEY' can be a multiple-column index. However, you cannot create a multiple-column index using the 'PRIMARY KEY' key attribute in a column specification. Doing so only marks that single column as primary. You must use a separate 'PRIMARY KEY(KEY_PART, ...)' clause.
If a table has a 'PRIMARY KEY' or 'UNIQUE NOT NULL' index that consists of a single column that has an integer type, you can use '_rowid' to refer to the indexed column in note 'SELECT': select. statements, as described in note create-index-unique::.
In MySQL, the name of a 'PRIMARY KEY' is 'PRIMARY'. For other indexes, if you do not assign a name, the index is assigned the same name as the first indexed column, with an optional suffix ('_2', '_3', '...') to make it unique. You can see index names for a table using 'SHOW INDEX FROM TBL_NAME'. See *note show-index::.
'KEY | INDEX'
'KEY' is normally a synonym for 'INDEX'. The key attribute 'PRIMARY KEY' can also be specified as just 'KEY' when given in a column definition. This was implemented for compatibility with other database systems.
'UNIQUE'
A 'UNIQUE' index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. For all engines, a 'UNIQUE' index permits multiple 'NULL' values for columns that can contain 'NULL'. If you specify a prefix value for a column in a 'UNIQUE' index, the column values must be unique within the prefix length.
If a table has a 'PRIMARY KEY' or 'UNIQUE NOT NULL' index that consists of a single column that has an integer type, you can use '_rowid' to refer to the indexed column in note 'SELECT': select. statements, as described in note create-index-unique::.
'FULLTEXT'
A 'FULLTEXT' index is a special type of index used for full-text searches. Only the note 'InnoDB': innodb-storage-engine. and note 'MyISAM': myisam-storage-engine. storage engines support 'FULLTEXT' indexes. They can be created only from note 'CHAR': char, note 'VARCHAR': char, and note 'TEXT': blob. columns. Indexing always happens over the entire column; column prefix indexing is not supported and any prefix length is ignored if specified. See note fulltext-search::, for details of operation. A 'WITH PARSER' clause can be specified as an INDEX_OPTION value to associate a parser plugin with the index if full-text indexing and searching operations need special handling. This clause is valid only for 'FULLTEXT' indexes. Both note 'InnoDB': innodb-storage-engine. and note 'MyISAM': myisam-storage-engine. support full-text parser plugins. See Full-Text Parser Plugins (https://dev.mysql.com/doc/extending-mysql/5.7/en/plugin-types.html#full-text-plugin-type) and Writing Full-Text Parser Plugins (https://dev.mysql.com/doc/extending-mysql/5.7/en/writing-full-text-plugins.html) for more information.
'SPATIAL'
You can create 'SPATIAL' indexes on spatial data types. Spatial types are supported only for 'MyISAM' and 'InnoDB' tables, and indexed columns must be declared as 'NOT NULL'. See *note spatial-types::.
'FOREIGN KEY'
MySQL supports foreign keys, which let you cross-reference related data across tables, and foreign key constraints, which help keep this spread-out data consistent. For definition and option information, see REFERENCE_DEFINITION, and REFERENCE_OPTION.
Partitioned tables employing the note 'InnoDB': innodb-storage-engine. storage engine do not support foreign keys. See note partitioning-limitations::, for more information.
'CHECK'
The 'CHECK' clause is parsed but ignored by all storage engines.
'KEY_PART'
* A KEY_PART specification can end with 'ASC' or 'DESC'. These
keywords are permitted for future extensions for specifying
ascending or descending index value storage. Currently, they
are parsed but ignored; index values are always stored in
ascending order.
* Prefixes, defined by the LENGTH attribute, can be up to 767
bytes long for 'InnoDB' tables or 3072 bytes if the
'innodb_large_prefix' option is enabled. For 'MyISAM' tables,
the prefix length limit is 1000 bytes.
Prefix _limits_ are measured in bytes. However, prefix
_lengths_ for index specifications in *note 'CREATE TABLE':
create-table, *note 'ALTER TABLE': alter-table, and *note
'CREATE INDEX': create-index. statements are interpreted as
number of characters for nonbinary string types (*note 'CHAR':
char, *note 'VARCHAR': char, *note 'TEXT': blob.) and number
of bytes for binary string types (*note 'BINARY':
binary-varbinary, *note 'VARBINARY': binary-varbinary, *note
'BLOB': blob.). Take this into account when specifying a
prefix length for a nonbinary string column that uses a
multibyte character set.
'INDEX_TYPE'
Some storage engines permit you to specify an index type when creating an index. The syntax for the INDEX_TYPE specifier is 'USING TYPE_NAME'.
Example:
CREATE TABLE lookup
(id INT, INDEX USING BTREE (id))
ENGINE = MEMORY;
The preferred position for 'USING' is after the index column list. It can be given before the column list, but support for use of the option in that position is deprecated; expect it to be removed in a future MySQL release.
'INDEX_OPTION'
INDEX_OPTION values specify additional options for an index.
* 'KEY_BLOCK_SIZE'
For *note 'MyISAM': myisam-storage-engine. tables,
'KEY_BLOCK_SIZE' optionally specifies the size in bytes to use
for index key blocks. The value is treated as a hint; a
different size could be used if necessary. A 'KEY_BLOCK_SIZE'
value specified for an individual index definition overrides
the table-level 'KEY_BLOCK_SIZE' value.
For information about the table-level 'KEY_BLOCK_SIZE'
attribute, see *note create-table-options::.
* 'WITH PARSER'
The 'WITH PARSER' option can be used only with 'FULLTEXT'
indexes. It associates a parser plugin with the index if
full-text indexing and searching operations need special
handling. Both *note 'InnoDB': innodb-storage-engine. and
*note 'MyISAM': myisam-storage-engine. support full-text
parser plugins. If you have a *note 'MyISAM':
myisam-storage-engine. table with an associated full-text
parser plugin, you can convert the table to 'InnoDB' using
'ALTER TABLE'.
* 'COMMENT'
Index definitions can include an optional comment of up to
1024 characters.
You can set the 'InnoDB' 'MERGE_THRESHOLD' value for an
individual index using the 'INDEX_OPTION' 'COMMENT' clause.
See *note index-page-merge-threshold::.
For more information about permissible INDEX_OPTION values, see note create-index::. For more information about indexes, see note mysql-indexes::.
'REFERENCE_DEFINITION'
For REFERENCE_DEFINITION syntax details and examples, see *note create-table-foreign-keys::.
note 'InnoDB': innodb-storage-engine. and note 'NDB': mysql-cluster. tables support checking of foreign key constraints. The columns of the referenced table must always be explicitly named. Both 'ON DELETE' and 'ON UPDATE' actions on foreign keys are supported. For more detailed information and examples, see *note create-table-foreign-keys::.
For other storage engines, MySQL Server parses and ignores the 'FOREIGN KEY' syntax in *note 'CREATE TABLE': create-table. statements.
Important:
For users familiar with the ANSI/ISO SQL Standard, please note that no storage engine, including 'InnoDB', recognizes or enforces the 'MATCH' clause used in referential integrity constraint definitions. Use of an explicit 'MATCH' clause does not have the specified effect, and also causes 'ON DELETE' and 'ON UPDATE' clauses to be ignored. For these reasons, specifying 'MATCH' should be avoided.
The 'MATCH' clause in the SQL standard controls how 'NULL' values in a composite (multiple-column) foreign key are handled when comparing to a primary key. 'InnoDB' essentially implements the semantics defined by 'MATCH SIMPLE', which permit a foreign key to be all or partially 'NULL'. In that case, the (child table) row containing such a foreign key is permitted to be inserted, and does not match any row in the referenced (parent) table. It is possible to implement other semantics using triggers.
Additionally, MySQL requires that the referenced columns be indexed for performance. However, 'InnoDB' does not enforce any requirement that the referenced columns be declared 'UNIQUE' or 'NOT NULL'. The handling of foreign key references to nonunique keys or keys that contain 'NULL' values is not well defined for operations such as 'UPDATE' or 'DELETE CASCADE'. You are advised to use foreign keys that reference only keys that are both 'UNIQUE' (or 'PRIMARY') and 'NOT NULL'.
MySQL parses but ignores 'inline 'REFERENCES' specifications' (as defined in the SQL standard) where the references are defined as part of the column specification. MySQL accepts 'REFERENCES' clauses only when specified as part of a separate 'FOREIGN KEY' specification. For more information, see *note ansi-diff-foreign-keys::.
'REFERENCE_OPTION'
For information about the 'RESTRICT', 'CASCADE', 'SET NULL', 'NO ACTION', and 'SET DEFAULT' options, see *note create-table-foreign-keys::.
Table Options
Table options are used to optimize the behavior of the table. In most cases, you do not have to specify any of them. These options apply to all storage engines unless otherwise indicated. Options that do not apply to a given storage engine may be accepted and remembered as part of the table definition. Such options then apply if you later use *note 'ALTER TABLE': alter-table. to convert the table to use a different storage engine.
'ENGINE'
Specifies the storage engine for the table, using one of the names shown in the following table. The engine name can be unquoted or quoted. The quoted name ''DEFAULT'' is recognized but ignored.
Storage Engine Description
'InnoDB' Transaction-safe tables with row locking and foreign keys. The default storage engine for new tables. See note innodb-storage-engine::, and in particular note innodb-introduction:: if you have MySQL experience but are new to 'InnoDB'.
'MyISAM' The binary portable storage engine that is primarily used for read-only or read-mostly workloads. See *note myisam-storage-engine::.
'MEMORY' The data for this storage engine is stored only in memory. See *note memory-storage-engine::.
'CSV' Tables that store rows in comma-separated values format. See *note csv-storage-engine::.
'ARCHIVE' The archiving storage engine. See *note archive-storage-engine::.
'EXAMPLE' An example engine. See *note example-storage-engine::.
'FEDERATED' Storage engine that accesses remote tables. See *note federated-storage-engine::.
'HEAP' This is a synonym for 'MEMORY'.
'MERGE' A collection of 'MyISAM' tables used as one table. Also known as 'MRG_MyISAM'. See *note merge-storage-engine::.
note 'NDB': mysql-cluster.Clustered, fault-tolerant, memory-based tables, supporting transactions and foreign keys. Also known as note 'NDBCLUSTER': mysql-cluster. See *note mysql-cluster::.
By default, if a storage engine is specified that is not available, the statement fails with an error. You can override this behavior by removing 'NO_ENGINE_SUBSTITUTION' from the server SQL mode (see *note sql-mode::) so that MySQL allows substitution of the specified engine with the default storage engine instead. Normally in such cases, this is 'InnoDB', which is the default value for the 'default_storage_engine' system variable. When 'NO_ENGINE_SUBSTITUTION' is disabled, a warning occurs if the storage engine specification is not honored.
'AUTO_INCREMENT'
The initial 'AUTO_INCREMENT' value for the table. In MySQL 5.7, this works for 'MyISAM', 'MEMORY', 'InnoDB', and 'ARCHIVE' tables. To set the first auto-increment value for engines that do not support the 'AUTO_INCREMENT' table option, insert a 'dummy' row with a value one less than the desired value after creating the table, and then delete the dummy row.
For engines that support the 'AUTO_INCREMENT' table option in *note 'CREATE TABLE': create-table. statements, you can also use 'ALTER TABLE TBL_NAME AUTO_INCREMENT = N' to reset the 'AUTO_INCREMENT' value. The value cannot be set lower than the maximum value currently in the column.
'AVG_ROW_LENGTH'
An approximation of the average row length for your table. You need to set this only for large tables with variable-size rows.
When you create a 'MyISAM' table, MySQL uses the product of the 'MAX_ROWS' and 'AVG_ROW_LENGTH' options to decide how big the resulting table is. If you don't specify either option, the maximum size for 'MyISAM' data and index files is 256TB by default. (If your operating system does not support files that large, table sizes are constrained by the file size limit.) If you want to keep down the pointer sizes to make the index smaller and faster and you don't really need big files, you can decrease the default pointer size by setting the 'myisam_data_pointer_size' system variable. (See *note server-system-variables::.) If you want all your tables to be able to grow above the default limit and are willing to have your tables slightly slower and larger than necessary, you can increase the default pointer size by setting this variable. Setting the value to 7 permits table sizes up to 65,536TB.
'[DEFAULT] CHARACTER SET'
Specifies a default character set for the table. 'CHARSET' is a synonym for 'CHARACTER SET'. If the character set name is 'DEFAULT', the database character set is used.
'CHECKSUM'
Set this to 1 if you want MySQL to maintain a live checksum for all rows (that is, a checksum that MySQL updates automatically as the table changes). This makes the table a little slower to update, but also makes it easier to find corrupted tables. The *note 'CHECKSUM TABLE': checksum-table. statement reports the checksum. ('MyISAM' only.)
'[DEFAULT] COLLATE'
Specifies a default collation for the table.
'COMMENT'
A comment for the table, up to 2048 characters long.
You can set the 'InnoDB' 'MERGE_THRESHOLD' value for a table using the 'TABLE_OPTION' 'COMMENT' clause. See *note index-page-merge-threshold::.
Setting NDB_TABLE options
In MySQL NDB Cluster 7.5.2 and later, the table comment in a 'CREATE TABLE' or *note 'ALTER TABLE': alter-table. statement can also be used to specify one to four of the 'NDB_TABLE' options 'NOLOGGING', 'READ_BACKUP', 'PARTITION_BALANCE', or 'FULLY_REPLICATED' as a set of name-value pairs, separated by commas if need be, immediately following the string 'NDB_TABLE=' that begins the quoted comment text. An example statement using this syntax is shown here (emphasized text):
CREATE TABLE t1 (
c1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
c2 VARCHAR(100),
c3 VARCHAR(100) )
ENGINE=NDB
_COMMENT="NDB_TABLE=READ_BACKUP=0,PARTITION_BALANCE=FOR_RP_BY_NODE"_;
Spaces are not permitted within the quoted string. The string is case-insensitive.
The comment is displayed as part of the ouput of note 'SHOW CREATE TABLE': show-create-table. The text of the comment is also available as the TABLE_COMMENT column of the MySQL Information Schema note 'TABLES': information-schema-tables-table. table.
This comment syntax is also supported with *note 'ALTER TABLE': alter-table. statements for 'NDB' tables. Keep in mind that a table comment used with 'ALTER TABLE' replaces any existing comment which the table might have had perviously.
Setting the 'MERGE_THRESHOLD' option in table comments is not supported for *note 'NDB': mysql-cluster. tables (it is ignored).
For complete syntax information and examples, see *note create-table-ndb-comment-options::.
'COMPRESSION'
The compression algorithm used for page level compression for 'InnoDB' tables. Supported values include 'Zlib', 'LZ4', and 'None'. The 'COMPRESSION' attribute was introduced with the transparent page compression feature. Page compression is only supported with 'InnoDB' tables that reside in file-per-table tablespaces, and is only available on Linux and Windows platforms that support sparse files and hole punching. For more information, see *note innodb-page-compression::.
'CONNECTION'
The connection string for a 'FEDERATED' table.
Note:
Older versions of MySQL used a 'COMMENT' option for the connection string.
'DATA DIRECTORY', 'INDEX DIRECTORY'
For 'InnoDB', the 'DATA DIRECTORY='DIRECTORY'' clause permits creating a table outside of the data directory. The 'innodb_file_per_table' variable must be enabled to use the 'DATA DIRECTORY' clause. The full directory path must be specified. For more information, see *note innodb-create-table-external::.
When creating 'MyISAM' tables, you can use the 'DATA DIRECTORY='DIRECTORY'' clause, the 'INDEX DIRECTORY='DIRECTORY'' clause, or both. They specify where to put a 'MyISAM' table's data file and index file, respectively. Unlike 'InnoDB' tables, MySQL does not create subdirectories that correspond to the database name when creating a 'MyISAM' table with a 'DATA DIRECTORY' or 'INDEX DIRECTORY' option. Files are created in the directory that is specified.
As of MySQL 5.7.17, you must have the 'FILE' privilege to use the 'DATA DIRECTORY' or 'INDEX DIRECTORY' table option.
Important:
Table-level 'DATA DIRECTORY' and 'INDEX DIRECTORY' options are ignored for partitioned tables. (Bug #32091)
These options work only when you are not using the '--skip-symbolic-links' option. Your operating system must also have a working, thread-safe 'realpath()' call. See *note symbolic-links-to-tables::, for more complete information.
If a 'MyISAM' table is created with no 'DATA DIRECTORY' option, the '.MYD' file is created in the database directory. By default, if 'MyISAM' finds an existing '.MYD' file in this case, it overwrites it. The same applies to '.MYI' files for tables created with no 'INDEX DIRECTORY' option. To suppress this behavior, start the server with the '--keep_files_on_create' option, in which case 'MyISAM' does not overwrite existing files and returns an error instead.
If a 'MyISAM' table is created with a 'DATA DIRECTORY' or 'INDEX DIRECTORY' option and an existing '.MYD' or '.MYI' file is found, MyISAM always returns an error. It does not overwrite a file in the specified directory.
Important:
You cannot use path names that contain the MySQL data directory with 'DATA DIRECTORY' or 'INDEX DIRECTORY'. This includes partitioned tables and individual table partitions. (See Bug #32167.)
'DELAY_KEY_WRITE'
Set this to 1 if you want to delay key updates for the table until the table is closed. See the description of the 'delay_key_write' system variable in *note server-system-variables::. ('MyISAM' only.)
'ENCRYPTION'
Set the 'ENCRYPTION' option to ''Y'' to enable page-level data encryption for an 'InnoDB' table created in a file-per-table tablespace. Option values are not case-sensitive. The 'ENCRYPTION' option was introduced with the 'InnoDB' tablespace encryption feature; see *note innodb-data-encryption::. A 'keyring' plugin must be installed and configured before encryption can be enabled.
The 'ENCRYPTION' option is supported only by the 'InnoDB' storage engine; thus it works only if the default storage engine is 'InnoDB', or if the 'CREATE TABLE' statement also specifies 'ENGINE=InnoDB'. Otherwise the statement is rejected with 'ER_CHECK_NOT_IMPLEMENTED' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_check_not_implemented).
'INSERT_METHOD'
If you want to insert data into a 'MERGE' table, you must specify with 'INSERT_METHOD' the table into which the row should be inserted. 'INSERT_METHOD' is an option useful for 'MERGE' tables only. Use a value of 'FIRST' or 'LAST' to have inserts go to the first or last table, or a value of 'NO' to prevent inserts. See *note merge-storage-engine::.
'KEY_BLOCK_SIZE'
For *note 'MyISAM': myisam-storage-engine. tables, 'KEY_BLOCK_SIZE' optionally specifies the size in bytes to use for index key blocks. The value is treated as a hint; a different size could be used if necessary. A 'KEY_BLOCK_SIZE' value specified for an individual index definition overrides the table-level 'KEY_BLOCK_SIZE' value.
For note 'InnoDB': innodb-storage-engine. tables, 'KEY_BLOCK_SIZE' specifies the page size in kilobytes to use for compressed 'InnoDB' tables. The 'KEY_BLOCK_SIZE' value is treated as a hint; a different size could be used by 'InnoDB' if necessary. 'KEY_BLOCK_SIZE' can only be less than or equal to the 'innodb_page_size' value. A value of 0 represents the default compressed page size, which is half of the 'innodb_page_size' value. Depending on 'innodb_page_size', possible 'KEY_BLOCK_SIZE' values include 0, 1, 2, 4, 8, and 16. See note innodb-table-compression:: for more information.
Oracle recommends enabling 'innodb_strict_mode' when specifying 'KEY_BLOCK_SIZE' for 'InnoDB' tables. When 'innodb_strict_mode' is enabled, specifying an invalid 'KEY_BLOCK_SIZE' value returns an error. If 'innodb_strict_mode' is disabled, an invalid 'KEY_BLOCK_SIZE' value results in a warning, and the 'KEY_BLOCK_SIZE' option is ignored.
The 'Create_options' column in response to note 'SHOW TABLE STATUS': show-table-status. reports the originally specified 'KEY_BLOCK_SIZE' option, as does note 'SHOW CREATE TABLE': show-create-table.
'InnoDB' only supports 'KEY_BLOCK_SIZE' at the table level.
'KEY_BLOCK_SIZE' is not supported with 32KB and 64KB 'innodb_page_size' values. 'InnoDB' table compression does not support these pages sizes.
'MAX_ROWS'
The maximum number of rows you plan to store in the table. This is not a hard limit, but rather a hint to the storage engine that the table must be able to store at least this many rows.
Important:
The use of 'MAX_ROWS' with 'NDB' tables to control the number of table partitions is deprecated as of NDB Cluster 7.5.4. It remains supported in later versions for backward compatibility, but is subject to removal in a future release. Use PARTITION_BALANCE instead; see *note create-table-comment-ndb-table-options::.
The note 'NDB': mysql-cluster. storage engine treats this value as a maximum. If you plan to create very large NDB Cluster tables (containing millions of rows), you should use this option to insure that note 'NDB': mysql-cluster. allocates sufficient number of index slots in the hash table used for storing hashes of the table's primary keys by setting 'MAX_ROWS = 2 * ROWS', where ROWS is the number of rows that you expect to insert into the table.
The maximum 'MAX_ROWS' value is 4294967295; larger values are truncated to this limit.
'MIN_ROWS'
The minimum number of rows you plan to store in the table. The *note 'MEMORY': memory-storage-engine. storage engine uses this option as a hint about memory use.
'PACK_KEYS'
Takes effect only with 'MyISAM' tables. Set this option to 1 if you want to have smaller indexes. This usually makes updates slower and reads faster. Setting the option to 0 disables all packing of keys. Setting it to 'DEFAULT' tells the storage engine to pack only long note 'CHAR': char, note 'VARCHAR': char, note 'BINARY': binary-varbinary, or note 'VARBINARY': binary-varbinary. columns.
If you do not use 'PACK_KEYS', the default is to pack strings, but not numbers. If you use 'PACK_KEYS=1', numbers are packed as well.
When packing binary number keys, MySQL uses prefix compression:
* Every key needs one extra byte to indicate how many bytes of
the previous key are the same for the next key.
* The pointer to the row is stored in high-byte-first order
directly after the key, to improve compression.
This means that if you have many equal keys on two consecutive rows, all following 'same' keys usually only take two bytes (including the pointer to the row). Compare this to the ordinary case where the following keys takes 'storage_size_for_key + pointer_size' (where the pointer size is usually 4). Conversely, you get a significant benefit from prefix compression only if you have many numbers that are the same. If all keys are totally different, you use one byte more per key, if the key is not a key that can have 'NULL' values. (In this case, the packed key length is stored in the same byte that is used to mark if a key is 'NULL'.)
'PASSWORD'
This option is unused. If you have a need to scramble your '.frm' files and make them unusable to any other MySQL server, please contact our sales department.
'ROW_FORMAT'
Defines the physical format in which the rows are stored.
When creating a table with strict mode disabled, the storage engine's default row format is used if the specified row format is not supported. The actual row format of the table is reported in the 'Row_format' column in response to note 'SHOW TABLE STATUS': show-table-status. The 'Create_options' column shows the row format that was specified in the note 'CREATE TABLE': create-table. statement, as does *note 'SHOW CREATE TABLE': show-create-table.
Row format choices differ depending on the storage engine used for the table.
For 'InnoDB' tables:
* The default row format is defined by
'innodb_default_row_format', which has a default setting of
'DYNAMIC'. The default row format is used when the
'ROW_FORMAT' option is not defined or when
'ROW_FORMAT=DEFAULT' is used.
If the 'ROW_FORMAT' option is not defined, or if
'ROW_FORMAT=DEFAULT' is used, operations that rebuild a table
also silently change the row format of the table to the
default defined by 'innodb_default_row_format'. For more
information, see *note innodb-row-format-defining::.
* For more efficient 'InnoDB' storage of data types, especially
*note 'BLOB': blob. types, use the 'DYNAMIC'. See *note
innodb-row-format-dynamic:: for requirements associated with
the 'DYNAMIC' row format.
* To enable compression for 'InnoDB' tables, specify
'ROW_FORMAT=COMPRESSED'. See *note innodb-compression:: for
requirements associated with the 'COMPRESSED' row format.
* The row format used in older versions of MySQL can still be
requested by specifying the 'REDUNDANT' row format.
* When you specify a non-default 'ROW_FORMAT' clause, consider
also enabling the 'innodb_strict_mode' configuration option.
* 'ROW_FORMAT=FIXED' is not supported. If 'ROW_FORMAT=FIXED' is
specified while 'innodb_strict_mode' is disabled, 'InnoDB'
issues a warning and assumes 'ROW_FORMAT=DYNAMIC'. If
'ROW_FORMAT=FIXED' is specified while 'innodb_strict_mode' is
enabled, which is the default, 'InnoDB' returns an error.
* For additional information about 'InnoDB' row formats, see
*note innodb-row-format::.
For 'MyISAM' tables, the option value can be 'FIXED' or 'DYNAMIC' for static or variable-length row format. note 'myisampack': myisampack. sets the type to 'COMPRESSED'. See note myisam-table-formats::.
For *note 'NDB': mysql-cluster. tables, the default 'ROW_FORMAT' in MySQL NDB Cluster 7.5.1 and later is 'DYNAMIC'. (Previously, it was 'FIXED'.)
'STATS_AUTO_RECALC'
Specifies whether to automatically recalculate persistent statistics for an 'InnoDB' table. The value 'DEFAULT' causes the persistent statistics setting for the table to be determined by the 'innodb_stats_auto_recalc' configuration option. The value '1' causes statistics to be recalculated when 10% of the data in the table has changed. The value '0' prevents automatic recalculation for this table; with this setting, issue an note 'ANALYZE TABLE': analyze-table. statement to recalculate the statistics after making substantial changes to the table. For more information about the persistent statistics feature, see note innodb-persistent-stats::.
'STATS_PERSISTENT'
Specifies whether to enable persistent statistics for an 'InnoDB' table. The value 'DEFAULT' causes the persistent statistics setting for the table to be determined by the 'innodb_stats_persistent' configuration option. The value '1' enables persistent statistics for the table, while the value '0' turns off this feature. After enabling persistent statistics through a 'CREATE TABLE' or 'ALTER TABLE' statement, issue an note 'ANALYZE TABLE': analyze-table. statement to calculate the statistics, after loading representative data into the table. For more information about the persistent statistics feature, see note innodb-persistent-stats::.
'STATS_SAMPLE_PAGES'
The number of index pages to sample when estimating cardinality and other statistics for an indexed column, such as those calculated by note 'ANALYZE TABLE': analyze-table. For more information, see note innodb-persistent-stats::.
'TABLESPACE'
The 'TABLESPACE' clause can be used to create an *note 'InnoDB': innodb-storage-engine. table in an existing general tablespace, a file-per-table tablespace, or the system tablespace.
CREATE TABLE TBL_NAME ... TABLESPACE [=] TABLESPACE_NAME
The general tablespace that you specify must exist prior to using the 'TABLESPACE' clause. For information about general tablespaces, see *note general-tablespaces::.
The 'TABLESPACE_NAME' is a case-sensitive identifier. It may be quoted or unquoted. The forward slash character ('/') is not permitted. Names beginning with 'innodb_' are reserved for special use.
To create a table in the system tablespace, specify 'innodb_system' as the tablespace name.
CREATE TABLE TBL_NAME ... TABLESPACE [=] innodb_system
Using 'TABLESPACE [=] innodb_system', you can place a table of any uncompressed row format in the system tablespace regardless of the 'innodb_file_per_table' setting. For example, you can add a table with 'ROW_FORMAT=DYNAMIC' to the system tablespace using 'TABLESPACE [=] innodb_system'.
To create a table in a file-per-table tablespace, specify 'innodb_file_per_table' as the tablespace name.
CREATE TABLE TBL_NAME ... TABLESPACE [=] innodb_file_per_table
Note:
If 'innodb_file_per_table' is enabled, you need not specify 'TABLESPACE=innodb_file_per_table' to create an 'InnoDB' file-per-table tablespace. 'InnoDB' tables are created in file-per-table tablespaces by default when 'innodb_file_per_table' is enabled.
Note:
Support for creating table partitions in shared 'InnoDB' tablespaces is deprecated in MySQL 5.7.24; expect it to be removed in a future version of MySQL. Shared tablespaces include the 'InnoDB' system tablespace and general tablespaces.
The 'DATA DIRECTORY' clause is permitted with 'CREATE TABLE ... TABLESPACE=innodb_file_per_table' but is otherwise not supported for use in combination with the 'TABLESPACE' option.
Note:
Support for 'TABLESPACE = innodb_file_per_table' and 'TABLESPACE = innodb_temporary' clauses with *note 'CREATE TEMPORARY TABLE': create-table. is deprecated as of MySQL 5.7.24; expect it to be removed in a future version of MySQL.
The 'STORAGE' table option is employed only with *note 'NDB': mysql-cluster. tables. 'STORAGE' determines the type of storage used, and can be either of 'DISK' or 'MEMORY'.
'TABLESPACE ... STORAGE DISK' assigns a table to an NDB Cluster Disk Data tablespace. 'STORAGE DISK' cannot be used in 'CREATE TABLE' unless preceded by 'TABLESPACE' TABLESPACE_NAME.
For 'STORAGE MEMORY', the tablespace name is optional, thus, you can use 'TABLESPACE TABLESPACE_NAME STORAGE MEMORY' or simply 'STORAGE MEMORY' to specify explicitly that the table is in-memory.
See *note mysql-cluster-disk-data::, for more information.
*note 'UNION': union.
Used to access a collection of identical 'MyISAM' tables as one. This works only with 'MERGE' tables. See *note merge-storage-engine::.
You must have 'SELECT', 'UPDATE', and 'DELETE' privileges for the tables you map to a 'MERGE' table.
Note:
Formerly, all tables used had to be in the same database as the 'MERGE' table itself. This restriction no longer applies.
Table Partitioning
PARTITION_OPTIONS can be used to control partitioning of the table created with *note 'CREATE TABLE': create-table.
Not all options shown in the syntax for PARTITION_OPTIONS at the beginning of this section are available for all partitioning types. Please see the listings for the following individual types for information specific to each type, and see *note partitioning::, for more complete information about the workings of and uses for partitioning in MySQL, as well as additional examples of table creation and other statements relating to MySQL partitioning.
Partitions can be modified, merged, added to tables, and dropped from tables. For basic information about the MySQL statements to accomplish these tasks, see note alter-table::. For more detailed descriptions and examples, see note partitioning-management::.
'PARTITION BY'
If used, a PARTITION_OPTIONS clause begins with 'PARTITION BY'. This clause contains the function that is used to determine the partition; the function returns an integer value ranging from 1 to NUM, where NUM is the number of partitions. (The maximum number of user-defined partitions which a table may contain is 1024; the number of subpartitions--discussed later in this section--is included in this maximum.)
Note:
The expression (EXPR) used in a 'PARTITION BY' clause cannot refer to any columns not in the table being created; such references are specifically not permitted and cause the statement to fail with an error. (Bug #29444)
'HASH(EXPR)'
Hashes one or more columns to create a key for placing and locating rows. EXPR is an expression using one or more table columns. This can be any valid MySQL expression (including MySQL functions) that yields a single integer value. For example, these are both valid *note 'CREATE TABLE': create-table. statements using 'PARTITION BY HASH':
CREATE TABLE t1 (col1 INT, col2 CHAR(5))
PARTITION BY HASH(col1);
CREATE TABLE t1 (col1 INT, col2 CHAR(5), col3 DATETIME)
PARTITION BY HASH ( YEAR(col3) );
You may not use either 'VALUES LESS THAN' or 'VALUES IN' clauses with 'PARTITION BY HASH'.
'PARTITION BY HASH' uses the remainder of EXPR divided by the number of partitions (that is, the modulus). For examples and additional information, see *note partitioning-hash::.
The 'LINEAR' keyword entails a somewhat different algorithm. In this case, the number of the partition in which a row is stored is calculated as the result of one or more logical 'AND' operations. For discussion and examples of linear hashing, see *note partitioning-linear-hash::.
'KEY(COLUMN_LIST)'
This is similar to 'HASH', except that MySQL supplies the hashing function so as to guarantee an even data distribution. The COLUMN_LIST argument is simply a list of 1 or more table columns (maximum: 16). This example shows a simple table partitioned by key, with 4 partitions:
CREATE TABLE tk (col1 INT, col2 CHAR(5), col3 DATE)
PARTITION BY KEY(col3)
PARTITIONS 4;
For tables that are partitioned by key, you can employ linear partitioning by using the 'LINEAR' keyword. This has the same effect as with tables that are partitioned by 'HASH'. That is, the partition number is found using the '&' operator rather than the modulus (see note partitioning-linear-hash::, and note partitioning-key::, for details). This example uses linear partitioning by key to distribute data between 5 partitions:
CREATE TABLE tk (col1 INT, col2 CHAR(5), col3 DATE)
PARTITION BY LINEAR KEY(col3)
PARTITIONS 5;
The 'ALGORITHM={1 | 2}' option is supported with '[SUB]PARTITION BY [LINEAR] KEY'. 'ALGORITHM=1' causes the server to use the same key-hashing functions as MySQL 5.1; 'ALGORITHM=2' means that the server employs the key-hashing functions used by default for new 'KEY' partitioned tables in MySQL 5.7 and later. Not specifying the option has the same effect as using 'ALGORITHM=2'. This option is intended for use chiefly when upgrading '[LINEAR] KEY' partitioned tables from MySQL 5.1 to later MySQL versions. For more information, see *note alter-table-partition-operations::.
*note 'mysqldump': mysqldump. writes this option encased in versioned comments, like this:
CREATE TABLE t1 (a INT)
/*!50100 PARTITION BY KEY */ _/*!50611 ALGORITHM = 1 */_ /*!50100 ()
PARTITIONS 3 */
This causes MySQL 5.6.10 and earlier servers to ignore the option, which would otherwise cause a syntax error in those versions.
'ALGORITHM=1' is shown when necessary in the output of note 'SHOW CREATE TABLE': show-create-table. using versioned comments in the same manner as note 'mysqldump': mysqldump. 'ALGORITHM=2' is always omitted from 'SHOW CREATE TABLE' output, even if this option was specified when creating the original table.
You may not use either 'VALUES LESS THAN' or 'VALUES IN' clauses with 'PARTITION BY KEY'.
'RANGE(EXPR)'
In this case, EXPR shows a range of values using a set of 'VALUES LESS THAN' operators. When using range partitioning, you must define at least one partition using 'VALUES LESS THAN'. You cannot use 'VALUES IN' with range partitioning.
Note:
For tables partitioned by 'RANGE', 'VALUES LESS THAN' must be used with either an integer literal value or an expression that evaluates to a single integer value. In MySQL 5.7, you can overcome this limitation in a table that is defined using 'PARTITION BY RANGE COLUMNS', as described later in this section.
Suppose that you have a table that you wish to partition on a column containing year values, according to the following scheme.
Partition Number: Years Range:
0 1990 and earlier
1 1991 to 1994
2 1995 to 1998
3 1999 to 2002
4 2003 to 2005
5 2006 and later
A table implementing such a partitioning scheme can be realized by the *note 'CREATE TABLE': create-table. statement shown here:
CREATE TABLE t1 (
year_col INT,
some_data INT
)
PARTITION BY RANGE (year_col) (
PARTITION p0 VALUES LESS THAN (1991),
PARTITION p1 VALUES LESS THAN (1995),
PARTITION p2 VALUES LESS THAN (1999),
PARTITION p3 VALUES LESS THAN (2002),
PARTITION p4 VALUES LESS THAN (2006),
PARTITION p5 VALUES LESS THAN MAXVALUE
);
'PARTITION ... VALUES LESS THAN ...' statements work in a consecutive fashion. 'VALUES LESS THAN MAXVALUE' works to specify 'leftover' values that are greater than the maximum value otherwise specified.
'VALUES LESS THAN' clauses work sequentially in a manner similar to that of the 'case' portions of a 'switch ... case' block (as found in many programming languages such as C, Java, and PHP). That is, the clauses must be arranged in such a way that the upper limit specified in each successive 'VALUES LESS THAN' is greater than that of the previous one, with the one referencing 'MAXVALUE' coming last of all in the list.
'RANGE COLUMNS(COLUMN_LIST)'
This variant on 'RANGE' facilitates partition pruning for queries using range conditions on multiple columns (that is, having conditions such as 'WHERE a = 1 AND b < 10' or 'WHERE a = 1 AND b = 10 AND c < 10'). It enables you to specify value ranges in multiple columns by using a list of columns in the 'COLUMNS' clause and a set of column values in each 'PARTITION ... VALUES LESS THAN (VALUE_LIST)' partition definition clause. (In the simplest case, this set consists of a single column.) The maximum number of columns that can be referenced in the COLUMN_LIST and VALUE_LIST isThe COLUMN_LIST used in the 'COLUMNS' clause may contain only names of columns; each column in the list must be one of the following MySQL data types: the integer types; the string types; and time or date column types. Columns using 'BLOB', 'TEXT', 'SET', 'ENUM', 'BIT', or spatial data types are not permitted; columns that use floating-point number types are also not permitted. You also may not use functions or arithmetic expressions in the 'COLUMNS' clause.
The 'VALUES LESS THAN' clause used in a partition definition must specify a literal value for each column that appears in the 'COLUMNS()' clause; that is, the list of values used for each 'VALUES LESS THAN' clause must contain the same number of values as there are columns listed in the 'COLUMNS' clause. An attempt to use more or fewer values in a 'VALUES LESS THAN' clause than there are in the 'COLUMNS' clause causes the statement to fail with the error 'Inconsistency in usage of column lists for partitioning...'. You cannot use 'NULL' for any value appearing in 'VALUES LESS THAN'. It is possible to use 'MAXVALUE' more than once for a given column other than the first, as shown in this example:
CREATE TABLE rc (
a INT NOT NULL,
b INT NOT NULL
)
PARTITION BY RANGE COLUMNS(a,b) (
PARTITION p0 VALUES LESS THAN (10,5),
PARTITION p1 VALUES LESS THAN (20,10),
PARTITION p2 VALUES LESS THAN (50,MAXVALUE),
PARTITION p3 VALUES LESS THAN (65,MAXVALUE),
PARTITION p4 VALUES LESS THAN (MAXVALUE,MAXVALUE)
);
Each value used in a 'VALUES LESS THAN' value list must match the type of the corresponding column exactly; no conversion is made. For example, you cannot use the string ''1'' for a value that matches a column that uses an integer type (you must use the numeral '1' instead), nor can you use the numeral '1' for a value that matches a column that uses a string type (in such a case, you must use a quoted string: ''1'').
For more information, see note partitioning-range::, and note partitioning-pruning::.
'LIST(EXPR)'
This is useful when assigning partitions based on a table column with a restricted set of possible values, such as a state or country code. In such a case, all rows pertaining to a certain state or country can be assigned to a single partition, or a partition can be reserved for a certain set of states or countries. It is similar to 'RANGE', except that only 'VALUES IN' may be used to specify permissible values for each partition.
'VALUES IN' is used with a list of values to be matched. For instance, you could create a partitioning scheme such as the following:
CREATE TABLE client_firms (
id INT,
name VARCHAR(35)
)
PARTITION BY LIST (id) (
PARTITION r0 VALUES IN (1, 5, 9, 13, 17, 21),
PARTITION r1 VALUES IN (2, 6, 10, 14, 18, 22),
PARTITION r2 VALUES IN (3, 7, 11, 15, 19, 23),
PARTITION r3 VALUES IN (4, 8, 12, 16, 20, 24)
);
When using list partitioning, you must define at least one partition using 'VALUES IN'. You cannot use 'VALUES LESS THAN' with 'PARTITION BY LIST'.
Note:
For tables partitioned by 'LIST', the value list used with 'VALUES IN' must consist of integer values only. In MySQL 5.7, you can overcome this limitation using partitioning by 'LIST COLUMNS', which is described later in this section.
'LIST COLUMNS(COLUMN_LIST)'
This variant on 'LIST' facilitates partition pruning for queries using comparison conditions on multiple columns (that is, having conditions such as 'WHERE a = 5 AND b = 5' or 'WHERE a = 1 AND b = 10 AND c = 5'). It enables you to specify values in multiple columns by using a list of columns in the 'COLUMNS' clause and a set of column values in each 'PARTITION ... VALUES IN (VALUE_LIST)' partition definition clause.
The rules governing regarding data types for the column list used in 'LIST COLUMNS(COLUMN_LIST)' and the value list used in 'VALUES IN(VALUE_LIST)' are the same as those for the column list used in 'RANGE COLUMNS(COLUMN_LIST)' and the value list used in 'VALUES LESS THAN(VALUE_LIST)', respectively, except that in the 'VALUES IN' clause, 'MAXVALUE' is not permitted, and you may use 'NULL'.
There is one important difference between the list of values used for 'VALUES IN' with 'PARTITION BY LIST COLUMNS' as opposed to when it is used with 'PARTITION BY LIST'. When used with 'PARTITION BY LIST COLUMNS', each element in the 'VALUES IN' clause must be a set of column values; the number of values in each set must be the same as the number of columns used in the 'COLUMNS' clause, and the data types of these values must match those of the columns (and occur in the same order). In the simplest case, the set consists of a single column. The maximum number of columns that can be used in the COLUMN_LIST and in the elements making up the VALUE_LIST isThe table defined by the following 'CREATE TABLE' statement provides an example of a table using 'LIST COLUMNS' partitioning:
CREATE TABLE lc (
a INT NULL,
b INT NULL
)
PARTITION BY LIST COLUMNS(a,b) (
PARTITION p0 VALUES IN( (0,0), (NULL,NULL) ),
PARTITION p1 VALUES IN( (0,1), (0,2), (0,3), (1,1), (1,2) ),
PARTITION p2 VALUES IN( (1,0), (2,0), (2,1), (3,0), (3,1) ),
PARTITION p3 VALUES IN( (1,3), (2,2), (2,3), (3,2), (3,3) )
);
'PARTITIONS NUM'
The number of partitions may optionally be specified with a 'PARTITIONS NUM' clause, where NUM is the number of partitions. If both this clause and any 'PARTITION' clauses are used, NUM must be equal to the total number of any partitions that are declared using 'PARTITION' clauses.
Note:
Whether or not you use a 'PARTITIONS' clause in creating a table that is partitioned by 'RANGE' or 'LIST', you must still include at least one 'PARTITION VALUES' clause in the table definition (see below).
'SUBPARTITION BY'
A partition may optionally be divided into a number of subpartitions. This can be indicated by using the optional 'SUBPARTITION BY' clause. Subpartitioning may be done by 'HASH' or 'KEY'. Either of these may be 'LINEAR'. These work in the same way as previously described for the equivalent partitioning types. (It is not possible to subpartition by 'LIST' or 'RANGE'.)
The number of subpartitions can be indicated using the 'SUBPARTITIONS' keyword followed by an integer value.
Rigorous checking of the value used in 'PARTITIONS' or 'SUBPARTITIONS' clauses is applied and this value must adhere to the following rules:
* The value must be a positive, nonzero integer.
* No leading zeros are permitted.
* The value must be an integer literal, and cannot not be an
expression. For example, 'PARTITIONS 0.2E+01' is not
permitted, even though '0.2E+01' evaluates to '2'. (Bug
#15890)
'PARTITION_DEFINITION'
Each partition may be individually defined using a PARTITION_DEFINITION clause. The individual parts making up this clause are as follows:
* 'PARTITION PARTITION_NAME'
Specifies a logical name for the partition.
* 'VALUES'
For range partitioning, each partition must include a 'VALUES
LESS THAN' clause; for list partitioning, you must specify a
'VALUES IN' clause for each partition. This is used to
determine which rows are to be stored in this partition. See
the discussions of partitioning types in *note partitioning::,
for syntax examples.
* '[STORAGE] ENGINE'
The partitioning handler accepts a '[STORAGE] ENGINE' option
for both 'PARTITION' and 'SUBPARTITION'. Currently, the only
way in which this can be used is to set all partitions or all
subpartitions to the same storage engine, and an attempt to
set different storage engines for partitions or subpartitions
in the same table raises the error 'ERROR 1469 (HY000): The
mix of handlers in the partitions is not permitted in this
version of MySQL'. We expect to lift this restriction on
partitioning in a future MySQL release.
* 'COMMENT'
An optional 'COMMENT' clause may be used to specify a string
that describes the partition. Example:
COMMENT = 'Data for the years previous to 1999'
The maximum length for a partition comment is 1024 characters.
* 'DATA DIRECTORY' and 'INDEX DIRECTORY'
'DATA DIRECTORY' and 'INDEX DIRECTORY' may be used to indicate
the directory where, respectively, the data and indexes for
this partition are to be stored. Both the 'DATA_DIR' and the
'INDEX_DIR' must be absolute system path names.
As of MySQL 5.7.17, you must have the 'FILE' privilege to use
the 'DATA DIRECTORY' or 'INDEX DIRECTORY' partition option.
Example:
CREATE TABLE th (id INT, name VARCHAR(30), adate DATE)
PARTITION BY LIST(YEAR(adate))
(
PARTITION p1999 VALUES IN (1995, 1999, 2003)
DATA DIRECTORY = '/var/appdata/95/data'
INDEX DIRECTORY = '/var/appdata/95/idx',
PARTITION p2000 VALUES IN (1996, 2000, 2004)
DATA DIRECTORY = '/var/appdata/96/data'
INDEX DIRECTORY = '/var/appdata/96/idx',
PARTITION p2001 VALUES IN (1997, 2001, 2005)
DATA DIRECTORY = '/var/appdata/97/data'
INDEX DIRECTORY = '/var/appdata/97/idx',
PARTITION p2002 VALUES IN (1998, 2002, 2006)
DATA DIRECTORY = '/var/appdata/98/data'
INDEX DIRECTORY = '/var/appdata/98/idx'
);
'DATA DIRECTORY' and 'INDEX DIRECTORY' behave in the same way
as in the *note 'CREATE TABLE': create-table. statement's
TABLE_OPTION clause as used for 'MyISAM' tables.
One data directory and one index directory may be specified
per partition. If left unspecified, the data and indexes are
stored by default in the table's database directory.
On Windows, the 'DATA DIRECTORY' and 'INDEX DIRECTORY' options
are not supported for individual partitions or subpartitions
of *note 'MyISAM': myisam-storage-engine. tables, and the
'INDEX DIRECTORY' option is not supported for individual
partitions or subpartitions of *note 'InnoDB':
innodb-storage-engine. tables. These options are ignored on
Windows, except that a warning is generated. (Bug #30459)
*Note*:
The 'DATA DIRECTORY' and 'INDEX DIRECTORY' options are ignored
for creating partitioned tables if 'NO_DIR_IN_CREATE' is in
effect. (Bug #24633)
* 'MAX_ROWS' and 'MIN_ROWS'
May be used to specify, respectively, the maximum and minimum
number of rows to be stored in the partition. The values for
MAX_NUMBER_OF_ROWS and MIN_NUMBER_OF_ROWS must be positive
integers. As with the table-level options with the same
names, these act only as 'suggestions' to the server and are
not hard limits.
* 'TABLESPACE'
May be used to designate a tablespace for the partition.
Supported by NDB Cluster. For 'InnoDB' tables, it may be used
to designate a file-per-table tablespace for the partition by
specifying 'TABLESPACE `innodb_file_per_table`'. All
partitions must belong to the same storage engine.
*Note*:
Support for placing 'InnoDB' table partitions in shared
'InnoDB' tablespaces is deprecated in MySQL 5.7.24; expect it
to be removed in a future MySQL version. Shared tablespaces
include the 'InnoDB' system tablespace and general
tablespaces.
'SUBPARTITION_DEFINITION'
The partition definition may optionally contain one or more SUBPARTITION_DEFINITION clauses. Each of these consists at a minimum of the 'SUBPARTITION NAME', where NAME is an identifier for the subpartition. Except for the replacement of the 'PARTITION' keyword with 'SUBPARTITION', the syntax for a subpartition definition is identical to that for a partition definition.
Subpartitioning must be done by 'HASH' or 'KEY', and can be done only on 'RANGE' or 'LIST' partitions. See *note partitioning-subpartitions::.
Partitioning by Generated Columns
Partitioning by generated columns is permitted. For example:
CREATE TABLE t1 (
s1 INT,
s2 INT AS (EXP(s1)) STORED
)
PARTITION BY LIST (s2) (
PARTITION p1 VALUES IN (1)
);
Partitioning sees a generated column as a regular column, which enables workarounds for limitations on functions that are not permitted for partitioning (see *note partitioning-limitations-functions::). The preceding example demonstrates this technique: 'EXP()' cannot be used directly in the 'PARTITION BY' clause, but a generated column defined using 'EXP()' is permitted.
File: manual.info.tmp, Node: create-table-files, Next: create-temporary-table, Prev: create-table, Up: create-table
13.1.18.1 Files Created by CREATE TABLE .......................................
MySQL represents each table by an '.frm' table format (definition) file in the database directory. The storage engine for the table might create other files as well.
For an 'InnoDB' table created in a file-per-table tablespace or general tablespace, table data and associated indexes are stored in a .ibd file in the database directory. When an 'InnoDB' table is created in the system tablespace, table data and indexes are stored in the ibdata* files that represent the system tablespace. The 'innodb_file_per_table' option controls whether tables are created in file-per-table tablespaces or the system tablespace, by default. The 'TABLESPACE' option can be used to place a table in a file-per-table tablespace, general tablespace, or the system tablespace, regardless of the 'innodb_file_per_table' setting.
For 'MyISAM' tables, the storage engine creates data and index files. Thus, for each 'MyISAM' table TBL_NAME, there are three disk files.
File Purpose
'TBL_NAME.frm' Table format (definition) file
'TBL_NAME.MYD' Data file
'TBL_NAME.MYI' Index file
note storage-engines::, describes what files each storage engine creates to represent tables. If a table name contains special characters, the names for the table files contain encoded versions of those characters as described in note identifier-mapping::.
Limits Imposed by .frm File Structure
As described previously, each table has an '.frm' file that contains the table definition. The server uses the following expression to check some of the table information stored in the file against an upper limit of 64KB:
if (info_length+(ulong) create_fields.elements*FCOMP+288+
n_length+int_length+com_length > 65535L || int_count > 255)
The portion of the information stored in the '.frm' file that is checked against the expression cannot grow beyond the 64KB limit, so if the table definition reaches this size, no more columns can be added.
The relevant factors in the expression are:
'info_length' is space needed for 'screens.' This is related to MySQL's Unireg heritage.
'create_fields.elements' is the number of columns.
'FCOMP' is 17.
'n_length' is the total length of all column names, including one byte per name as a separator.
'int_length' is related to the list of values for note 'ENUM': enum. and note 'SET': set. columns. In this context, 'int' does not mean 'integer.' It means 'interval,' a term that refers collectively to note 'ENUM': enum. and note 'SET': set. columns.
'int_count' is the number of unique note 'ENUM': enum. and note 'SET': set. definitions.
'com_length' is the total length of column comments.
The expression just described has several implications for permitted table definitions:
Using long column names can reduce the maximum number of columns, as can the inclusion of note 'ENUM': enum. or note 'SET': set. columns, or use of column comments.
A table can have no more than 255 unique note 'ENUM': enum. and note 'SET': set. definitions. Columns with identical element lists are considered the same against this limt. For example, if a table contains these two columns, they count as one (not two) toward this limit because the definitions are identical:
e1 ENUM('a','b','c')
e2 ENUM('a','b','c')
The sum of the length of element names in the unique note 'ENUM': enum. and note 'SET': set. definitions counts toward the 64KB limit, so although the theoretical limit on number of elements in a given *note 'ENUM': enum. column is 65,535, the practical limit is less than 3000.
File: manual.info.tmp, Node: create-temporary-table, Next: create-table-like, Prev: create-table-files, Up: create-table
13.1.18.2 CREATE TEMPORARY TABLE Statement ..........................................
You can use the 'TEMPORARY' keyword when creating a table. A 'TEMPORARY' table is visible only within the current session, and is dropped automatically when the session is closed. This means that two different sessions can use the same temporary table name without conflicting with each other or with an existing non-'TEMPORARY' table of the same name. (The existing table is hidden until the temporary table is dropped.)
note 'CREATE TABLE': create-table. causes an implicit commit, except when used with the 'TEMPORARY' keyword. See note implicit-commit::.
'TEMPORARY' tables have a very loose relationship with databases (schemas). Dropping a database does not automatically drop any 'TEMPORARY' tables created within that database. Also, you can create a 'TEMPORARY' table in a nonexistent database if you qualify the table name with the database name in the 'CREATE TABLE' statement. In this case, all subsequent references to the table must be qualified with the database name.
To create a temporary table, you must have the 'CREATE TEMPORARY TABLES' privilege. After a session has created a temporary table, the server performs no further privilege checks on the table. The creating session can perform any operation on the table, such as note 'DROP TABLE': drop-table, note 'INSERT': insert, note 'UPDATE': update, or note 'SELECT': select.
One implication of this behavior is that a session can manipulate its temporary tables even if the current user has no privilege to create them. Suppose that the current user does not have the 'CREATE TEMPORARY TABLES' privilege but is able to execute a definer-context stored procedure that executes with the privileges of a user who does have 'CREATE TEMPORARY TABLES' and that creates a temporary table. While the procedure executes, the session uses the privileges of the defining user. After the procedure returns, the effective privileges revert to those of the current user, which can still see the temporary table and perform any operation on it.
Note:
Support for 'TABLESPACE = innodb_file_per_table' and 'TABLESPACE = innodb_temporary' clauses with *note 'CREATE TEMPORARY TABLE': create-table. is deprecated as of MySQL 5.7.24; expect it to be removed in a future version of MySQL.
File: manual.info.tmp, Node: create-table-like, Next: create-table-select, Prev: create-temporary-table, Up: create-table
13.1.18.3 CREATE TABLE ... LIKE Statement .........................................
Use 'CREATE TABLE ... LIKE' to create an empty table based on the definition of another table, including any column attributes and indexes defined in the original table:
CREATE TABLE NEW_TBL LIKE ORIG_TBL;
The copy is created using the same version of the table storage format as the original table. The 'SELECT' privilege is required on the original table.
'LIKE' works only for base tables, not for views.
Important:
You cannot execute 'CREATE TABLE' or 'CREATE TABLE ... LIKE' while a *note 'LOCK TABLES': lock-tables. statement is in effect.
note 'CREATE TABLE ... LIKE': create-table. makes the same checks as note 'CREATE TABLE': create-table. and does not just copy the '.frm' file. This means that, if the current SQL mode is different from the mode in effect when the original table was created, the table definition might be considered invalid for the new mode, and the statement fails.
For 'CREATE TABLE ... LIKE', the destination table preserves generated column information from the original table.
'CREATE TABLE ... LIKE' does not preserve any 'DATA DIRECTORY' or 'INDEX DIRECTORY' table options that were specified for the original table, or any foreign key definitions.
If the original table is a 'TEMPORARY' table, 'CREATE TABLE ... LIKE' does not preserve 'TEMPORARY'. To create a 'TEMPORARY' destination table, use 'CREATE TEMPORARY TABLE ... LIKE'.
File: manual.info.tmp, Node: create-table-select, Next: create-table-foreign-keys, Prev: create-table-like, Up: create-table
13.1.18.4 CREATE TABLE ... SELECT Statement ...........................................
You can create one table from another by adding a note 'SELECT': select. statement at the end of the note 'CREATE TABLE': create-table. statement:
CREATE TABLE NEW_TBL [AS] SELECT * FROM ORIG_TBL;
MySQL creates new columns for all elements in the *note 'SELECT': select. For example:
mysql> CREATE TABLE test (a INT NOT NULL AUTO_INCREMENT,
-> PRIMARY KEY (a), KEY(b))
-> ENGINE=InnoDB SELECT b,c FROM test2;
This creates an note 'InnoDB': innodb-storage-engine. table with three columns, 'a', 'b', and 'c'. The 'ENGINE' option is part of the note 'CREATE TABLE': create-table. statement, and should not be used following the note 'SELECT': select.; this would result in a syntax error. The same is true for other note 'CREATE TABLE': create-table. options such as 'CHARSET'.
Notice that the columns from the *note 'SELECT': select. statement are appended to the right side of the table, not overlapped onto it. Take the following example:
mysql> SELECT * FROM foo;
+---+
| n |
+---+
| 1 |
+---+
mysql> CREATE TABLE bar (m INT) SELECT n FROM foo;
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM bar;
+------+---+
| m | n |
+------+---+
| NULL | 1 |
+------+---+
1 row in set (0.00 sec)
For each row in table 'foo', a row is inserted in 'bar' with the values from 'foo' and default values for the new columns.
In a table resulting from note 'CREATE TABLE ... SELECT': create-table, columns named only in the note 'CREATE TABLE': create-table. part come first. Columns named in both parts or only in the note 'SELECT': select. part come after that. The data type of note 'SELECT': select. columns can be overridden by also specifying the column in the *note 'CREATE TABLE': create-table. part.
If any errors occur while copying the data to the table, it is automatically dropped and not created.
You can precede the note 'SELECT': select. by 'IGNORE' or 'REPLACE' to indicate how to handle rows that duplicate unique key values. With 'IGNORE', rows that duplicate an existing row on a unique key value are discarded. With 'REPLACE', new rows replace rows that have the same unique key value. If neither 'IGNORE' nor 'REPLACE' is specified, duplicate unique key values result in an error. For more information, see note ignore-effect-on-execution::.
Because the ordering of the rows in the underlying note 'SELECT': select. statements cannot always be determined, 'CREATE TABLE ... IGNORE SELECT' and 'CREATE TABLE ... REPLACE SELECT' statements are flagged as unsafe for statement-based replication. Such statements produce a warning in the error log when using statement-based mode and are written to the binary log using the row-based format when using 'MIXED' mode. See also note replication-sbr-rbr::.
note 'CREATE TABLE ... SELECT': create-table. does not automatically create any indexes for you. This is done intentionally to make the statement as flexible as possible. If you want to have indexes in the created table, you should specify these before the note 'SELECT': select. statement:
mysql> CREATE TABLE bar (UNIQUE (n)) SELECT n FROM foo;
For 'CREATE TABLE ... SELECT', the destination table does not preserve information about whether columns in the selected-from table are generated columns. The *note 'SELECT': select. part of the statement cannot assign values to generated columns in the destination table.
Some conversion of data types might occur. For example, the 'AUTO_INCREMENT' attribute is not preserved, and note 'VARCHAR': char. columns can become note 'CHAR': char. columns. Retrained attributes are 'NULL' (or 'NOT NULL') and, for those columns that have them, 'CHARACTER SET', 'COLLATION', 'COMMENT', and the 'DEFAULT' clause.
When creating a table with *note 'CREATE TABLE ... SELECT': create-table-select, make sure to alias any function calls or expressions in the query. If you do not, the 'CREATE' statement might fail or result in undesirable column names.
CREATE TABLE artists_and_works
SELECT artist.name, COUNT(work.artist_id) AS number_of_works
FROM artist LEFT JOIN work ON artist.id = work.artist_id
GROUP BY artist.id;
You can also explicitly specify the data type for a column in the created table:
CREATE TABLE foo (a TINYINT NOT NULL) SELECT b+1 AS a FROM bar;
For *note 'CREATE TABLE ... SELECT': create-table, if 'IF NOT EXISTS' is given and the target table exists, nothing is inserted into the destination table, and the statement is not logged.
To ensure that the binary log can be used to re-create the original tables, MySQL does not permit concurrent inserts during *note 'CREATE TABLE ... SELECT': create-table.
You cannot use 'FOR UPDATE' as part of the note 'SELECT': select. in a statement such as note 'CREATE TABLE NEW_TABLE SELECT ... FROM OLD_TABLE ...': create-table-select. If you attempt to do so, the statement fails.
File: manual.info.tmp, Node: create-table-foreign-keys, Next: silent-column-changes, Prev: create-table-select, Up: create-table
13.1.18.5 FOREIGN KEY Constraints .................................
MySQL supports foreign keys, which permit cross-referencing related data across tables, and foreign key constraints, which help keep the related data consistent.
A foreign key relationship involves a parent table that holds the initial column values, and a child table with column values that reference the parent column values. A foreign key constraint is defined on the child table.
The essential syntax for a defining a foreign key constraint in a note 'CREATE TABLE': create-table. or note 'ALTER TABLE': alter-table. statement includes the following:
[CONSTRAINT [SYMBOL]] FOREIGN KEY
[INDEX_NAME] (COL_NAME, ...)
REFERENCES TBL_NAME (COL_NAME,...)
[ON DELETE REFERENCE_OPTION]
[ON UPDATE REFERENCE_OPTION]
REFERENCE_OPTION:
RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT
Foreign key constraint usage is described under the following topics in this section:
*note foreign-key-identifiers::
*note foreign-key-restrictions::
*note foreign-key-referential-actions::
*note foreign-key-examples::
*note foreign-key-adding::
*note foreign-key-dropping::
*note foreign-key-checks::
*note foreign-key-metadata::
*note foreign-key-errors::
Identifiers
Foreign key constraint naming is governed by the following rules:
The 'CONSTRAINT' SYMBOL value is used, if defined.
If the 'CONSTRAINT' SYMBOL clause is not defined, or a symbol is not included following the 'CONSTRAINT' keyword:
* For 'InnoDB' tables, a constraint name is generated
automatically.
* For 'NDB' tables, the 'FOREIGN KEY' INDEX_NAME value is used,
if defined. Otherwise, a constraint name is generated
automatically.
The 'CONSTRAINT SYMBOL' value, if defined, must be unique in the database. A duplicate SYMBOL results in an error similar to: 'ERROR 1005 (HY000): Can't create table 'test.fk1' (errno: 121)'.
Table and column identifiers in a 'FOREIGN KEY ... REFERENCES' clause can be quoted within backticks ('`'). Alternatively, double quotation marks ('"') can be used if the 'ANSI_QUOTES' SQL mode is enabled. The 'lower_case_table_names' system variable setting is also taken into account.
Conditions and Restrictions
Foreign key constraints are subject to the following conditions and restrictions:
Parent and child tables must use the same storage engine, and they cannot be defined as temporary tables.
Creating a foreign key constraint requires the 'REFERENCES' privilege on the parent table.
Corresponding columns in the foreign key and the referenced key must have similar data types. The size and sign of fixed precision types such as note 'INTEGER': integer-types. and note 'DECIMAL': fixed-point-types. must be the same. The length of string types need not be the same. For nonbinary (character) string columns, the character set and collation must be the same.
MySQL supports foreign key references between one column and another within a table. (A column cannot have a foreign key reference to itself.) In these cases, a 'child table record' refers to a dependent record within the same table.
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later if you create another index that can be used to enforce the foreign key constraint. INDEX_NAME, if given, is used as described previously.
'InnoDB' permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are the first columns in the same order. Hidden columns that 'InnoDB' adds to an index are also considered (see *note innodb-index-types::).
'NDB' requires an explicit unique key (or primary key) on any column referenced as a foreign key. 'InnoDB' does not, which is an extension of standard SQL.
Index prefixes on foreign key columns are not supported. Consequently, note 'BLOB': blob. and note 'TEXT': blob. columns cannot be included in a foreign key because indexes on those columns must always include a prefix length.
*note 'InnoDB': innodb-storage-engine. does not currently support foreign keys for tables with user-defined partitioning. This includes both parent and child tables.
This restriction does not apply for *note 'NDB': mysql-cluster. tables that are partitioned by 'KEY' or 'LINEAR KEY' (the only user partitioning types supported by the 'NDB' storage engine); these may have foreign key references or be the targets of such references.
A table in a foreign key relationship cannot be altered to use another storage engine. To change the storage engine, you must drop any foreign key constraints first.
A foreign key constraint cannot reference a virtual generated column.
Prior to 5.7.16, a foreign key constraint cannot reference a secondary index defined on a virtual generated column.
For information about how the MySQL implementation of foreign key constraints differs from the SQL standard, see *note ansi-diff-foreign-keys::.
Referential Actions
When an note 'UPDATE': update. or note 'DELETE': delete. operation affects a key value in the parent table that has matching rows in the child table, the result depends on the referential action specified by 'ON UPDATE' and 'ON DELETE' subclauses of the 'FOREIGN KEY' clause. Referential actions include:
'CASCADE': Delete or update the row from the parent table and automatically delete or update the matching rows in the child table. Both 'ON DELETE CASCADE' and 'ON UPDATE CASCADE' are supported. Between two tables, do not define several 'ON UPDATE CASCADE' clauses that act on the same column in the parent table or in the child table.
If a 'FOREIGN KEY' clause is defined on both tables in a foreign key relationship, making both tables a parent and child, an 'ON UPDATE CASCADE' or 'ON DELETE CASCADE' subclause defined for one 'FOREIGN KEY' clause must be defined for the other in order for cascading operations to succeed. If an 'ON UPDATE CASCADE' or 'ON DELETE CASCADE' subclause is only defined for one 'FOREIGN KEY' clause, cascading operations fail with an error.
Note:
Cascaded foreign key actions do not activate triggers.
'SET NULL': Delete or update the row from the parent table and set the foreign key column or columns in the child table to 'NULL'. Both 'ON DELETE SET NULL' and 'ON UPDATE SET NULL' clauses are supported.
If you specify a 'SET NULL' action, make sure that you have not declared the columns in the child table as 'NOT NULL'.
'RESTRICT': Rejects the delete or update operation for the parent table. Specifying 'RESTRICT' (or 'NO ACTION') is the same as omitting the 'ON DELETE' or 'ON UPDATE' clause.
'NO ACTION': A keyword from standard SQL. For note 'InnoDB': innodb-storage-engine, this is equivalent to 'RESTRICT'; the delete or update operation for the parent table is immediately rejected if there is a related foreign key value in the referenced table. note 'NDB': mysql-cluster. supports deferred checks, and 'NO ACTION' specifies a deferred check; when this is used, constraint checks are not performed until commit time. Note that for 'NDB' tables, this causes all foreign key checks made for both parent and child tables to be deferred.
'SET DEFAULT': This action is recognized by the MySQL parser, but both note 'InnoDB': innodb-storage-engine. and note 'NDB': mysql-cluster. reject table definitions containing 'ON DELETE SET DEFAULT' or 'ON UPDATE SET DEFAULT' clauses.
For storage engines that support foreign keys, MySQL rejects any note 'INSERT': insert. or note 'UPDATE': update. operation that attempts to create a foreign key value in a child table if there is no matching candidate key value in the parent table.
For an 'ON DELETE' or 'ON UPDATE' that is not specified, the default action is always 'RESTRICT'.
For *note 'NDB': mysql-cluster. tables, 'ON UPDATE CASCADE' is not supported where the reference is to the parent table's primary key.
As of NDB 7.5.14 and NDB 7.6.10: For note 'NDB': mysql-cluster. tables, 'ON DELETE CASCADE' is not supported where the child table contains one or more columns of any of the note 'TEXT': blob. or *note 'BLOB': blob. types. (Bug #89511, Bug #27484882)
'InnoDB' performs cascading operations using a depth-first search algorithm on the records of the index that corresponds to the foreign key constraint.
A foreign key constraint on a stored generated column cannot use 'CASCADE', 'SET NULL', or 'SET DEFAULT' as 'ON UPDATE' referential actions, nor can it use 'SET NULL' or 'SET DEFAULT' as 'ON DELETE' referential actions.
A foreign key constraint on the base column of a stored generated column cannot use 'CASCADE', 'SET NULL', or 'SET DEFAULT' as 'ON UPDATE' or 'ON DELETE' referential actions.
In MySQL 5.7.13 and earlier, 'InnoDB' does not permit defining a foreign key constraint with a cascading referential action on the base column of an indexed virtual generated column. This restriction is lifted in MySQL 5.7.14.
In MySQL 5.7.13 and earlier, 'InnoDB' does not permit defining cascading referential actions on non-virtual foreign key columns that are explicitly included in a virtual index. This restriction is lifted in MySQL 5.7.14.
Foreign Key Constraint Examples
This simple example relates 'parent' and 'child' tables through a single-column foreign key:
CREATE TABLE parent (
id INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (
id INT,
parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id)
REFERENCES parent(id)
ON DELETE CASCADE
) ENGINE=INNODB;
This is a more complex example in which a 'product_order' table has foreign keys for two other tables. One foreign key references a two-column index in the 'product' table. The other references a single-column index in the 'customer' table:
CREATE TABLE product (
category INT NOT NULL, id INT NOT NULL,
price DECIMAL,
PRIMARY KEY(category, id)
) ENGINE=INNODB;
CREATE TABLE customer (
id INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE product_order (
no INT NOT NULL AUTO_INCREMENT,
product_category INT NOT NULL,
product_id INT NOT NULL,
customer_id INT NOT NULL,
PRIMARY KEY(no),
INDEX (product_category, product_id),
INDEX (customer_id),
FOREIGN KEY (product_category, product_id)
REFERENCES product(category, id)
ON UPDATE CASCADE ON DELETE RESTRICT,
FOREIGN KEY (customer_id)
REFERENCES customer(id)
) ENGINE=INNODB;
Adding Foreign Key Constraints
You can add a foreign key constraint to an existing table using the following *note 'ALTER TABLE': alter-table. syntax:
ALTER TABLE TBL_NAME
ADD [CONSTRAINT [SYMBOL]] FOREIGN KEY
[INDEX_NAME] (COL_NAME, ...)
REFERENCES TBL_NAME (COL_NAME,...)
[ON DELETE REFERENCE_OPTION]
[ON UPDATE REFERENCE_OPTION]
The foreign key can be self referential (referring to the same table). When you add a foreign key constraint to a table using *note 'ALTER TABLE': alter-table, remember to first create an index on the column(s) referenced by the foreign key.
Dropping Foreign Key Constraints
You can drop a foreign key constraint using the following *note 'ALTER TABLE': alter-table. syntax:
ALTER TABLE TBL_NAME DROP FOREIGN KEY FK_SYMBOL;
If the 'FOREIGN KEY' clause defined a 'CONSTRAINT' name when you created the constraint, you can refer to that name to drop the foreign key constraint. Otherwise, a constraint name was generated internally, and you must use that value. To determine the foreign key constraint name, use *note 'SHOW CREATE TABLE': show-create-table.:
mysql> SHOW CREATE TABLE child\G
*************************** 1. row ***************************
Table: child
Create Table: CREATE TABLE `child` (
`id` int(11) DEFAULT NULL,
`parent_id` int(11) DEFAULT NULL,
KEY `par_ind` (`parent_id`),
CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`)
REFERENCES `parent` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
mysql> ALTER TABLE child DROP FOREIGN KEY `child_ibfk_1`;
Adding and dropping a foreign key in the same note 'ALTER TABLE': alter-table. statement is supported for note 'ALTER TABLE ... ALGORITHM=INPLACE': alter-table. It is not supported for *note 'ALTER TABLE ... ALGORITHM=COPY': alter-table.
Foreign Key Checks
In MySQL, InnoDB and NDB tables support checking of foreign key constraints. Foreign key checking is controlled by the 'foreign_key_checks' variable, which is enabled by default. Typically, you leave this variable enabled during normal operation to enforce referential integrity. The 'foreign_key_checks' variable has the same effect on note 'NDB': mysql-cluster. tables as it does for note 'InnoDB': innodb-storage-engine. tables.
The 'foreign_key_checks' variable is dynamic and supports both global and session scopes. For information about using system variables, see *note using-system-variables::.
Disabling foreign key checking is useful when:
Dropping a table that is referenced by a foreign key constraint. A referenced table can only be dropped after 'foreign_key_checks' is disabled. When you drop a table, constraints defined on the table are also dropped.
Reloading tables in different order than required by their foreign key relationships. For example, note 'mysqldump': mysqldump. produces correct definitions of tables in the dump file, including foreign key constraints for child tables. To make it easier to reload dump files for tables with foreign key relationships, note 'mysqldump': mysqldump. automatically includes a statement in the dump output that disables 'foreign_key_checks'. This enables you to import the tables in any order in case the dump file contains tables that are not correctly ordered for foreign keys. Disabling 'foreign_key_checks' also speeds up the import operation by avoiding foreign key checks.
Executing *note 'LOAD DATA': load-data. operations, to avoid foreign key checking.
Performing an *note 'ALTER TABLE': alter-table. operation on a table that has a foreign key relationship.
When 'foreign_key_checks' is disabled, foreign key constraints are ignored, with the following exceptions:
Recreating a table that was previously dropped returns an error if the table definition does not conform to the foreign key constraints that reference the table. The table must have the correct column names and types. It must also have indexes on the referenced keys. If these requirements are not satisfied, MySQL returns Error 1005 that refers to errno: 150 in the error message, which means that a foreign key constraint was not correctly formed.
Altering a table returns an error (errno: 150) if a foreign key definition is incorrectly formed for the altered table.
Dropping an index required by a foreign key constraint. The foreign key constraint must be removed before dropping the index.
Creating a foreign key constraint where a column references a nonmatching column type.
Disabling 'foreign_key_checks' has these additional implications:
It is permitted to drop a database that contains tables with foreign keys that are referenced by tables outside the database.
It is permitted to drop a table with foreign keys referenced by other tables.
Enabling 'foreign_key_checks' does not trigger a scan of table data, which means that rows added to a table while 'foreign_key_checks' is disabled are not checked for consistency when 'foreign_key_checks' is re-enabled.
Foreign Key Definitions and Metadata
To view a foreign key definition, use *note 'SHOW CREATE TABLE': show-create-table.:
mysql> SHOW CREATE TABLE child\G
*************************** 1. row ***************************
Table: child
Create Table: CREATE TABLE `child` (
`id` int(11) DEFAULT NULL,
`parent_id` int(11) DEFAULT NULL,
KEY `par_ind` (`parent_id`),
CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`)
REFERENCES `parent` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
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 |
+--------------+------------+-------------+-----------------+
| test | child | parent_id | child_ibfk_1 |
+--------------+------------+-------------+-----------------+
You can obtain information specific to 'InnoDB' foreign keys from 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. Example queries are show here:
mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_SYS_FOREIGN \G
*************************** 1. row ***************************
ID: test/child_ibfk_1
FOR_NAME: test/child
REF_NAME: test/parent
N_COLS: 1
TYPE: 1
mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_SYS_FOREIGN_COLS \G
*************************** 1. row ***************************
ID: test/child_ibfk_1
FOR_COL_NAME: parent_id
REF_COL_NAME: id
POS: 0
Foreign Key Errors
In the event of a foreign key error involving 'InnoDB' tables (usually Error 150 in the MySQL Server), information about the latest foreign key error can be obtained by checking *note 'SHOW ENGINE INNODB STATUS': show-engine. output.
mysql> SHOW ENGINE INNODB STATUS\G
...
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2014-10-16 18:35:18 0x7fc2a95c1700 Transaction:
TRANSACTION 1814, ACTIVE 0 sec inserting
mysql tables in use 1, locked 1
4 lock struct(s), heap size 1136, 3 row lock(s), undo log entries 3
MySQL thread id 2, OS thread handle 140474041767680, query id 74 localhost
root update
INSERT INTO child VALUES
(NULL, 1)
, (NULL, 2)
, (NULL, 3)
, (NULL, 4)
, (NULL, 5)
, (NULL, 6)
Foreign key constraint fails for table `mysql`.`child`:
,
CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent`
(`id`) ON DELETE CASCADE ON UPDATE CASCADE
Trying to add in child table, in index par_ind tuple:
DATA TUPLE: 2 fields;
0: len 4; hex 80000003; asc ;;
1: len 4; hex 80000003; asc ;;
But in parent table `mysql`.`parent`, in index PRIMARY,
the closest match we can find is record:
PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 4; hex 80000004; asc ;;
1: len 6; hex 00000000070a; asc ;;
2: len 7; hex aa0000011d0134; asc 4;;
...
Warning:
'ER_NO_REFERENCED_ROW_2' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_no_referenced_row_2) and 'ER_ROW_IS_REFERENCED_2' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_row_is_referenced_2) error messages for foreign key operations expose information about parent tables, even if the user has no parent table access privileges. To hide information about parent tables, include the appropriate condition handlers in application code and stored programs.
File: manual.info.tmp, Node: silent-column-changes, Next: create-table-generated-columns, Prev: create-table-foreign-keys, Up: create-table
13.1.18.6 Silent Column Specification Changes .............................................
In some cases, MySQL silently changes column specifications from those given in a note 'CREATE TABLE': create-table. or note 'ALTER TABLE': alter-table. statement. These might be changes to a data type, to attributes associated with a data type, or to an index specification.
All changes are subject to the internal row-size limit of 65,535 bytes, which may cause some attempts at data type changes to fail. See *note column-count-limit::.
Columns that are part of a 'PRIMARY KEY' are made 'NOT NULL' even if not declared that way.
Trailing spaces are automatically deleted from note 'ENUM': enum. and note 'SET': set. member values when the table is created.
MySQL maps certain data types used by other SQL database vendors to MySQL types. See *note other-vendor-data-types::.
If you include a 'USING' clause to specify an index type that is not permitted for a given storage engine, but there is another index type available that the engine can use without affecting query results, the engine uses the available type.
If strict SQL mode is not enabled, a note 'VARCHAR': char. column with a length specification greater than 65535 is converted to note 'TEXT': blob, and a note 'VARBINARY': binary-varbinary. column with a length specification greater than 65535 is converted to note 'BLOB': blob. Otherwise, an error occurs in either of these cases.
Specifying the 'CHARACTER SET binary' attribute for a character data type causes the column to be created as the corresponding binary data type: note 'CHAR': char. becomes note 'BINARY': binary-varbinary, note 'VARCHAR': char. becomes note 'VARBINARY': binary-varbinary, and note 'TEXT': blob. becomes note 'BLOB': blob. For the note 'ENUM': enum. and note 'SET': set. data types, this does not occur; they are created as declared. Suppose that you specify a table using this definition:
CREATE TABLE t
(
c1 VARCHAR(10) CHARACTER SET binary,
c2 TEXT CHARACTER SET binary,
c3 ENUM('a','b','c') CHARACTER SET binary
);
The resulting table has this definition:
CREATE TABLE t
(
c1 VARBINARY(10),
c2 BLOB,
c3 ENUM('a','b','c') CHARACTER SET binary
);
To see whether MySQL used a data type other than the one you specified, issue a note 'DESCRIBE': describe. or note 'SHOW CREATE TABLE': show-create-table. statement after creating or altering the table.
Certain other data type changes can occur if you compress a table using note 'myisampack': myisampack. See note compressed-format::.
File: manual.info.tmp, Node: create-table-generated-columns, Next: create-table-secondary-indexes, Prev: silent-column-changes, Up: create-table
13.1.18.7 CREATE TABLE and Generated Columns ............................................
*note 'CREATE TABLE': create-table. supports the specification of generated columns. Values of a generated column are computed from an expression included in the column definition.
Generated columns are supported by the *note 'NDB': mysql-cluster. storage engine beginning with MySQL NDB Cluster 7.5.3.
The following simple example shows a table that stores the lengths of the sides of right triangles in the 'sidea' and 'sideb' columns, and computes the length of the hypotenuse in 'sidec' (the square root of the sums of the squares of the other sides):
CREATE TABLE triangle (
sidea DOUBLE,
sideb DOUBLE,
sidec DOUBLE AS (SQRT(sidea * sidea + sideb * sideb))
);
INSERT INTO triangle (sidea, sideb) VALUES(1,1),(3,4),(6,8);
Selecting from the table yields this result:
mysql> SELECT * FROM triangle;
+-------+-------+--------------------+
| sidea | sideb | sidec |
+-------+-------+--------------------+
| 1 | 1 | 1.4142135623730951 |
| 3 | 4 | 5 |
| 6 | 8 | 10 |
+-------+-------+--------------------+
Any application that uses the 'triangle' table has access to the hypotenuse values without having to specify the expression that calculates them.
Generated column definitions have this syntax:
COL_NAME DATA_TYPE [GENERATED ALWAYS] AS (EXPR)
[VIRTUAL | STORED] [NOT NULL | NULL]
[UNIQUE [KEY]] [[PRIMARY] KEY]
[COMMENT 'STRING']
'AS (EXPR)' indicates that the column is generated and defines the expression used to compute column values. 'AS' may be preceded by 'GENERATED ALWAYS' to make the generated nature of the column more explicit. Constructs that are permitted or prohibited in the expression are discussed later.
The 'VIRTUAL' or 'STORED' keyword indicates how column values are stored, which has implications for column use:
'VIRTUAL': Column values are not stored, but are evaluated when rows are read, immediately after any 'BEFORE' triggers. A virtual column takes no storage.
'InnoDB' supports secondary indexes on virtual columns. See *note create-table-secondary-indexes::.
'STORED': Column values are evaluated and stored when rows are inserted or updated. A stored column does require storage space and can be indexed.
The default is 'VIRTUAL' if neither keyword is specified.
It is permitted to mix 'VIRTUAL' and 'STORED' columns within a table.
Other attributes may be given to indicate whether the column is indexed or can be 'NULL', or provide a comment.
Generated column expressions must adhere to the following rules. An error occurs if an expression contains disallowed constructs.
Literals, deterministic built-in functions, and operators are permitted. A function is deterministic if, given the same data in tables, multiple invocations produce the same result, independently of the connected user. Examples of functions that are nondeterministic and fail this definition: 'CONNECTION_ID()', 'CURRENT_USER()', 'NOW()'.
Stored functions and loadable functions are not permitted.
Stored procedure and function parameters are not permitted.
Variables (system variables, user-defined variables, and stored program local variables) are not permitted.
Subqueries are not permitted.
A generated column definition can refer to other generated columns, but only those occurring earlier in the table definition. A generated column definition can refer to any base (nongenerated) column in the table whether its definition occurs earlier or later.
The 'AUTO_INCREMENT' attribute cannot be used in a generated column definition.
An 'AUTO_INCREMENT' column cannot be used as a base column in a generated column definition.
As of MySQL 5.7.10, if expression evaluation causes truncation or provides incorrect input to a function, the *note 'CREATE TABLE': create-table. statement terminates with an error and the DDL operation is rejected.
If the expression evaluates to a data type that differs from the declared column type, implicit coercion to the declared type occurs according to the usual MySQL type-conversion rules. See *note type-conversion::.
Note:
If any component of the expression depends on the SQL mode, different results may occur for different uses of the table unless the SQL mode is the same during all uses.
For *note 'CREATE TABLE ... LIKE': create-table-like, the destination table preserves generated column information from the original table.
For note 'CREATE TABLE ... SELECT': create-table-select, the destination table does not preserve information about whether columns in the selected-from table are generated columns. The note 'SELECT': select. part of the statement cannot assign values to generated columns in the destination table.
Partitioning by generated columns is permitted. See *note create-table-partitioning::.
A foreign key constraint on a stored generated column cannot use 'CASCADE', 'SET NULL', or 'SET DEFAULT' as 'ON UPDATE' referential actions, nor can it use 'SET NULL' or 'SET DEFAULT' as 'ON DELETE' referential actions.
A foreign key constraint on the base column of a stored generated column cannot use 'CASCADE', 'SET NULL', or 'SET DEFAULT' as 'ON UPDATE' or 'ON DELETE' referential actions.
A foreign key constraint cannot reference a virtual generated column.
Triggers cannot use 'NEW.COL_NAME' or use 'OLD.COL_NAME' to refer to generated columns.
For note 'INSERT': insert, note 'REPLACE': replace, and *note 'UPDATE': update, if a generated column is inserted into, replaced, or updated explicitly, the only permitted value is 'DEFAULT'.
A generated column in a view is considered updatable because it is possible to assign to it. However, if such a column is updated explicitly, the only permitted value is 'DEFAULT'.
Generated columns have several use cases, such as these:
Virtual generated columns can be used as a way to simplify and unify queries. A complicated condition can be defined as a generated column and referred to from multiple queries on the table to ensure that all of them use exactly the same condition.
Stored generated columns can be used as a materialized cache for complicated conditions that are costly to calculate on the fly.
Generated columns can simulate functional indexes: Use a generated column to define a functional expression and index it. This can be useful for working with columns of types that cannot be indexed directly, such as note 'JSON': json. columns; see note json-column-indirect-index::, for a detailed example.
For stored generated columns, the disadvantage of this approach is that values are stored twice; once as the value of the generated column and once in the index.
If a generated column is indexed, the optimizer recognizes query expressions that match the column definition and uses indexes from the column as appropriate during query execution, even if a query does not refer to the column directly by name. For details, see *note generated-column-index-optimizations::.
Example:
Suppose that a table 't1' contains 'first_name' and 'last_name' columns and that applications frequently construct the full name using an expression like this:
SELECT CONCAT(first_name,' ',last_name) AS full_name FROM t1;
One way to avoid writing out the expression is to create a view 'v1' on 't1', which simplifies applications by enabling them to select 'full_name' directly without using an expression:
CREATE VIEW v1 AS
SELECT *, CONCAT(first_name,' ',last_name) AS full_name FROM t1;
SELECT full_name FROM v1;
A generated column also enables applications to select 'full_name' directly without the need to define a view:
CREATE TABLE t1 (
first_name VARCHAR(10),
last_name VARCHAR(10),
full_name VARCHAR(255) AS (CONCAT(first_name,' ',last_name))
);
SELECT full_name FROM t1;
File: manual.info.tmp, Node: create-table-secondary-indexes, Next: create-table-ndb-comment-options, Prev: create-table-generated-columns, Up: create-table
13.1.18.8 Secondary Indexes and Generated Columns .................................................
'InnoDB' supports secondary indexes on virtual generated columns. Other index types are not supported. A secondary index defined on a virtual column is sometimes referred to as a 'virtual index'.
A secondary index may be created on one or more virtual columns or on a combination of virtual columns and regular columns or stored generated columns. Secondary indexes that include virtual columns may be defined as 'UNIQUE'.
When a secondary index is created on a virtual generated column, generated column values are materialized in the records of the index. If the index is a covering index (one that includes all the columns retrieved by a query), generated column values are retrieved from materialized values in the index structure instead of computed 'on the fly'.
There are additional write costs to consider when using a secondary index on a virtual column due to computation performed when materializing virtual column values in secondary index records during note 'INSERT': insert. and note 'UPDATE': update. operations. Even with additional write costs, secondary indexes on virtual columns may be preferable to generated stored columns, which are materialized in the clustered index, resulting in larger tables that require more disk space and memory. If a secondary index is not defined on a virtual column, there are additional costs for reads, as virtual column values must be computed each time the column's row is examined.
Values of an indexed virtual column are MVCC-logged to avoid unnecessary recomputation of generated column values during rollback or during a purge operation. The data length of logged values is limited by the index key limit of 767 bytes for 'COMPACT' and 'REDUNDANT' row formats, and 3072 bytes for 'DYNAMIC' and 'COMPRESSED' row formats.
Adding or dropping a secondary index on a virtual column is an in-place operation.
Prior to 5.7.16, a foreign key constraint cannot reference a secondary index defined on a virtual generated column.
In MySQL 5.7.13 and earlier, 'InnoDB' does not permit defining a foreign key constraint with a cascading referential action on the base column of an indexed generated virtual column. This restriction is lifted in MySQL 5.7.14.
Indexing a Generated Column to Provide a JSON Column Index
As noted elsewhere, *note 'JSON': json. columns cannot be indexed directly. To create an index that references such a column indirectly, you can define a generated column that extracts the information that should be indexed, then create an index on the generated column, as shown in this example:
mysql> CREATE TABLE jemp (
-> c JSON,
-> g INT GENERATED ALWAYS AS (c->"$.id"),
-> INDEX i (g)
-> );
Query OK, 0 rows affected (0.28 sec)
mysql> INSERT INTO jemp (c) VALUES
> ('{"id": "1", "name": "Fred"}'), ('{"id": "2", "name": "Wilma"}'),
> ('{"id": "3", "name": "Barney"}'), ('{"id": "4", "name": "Betty"}');
Query OK, 4 rows affected (0.04 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> SELECT c->>"$.name" AS name
> FROM jemp WHERE g > 2;
+--------+
| name |
+--------+
| Barney |
| Betty |
+--------+
2 rows in set (0.00 sec)
mysql> EXPLAIN SELECT c->>"$.name" AS name
> FROM jemp WHERE g > 2\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: jemp
partitions: NULL
type: range
possible_keys: i
key: i
key_len: 5
ref: NULL
rows: 2
filtered: 100.00
Extra: Using where
1 row in set, 1 warning (0.00 sec)
mysql> SHOW WARNINGS\G
*************************** 1. row ***************************
Level: Note
Code: 1003
Message: /* select#1 */ select json_unquote(json_extract(`test`.`jemp`.`c`,'$.name'))
AS `name` from `test`.`jemp` where (`test`.`jemp`.`g` > 2)
1 row in set (0.00 sec)
(We have wrapped the output from the last statement in this example to fit the viewing area.)
The '->' operator is supported in MySQL 5.7.9 and later. The '->>' operator is supported beginning with MySQL 5.7.13.
When you use note 'EXPLAIN': explain. on a note 'SELECT': select. or other SQL statement containing one or more expressions that use the '->' or '->>' operator, these expressions are translated into their equivalents using 'JSON_EXTRACT()' and (if needed) 'JSON_UNQUOTE()' instead, as shown here in the output from *note 'SHOW WARNINGS': show-warnings. immediately following this 'EXPLAIN' statement:
mysql> EXPLAIN SELECT c->>"$.name"
> FROM jemp WHERE g > 2 ORDER BY c->"$.name"\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: jemp
partitions: NULL
type: range
possible_keys: i
key: i
key_len: 5
ref: NULL
rows: 2
filtered: 100.00
Extra: Using where; Using filesort
1 row in set, 1 warning (0.00 sec)
mysql> SHOW WARNINGS\G
*************************** 1. row ***************************
Level: Note
Code: 1003
Message: /* select#1 */ select json_unquote(json_extract(`test`.`jemp`.`c`,'$.name')) AS
`c->>"$.name"` from `test`.`jemp` where (`test`.`jemp`.`g` > 2) order by
json_extract(`test`.`jemp`.`c`,'$.name')
1 row in set (0.00 sec)
See the descriptions of the '->' and '->>' operators, as well as those of the 'JSON_EXTRACT()' and 'JSON_UNQUOTE()' functions, for additional information and examples.
This technique also can be used to provide indexes that indirectly reference columns of other types that cannot be indexed directly, such as 'GEOMETRY' columns.
JSON columns and indirect indexing in NDB Cluster
It is also possible to use indirect indexing of JSON columns in MySQL NDB Cluster, subject to the following conditions:
note 'NDB': mysql-cluster. handles a note 'JSON': json. column value internally as a *note 'BLOB': blob. This means that any 'NDB' table having one or more JSON columns must have a primary key, else it cannot be recorded in the binary log.
The *note 'NDB': mysql-cluster. storage engine does not support indexing of virtual columns. Since the default for generated columns is 'VIRTUAL', you must specify explicitly the generated column to which to apply the indirect index as 'STORED'.
The CREATE TABLE statement used to create the table 'jempn' shown here is a version of the 'jemp' table shown previously, with modifications making it compatible with 'NDB':
CREATE TABLE jempn (
a BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
c JSON DEFAULT NULL,
g INT GENERATED ALWAYS AS (c->"$.name") STORED,
INDEX i (g)
) ENGINE=NDB;
We can populate this table using the following *note 'INSERT': insert. statement:
INSERT INTO jempn (a, c) VALUES
(NULL, '{"id": "1", "name": "Fred"}'),
(NULL, '{"id": "2", "name": "Wilma"}'),
(NULL, '{"id": "3", "name": "Barney"}'),
(NULL, '{"id": "4", "name": "Betty"}');
Now 'NDB' can use index 'i', as shown here:
mysql> EXPLAIN SELECT c->>"$.name" AS name
FROM jempn WHERE g > 2\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: jempn
partitions: p0,p1
type: range
possible_keys: i
key: i
key_len: 5
ref: NULL
rows: 3
filtered: 100.00
Extra: Using where with pushed condition (`test`.`jempn`.`g` > 2)
1 row in set, 1 warning (0.00 sec)
mysql> SHOW WARNINGS\G
*************************** 1. row ***************************
Level: Note
Code: 1003
Message: /* select#1 */ select
json_unquote(json_extract(`test`.`jempn`.`c`,'$.name')) AS `name` from
`test`.`jempn` where (`test`.`jempn`.`g` > 2)
1 row in set (0.00 sec)
You should keep in mind that a stored generated column, as well as any index on such a column, uses 'DataMemory'. In NDB 7.5, an index on a stored generated column also uses 'IndexMemory'.
File: manual.info.tmp, Node: create-table-ndb-comment-options, Prev: create-table-secondary-indexes, Up: create-table
13.1.18.9 Setting NDB Comment Options .....................................
*note create-table-ndb-comment-column-options::
*note create-table-ndb-comment-table-options::
It is possible to set a number of options specific to NDB Cluster in the table comment or column comments of an *note 'NDB': mysql-cluster. table. Table-level options for controlling read from any replica and partition balance can be embedded in a table comment using 'NDB_TABLE'.
'NDB_COLUMN' can be used in a column comment to set the size of the blob parts table column used for storing parts of blob values by 'NDB' to its maximum. This works for note 'BLOB': blob, 'MEDIUMBLOB', 'LONGBLOB', note 'TEXT': blob, 'MEDIUMTEXT', 'LONGTEXT', and *note 'JSON': json. columns.
'NDB_TABLE' can be used in a table comment to set options relating to partition balance and whether the table is fully replicated, among others.
The remainder of this section describes these options and their use.
NDB_COLUMN Options
In NDB Cluster, a column comment in a 'CREATE TABLE' or *note 'ALTER TABLE': alter-table. statement can also be used to specify an 'NDB_COLUMN' option. NDB 7.5 and 7.6 support a single column comment option 'MAX_BLOB_PART_SIZE'; syntax for this option is shown here:
COMMENT 'NDB_COLUMN=MAX_BLOB_PART_SIZE[={0|1}]'
The '=' sign and the value following it are optional. Using any value other than 0 or 1 results in a syntax error.
The effect of using 'MAX_BLOB_PART_SIZE' in a column comment is to set the blob part size of a note 'TEXT': blob. or note 'BLOB': blob. column to the maximum number of bytes supported for this by 'NDB' (13948). This option can be applied to any blob column type supported by MySQL except 'TINYBLOB' or 'TINYTEXT' ('BLOB', 'MEDIUMBLOB', 'LONGBLOB', 'TEXT', 'MEDIUMTEXT', 'LONGTEXT'). 'MAX_BLOB_PART_SIZE' has no effect on 'JSON' columns.
You should also keep in mind, especially when working with 'TEXT' columns, that the value set by 'MAX_BLOB_PART_SIZE' represents column size in bytes. It does not indicate the number of characters, which varies according to the character set and collation used by the column.
To see the effects of this option, we first run the following SQL statement in the *note 'mysql': mysql. client to create a table with two 'BLOB' columns, one ('c1') with no extra options, and another ('c2') with 'MAX_BLOB_PART_SIZE':
mysql> CREATE TABLE test.t (
-> p INT PRIMARY KEY,
-> c1 BLOB,
-> c2 BLOB COMMENT 'NDB_COLUMN=MAX_BLOB_PART_SIZE'
-> ) ENGINE NDB;
Query OK, 0 rows affected (0.32 sec)
From the system shell, run the *note 'ndb_desc': mysql-cluster-programs-ndb-desc. utility to obtain information about the table just created, as shown in this example:
$> ndb_desc -d test t
-- t --
Version: 1
Fragment type: HashMapPartition
K Value: 6
Min load factor: 78
Max load factor: 80
Temporary table: no
Number of attributes: 3
Number of primary keys: 1
Length of frm data: 324
Row Checksum: 1
Row GCI: 1
SingleUserMode: 0
ForceVarPart: 1
FragmentCount: 2
ExtraRowGciBits: 0
ExtraRowAuthorBits: 0
TableStatus: Retrieved
HashMap: DEFAULT-HASHMAP-3840-2
-- Attributes --
p Int PRIMARY KEY DISTRIBUTION KEY AT=FIXED ST=MEMORY
_c1 Blob(256,2000,0) NULL AT=MEDIUM_VAR ST=MEMORY BV=2 BT=NDB$BLOB_22_1
c2 Blob(256,13948,0) NULL AT=MEDIUM_VAR ST=MEMORY BV=2 BT=NDB$BLOB_22_2_
-- Indexes --
PRIMARY KEY(p) - UniqueHashIndex
PRIMARY(p) - OrderedIndex
Column information in the output is listed under 'Attributes'; for columns 'c1' and 'c2' it is displayed here in emphasized text. For 'c1', the blob part size is 2000, the default value; for 'c2', it is 13948, as set by 'MAX_BLOB_PART_SIZE'.
You can change the blob part size for a given blob column of an 'NDB' table using an 'ALTER TABLE' statement such as this one, and verifying the changes afterwards using *note 'SHOW CREATE TABLE': show-create-table.:
mysql> ALTER TABLE test.t
-> DROP COLUMN c1,
-> ADD COLUMN c1 BLOB COMMENT 'NDB_COLUMN=MAX_BLOB_PART_SIZE',
-> CHANGE COLUMN c2 c2 BLOB AFTER c1;
Query OK, 0 rows affected (0.47 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW CREATE TABLE test.t\G
*************************** 1. row ***************************
Table: t
Create Table: CREATE TABLE `t` (
`p` int(11) NOT NULL,
`c1` blob COMMENT 'NDB_COLUMN=MAX_BLOB_PART_SIZE',
`c2` blob,
PRIMARY KEY (`p`)
) ENGINE=ndbcluster DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> EXIT
Bye
The output of *note 'ndb_desc': mysql-cluster-programs-ndb-desc. shows that the blob part sizes of the columns have been changed as expected:
$> ndb_desc -d test t
-- t --
Version: 16777220
Fragment type: HashMapPartition
K Value: 6
Min load factor: 78
Max load factor: 80
Temporary table: no
Number of attributes: 3
Number of primary keys: 1
Length of frm data: 324
Row Checksum: 1
Row GCI: 1
SingleUserMode: 0
ForceVarPart: 1
FragmentCount: 2
ExtraRowGciBits: 0
ExtraRowAuthorBits: 0
TableStatus: Retrieved
HashMap: DEFAULT-HASHMAP-3840-2
-- Attributes --
p Int PRIMARY KEY DISTRIBUTION KEY AT=FIXED ST=MEMORY
_c1 Blob(256,13948,0) NULL AT=MEDIUM_VAR ST=MEMORY BV=2 BT=NDB$BLOB_26_1
c2 Blob(256,2000,0) NULL AT=MEDIUM_VAR ST=MEMORY BV=2 BT=NDB$BLOB_26_2_
-- Indexes --
PRIMARY KEY(p) - UniqueHashIndex
PRIMARY(p) - OrderedIndex
NDBT_ProgramExit: 0 - OK
Changing a column's blob part size must be done using a copying 'ALTER TABLE'; this operation cannot be performed online (see *note mysql-cluster-online-operations::).
For more information about how note 'NDB': mysql-cluster. stores columns of blob types, see note data-types-storage-reqs-strings::.
NDB_TABLE Options
For an NDB Cluster table, the table comment in a 'CREATE TABLE' or *note 'ALTER TABLE': alter-table. statement can also be used to specify an 'NDB_TABLE' option, which consists of one or more name-value pairs, separated by commas if need be, following the string 'NDB_TABLE='. Complete syntax for names and values syntax is shown here:
COMMENT="NDB_TABLE=NDB_TABLE_OPTION[,NDB_TABLE_OPTION[,...]]"
NDB_TABLE_OPTION: {
NOLOGGING={1 | 0}
| READ_BACKUP={1 | 0}
| PARTITION_BALANCE={FOR_RP_BY_NODE | FOR_RA_BY_NODE | FOR_RP_BY_LDM
| FOR_RA_BY_LDM | FOR_RA_BY_LDM_X_2
| FOR_RA_BY_LDM_X_3 | FOR_RA_BY_LDM_X_4}
| FULLY_REPLICATED={1 | 0}
}
Spaces are not permitted within the quoted string. The string is case-insensitive.
The four 'NDB' table options that can be set as part of a comment in this way are described in more detail in the next few paragraphs.
'NOLOGGING': By default, 'NDB' tables are logged, and checkpointed. This makes them durable to whole cluster failures. Using 'NOLOGGING' when creating or altering a table means that this table is not redo logged or included in local checkpoints. In this case, the table is still replicated across the data nodes for high availability, and updated using transactions, but changes made to it are not recorded in the data node's redo logs, and its content is not checkpointed to disk; when recovering from a cluster failure, the cluster retains the table definition, but none of its rows--that is, the table is empty.
Using such nonlogging tables reduces the data node's demands on disk I/O and storage, as well as CPU for checkpointing CPU. This may be suitable for short-lived data which is frequently updated, and where the loss of all data in the unlikely event of a total cluster failure is acceptable.
It is also possible to use the 'ndb_table_no_logging' system variable to cause any NDB tables created or altered while this variable is in effect to behave as though it had been created with the 'NOLOGGING' comment. Unlike when using the comment directly, there is nothing in this case in the output of *note 'SHOW CREATE TABLE': show-create-table. to indicate that it is a nonlogging table. Using the table comment approach is recommended since it offers per-table control of the feature, and this aspect of the table schema is embedded in the table creation statement where it can be found easily by SQL-based tools.
'READ_BACKUP': Setting this option to 1 has the same effect as though 'ndb_read_backup' were enabled; enables reading from any replica. Doing so greatly improves the performance of reads from the table at a relatively small cost to write performance.
Starting with MySQL NDB Cluster 7.5.3, you can set 'READ_BACKUP' for an existing table online (Bug #80858, Bug #23001617), using an 'ALTER TABLE' statement similar to one of those shown here:
ALTER TABLE ... ALGORITHM=INPLACE, COMMENT="NDB_TABLE=READ_BACKUP=1";
ALTER TABLE ... ALGORITHM=INPLACE, COMMENT="NDB_TABLE=READ_BACKUP=0";
Prior to MySQL NDB Cluster 7.5.4, setting 'READ_BACKUP' to 1 also caused 'FRAGMENT_COUNT_TYPE' to be set to 'ONE_PER_LDM_PER_NODE_GROUP'.
For more information about the 'ALGORITHM' option for 'ALTER TABLE', see *note mysql-cluster-online-operations::.
'PARTITION_BALANCE': Provides additional control over assignment and placement of partitions. The following four schemes are supported:
'FOR_RP_BY_NODE': One partition per node.
Only one LDM on each node stores a primary partition. Each partition is stored in the same LDM (same ID) on all nodes.
'FOR_RA_BY_NODE': One partition per node group.
Each node stores a single partition, which can be either a primary replica or a backup replica. Each partition is stored in the same LDM on all nodes.
'FOR_RP_BY_LDM': One partition for each LDM on each node; the default.
This is the same behavior as prior to MySQL NDB Cluster 7.5.2, except for a slightly different mapping of partitions to LDMs, starting with LDM 0 and placing one partition per node group, then moving on to the next LDM.
In MySQL NDB Cluster 7.5.4 and later, this is the setting used if 'READ_BACKUP' is set to 1. (Bug #82634, Bug #24482114)
'FOR_RA_BY_LDM': One partition per LDM in each node group.
These partitions can be primary or backup partitions.
Prior to MySQL NDB Cluster 7.5.4, this was the setting used if 'READ_BACKUP' was set to 1.
'FOR_RA_BY_LDM_X_2': Two partitions per LDM in each node group.
These partitions can be primary or backup partitions.
This setting was added in NDB 7.5.4.
'FOR_RA_BY_LDM_X_3': Three partitions per LDM in each node group.
These partitions can be primary or backup partitions.
This setting was added in NDB 7.5.4.
'FOR_RA_BY_LDM_X_4': Four partitions per LDM in each node group.
These partitions can be primary or backup partitions.
This setting was added in NDB 7.5.4.
Beginning with NDB 7.5.4, 'PARTITION_BALANCE' is the preferred interface for setting the number of partitions per table. Using 'MAX_ROWS' to force the number of partitions is deprecated as of NDB 7.5.4, continues to be supported in NDB 7.6 for backward compatibility, but is subject to removal in a future release of MySQL NDB Cluster. (Bug #81759, Bug #23544301)
Prior to MySQL NDB Cluster 7.5.4, 'PARTITION_BALANCE' was named 'FRAGMENT_COUNT_TYPE', and accepted as its value one of (in the same order as that of the listing just shown) 'ONE_PER_NODE', 'ONE_PER_NODE_GROUP', 'ONE_PER_LDM_PER_NODE', or 'ONE_PER_LDM_PER_NODE_GROUP'. (Bug #81761, Bug #23547525)
'FULLY_REPLICATED' controls whether the table is fully replicated, that is, whether each data node has a complete copy of the table. To enable full replication of the table, use 'FULLY_REPLICATED=1'.
This setting can also be controlled using the 'ndb_fully_replicated' system variable. Setting it to 'ON' enables the option by default for all new 'NDB' tables; the default is 'OFF', which maintains the previous behavior (as in MySQL NDB Cluster 7.5.1 and earlier, before support for fully replicated tables was introduced). The 'ndb_data_node_neighbour' system variable is also used for fully replicated tables, to ensure that when a fully replicated table is accessed, we access the data node which is local to this MySQL Server.
An example of a 'CREATE TABLE' statement using such a comment when creating an 'NDB' table is shown here:
mysql> CREATE TABLE t1 (
> c1 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
> c2 VARCHAR(100),
> c3 VARCHAR(100) )
> ENGINE=NDB
>
COMMENT="NDB_TABLE=READ_BACKUP=0,PARTITION_BALANCE=FOR_RP_BY_NODE";
The comment is displayed as part of the ouput of note 'SHOW CREATE TABLE': show-create-table. The text of the comment is also available from querying the MySQL Information Schema note 'TABLES': information-schema-tables-table. table, as in this example:
mysql> SELECT TABLE_NAME, TABLE_SCHEMA, TABLE_COMMENT
> FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME="t1"\G
*************************** 1. row ***************************
TABLE_NAME: t1
TABLE_SCHEMA: test
TABLE_COMMENT: NDB_TABLE=READ_BACKUP=0,PARTITION_BALANCE=FOR_RP_BY_NODE
1 row in set (0.01 sec)
This comment syntax is also supported with *note 'ALTER TABLE': alter-table. statements for 'NDB' tables, as shown here:
mysql> ALTER TABLE t1 COMMENT="NDB_TABLE=PARTITION_BALANCE=FOR_RA_BY_NODE";
Query OK, 0 rows affected (0.40 sec)
Records: 0 Duplicates: 0 Warnings: 0
Beginning with NDB 7.6.15, the 'TABLE_COMMENT' column displays the comment that is required to re-create the table as it is following the 'ALTER TABLE' statement, like this:
mysql> SELECT TABLE_NAME, TABLE_SCHEMA, TABLE_COMMENT
-> FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME="t1"\G
*************************** 1. row ***************************
TABLE_NAME: t1
TABLE_SCHEMA: test
TABLE_COMMENT: NDB_TABLE=READ_BACKUP=0,PARTITION_BALANCE=FOR_RP_BY_NODE
1 row in set (0.01 sec)
mysql> SELECT TABLE_NAME, TABLE_SCHEMA, TABLE_COMMENT
> FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME="t1";
+------------+--------------+--------------------------------------------------+
| TABLE_NAME | TABLE_SCHEMA | TABLE_COMMENT |
+------------+--------------+--------------------------------------------------+
| t1 | c | NDB_TABLE=PARTITION_BALANCE=FOR_RA_BY_NODE |
| t1 | d | |
+------------+--------------+--------------------------------------------------+
2 rows in set (0.01 sec)
Keep in mind that a table comment used with 'ALTER TABLE' replaces any existing comment which the table might have.
mysql> ALTER TABLE t1 COMMENT="NDB_TABLE=PARTITION_BALANCE=FOR_RA_BY_NODE";
Query OK, 0 rows affected (0.40 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SELECT TABLE_NAME, TABLE_SCHEMA, TABLE_COMMENT
> FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME="t1";
+------------+--------------+--------------------------------------------------+
| TABLE_NAME | TABLE_SCHEMA | TABLE_COMMENT |
+------------+--------------+--------------------------------------------------+
| t1 | c | NDB_TABLE=PARTITION_BALANCE=FOR_RA_BY_NODE |
| t1 | d | |
+------------+--------------+--------------------------------------------------+
2 rows in set (0.01 sec)
Prior to NDB 7.6.15, the table comment used with 'ALTER TABLE' replaced any existing comment which the table might have had. This meant that (for example) the 'READ_BACKUP' value was not carried over to the new comment set by the 'ALTER TABLE' statement, and that any unspecified values reverted to their defaults. (BUG#30428829) There was thus no longer any way using SQL to retrieve the value previously set for the comment. To keep comment values from reverting to their defaults, it was necessry to preserve any such values from the existing comment string and include them in the comment passed to 'ALTER TABLE'.
You can also see the value of the 'PARTITION_BALANCE' option in the output of note 'ndb_desc': mysql-cluster-programs-ndb-desc. note 'ndb_desc': mysql-cluster-programs-ndb-desc. also shows whether the 'READ_BACKUP' and 'FULLY_REPLICATED' options are set for the table. See the description of this program for more information.
File: manual.info.tmp, Node: create-tablespace, Next: create-trigger, Prev: create-table, Up: sql-data-definition-statements
CREATE TABLESPACE TABLESPACE_NAME
_InnoDB and NDB:_
ADD DATAFILE 'FILE_NAME'
_InnoDB only:_
[FILE_BLOCK_SIZE = value]
_NDB only:_
USE LOGFILE GROUP LOGFILE_GROUP
[EXTENT_SIZE [=] EXTENT_SIZE]
[INITIAL_SIZE [=] INITIAL_SIZE]
[AUTOEXTEND_SIZE [=] AUTOEXTEND_SIZE]
[MAX_SIZE [=] MAX_SIZE]
[NODEGROUP [=] NODEGROUP_ID]
[WAIT]
[COMMENT [=] 'STRING']
_InnoDB and NDB:_
[ENGINE [=] ENGINE_NAME]
This statement is used to create a tablespace. The precise syntax and semantics depend on the storage engine used. In standard MySQL 5.7 releases, this is always an note 'InnoDB': innodb-storage-engine. tablespace. MySQL NDB Cluster 7.5 also supports tablespaces using the note 'NDB': mysql-cluster. storage engine in addition to those using 'InnoDB'.
*note create-tablespace-innodb::
*note create-tablespace-ndb::
*note create-tablespace-options::
*note create-tablespace-notes::
*note create-tablespace-innodb-examples::
*note create-tablespace-ndb-examples::
Considerations for InnoDB
*note 'CREATE TABLESPACE': create-tablespace. syntax is used to create general tablespaces. A general tablespace is a shared tablespace. It can hold multiple tables, and supports all table row formats. General tablespaces can be created in a location relative to or independent of the data directory.
After creating an 'InnoDB' general tablespace, you can use note 'CREATE TABLE TBL_NAME ... TABLESPACE [=] TABLESPACE_NAME': create-table. or note 'ALTER TABLE TBL_NAME TABLESPACE [=] TABLESPACE_NAME': alter-table. to add tables to the tablespace. For more information, see *note general-tablespaces::.
Considerations for NDB Cluster
This statement is used to create a tablespace, which can contain one or more data files, providing storage space for NDB Cluster Disk Data tables (see note mysql-cluster-disk-data::). One data file is created and added to the tablespace using this statement. Additional data files may be added to the tablespace by using the note 'ALTER TABLESPACE': alter-tablespace. statement (see *note alter-tablespace::).
Note:
All NDB Cluster Disk Data objects share the same namespace. This means that each Disk Data object must be uniquely named (and not merely each Disk Data object of a given type). For example, you cannot have a tablespace and a log file group with the same name, or a tablespace and a data file with the same name.
A log file group of one or more 'UNDO' log files must be assigned to the tablespace to be created with the 'USE LOGFILE GROUP' clause. LOGFILE_GROUP must be an existing log file group created with note 'CREATE LOGFILE GROUP': create-logfile-group. (see note create-logfile-group::). Multiple tablespaces may use the same log file group for 'UNDO' logging.
When setting 'EXTENT_SIZE' or 'INITIAL_SIZE', you may optionally follow the number with a one-letter abbreviation for an order of magnitude, similar to those used in 'my.cnf'. Generally, this is one of the letters 'M' (for megabytes) or 'G' (for gigabytes).
'INITIAL_SIZE' and 'EXTENT_SIZE' are subject to rounding as follows:
'EXTENT_SIZE' is rounded up to the nearest whole multiple of 32K.
'INITIAL_SIZE' is rounded down to the nearest whole multiple of 32K; this result is rounded up to the nearest whole multiple of 'EXTENT_SIZE' (after any rounding).
Note:
*note 'NDB': mysql-cluster. reserves 4% of a tablespace for data node restart operations. This reserved space cannot be used for data storage. This restriction applies beginning with NDB 7.6.
The rounding just described is done explicitly, and a warning is issued by the MySQL Server when any such rounding is performed. The rounded values are also used by the NDB kernel for calculating Information Schema *note 'FILES': information-schema-files-table. column values and other purposes. However, to avoid an unexpected result, we suggest that you always use whole multiples of 32K in specifying these options.
When note 'CREATE TABLESPACE': create-tablespace. is used with 'ENGINE [=] NDB', a tablespace and associated data file are created on each Cluster data node. You can verify that the data files were created and obtain information about them by querying the Information Schema note 'FILES': information-schema-files-table. table. (See the example later in this section.)
(See *note information-schema-files-table::.)
Options
'ADD DATAFILE': Defines the name of a tablespace data file; this option is always required. The 'FILE_NAME', including any specified path, must be quoted with single or double quotation marks. File names (not counting the file extension) and directory names must be at least one byte in length. Zero length file names and directory names are not supported.
Because there are considerable differences in how 'InnoDB' and 'NDB' treat data files, the two storage engines are covered separately in the discussion that follows.
InnoDB data files
An 'InnoDB' tablespace supports only a single data file, whose name must include a '.ibd' extension.
For an 'InnoDB' tablespace, the data file is created by default in the MySQL data directory ('datadir'). To place the data file in a location other than the default, include an absolute directory path or a path relative to the default location.
When an 'InnoDB' tablespace is created outside of the data directory, an isl file is created in the data directory. To avoid conflicts with implicitly created file-per-table tablespaces, creating an 'InnoDB' general tablespace in a subdirectory under the data directory is not supported. When creating an 'InnoDB' general tablespace outside of the data directory, the directory must exist prior to creating the tablespace.
Note:
In MySQL 5.7, 'ALTER TABLESPACE' is not supported by 'InnoDB'.
NDB data files
An 'NDB' tablespace supports multiple data files which can have any legal file names; more data files can be added to an NDB Cluster tablespace following its creation by using an *note 'ALTER TABLESPACE': alter-tablespace. statement.
An 'NDB' tablespace data file is created by default in the data node file system directory--that is, the directory named 'ndb_NODEID_fs/TS' under the data node's data directory ('DataDir'), where NODEID is the data node's 'NodeId'. To place the data file in a location other than the default, include an absolute directory path or a path relative to the default location. If the directory specified does not exist, 'NDB' attempts to create it; the system user account under which the data node process is running must have the appropriate permissions to do so.
Note:
When determining the path used for a data file, 'NDB' does not expand the '~' (tilde) character.
When multiple data nodes are run on the same physical host, the following considerations apply:
* You cannot specify an absolute path when creating a data file.
* It is not possible to create tablespace data files outside the
data node file system directory, unless each data node has a
separate data directory.
* If each data node has its own data directory, data files can
be created anywhere within this directory.
* If each data node has its own data directory, it may also be
possible to create a data file outside the node's data
directory using a relative path, as long as this path resolves
to a unique location on the host file system for each data
node running on that host.
'FILE_BLOCK_SIZE': This option--which is specific to 'InnoDB', and is ignored by 'NDB'--defines the block size for the tablespace data file. Values can be specified in bytes or kilobytes. For example, an 8 kilobyte file block size can be specified as 8192 or 8K. If you do not specify this option, 'FILE_BLOCK_SIZE' defaults to the 'innodb_page_size' value. 'FILE_BLOCK_SIZE' is required when you intend to use the tablespace for storing compressed 'InnoDB' tables ('ROW_FORMAT=COMPRESSED'). In this case, you must define the tablespace 'FILE_BLOCK_SIZE' when creating the tablespace.
If 'FILE_BLOCK_SIZE' is equal the 'innodb_page_size' value, the tablespace can contain only tables having an uncompressed row format ('COMPACT', 'REDUNDANT', and 'DYNAMIC'). Tables with a 'COMPRESSED' row format have a different physical page size than uncompressed tables. Therefore, compressed tables cannot coexist in the same tablespace as uncompressed tables.
For a general tablespace to contain compressed tables, 'FILE_BLOCK_SIZE' must be specified, and the 'FILE_BLOCK_SIZE' value must be a valid compressed page size in relation to the 'innodb_page_size' value. Also, the physical page size of the compressed table ('KEY_BLOCK_SIZE') must be equal to 'FILE_BLOCK_SIZE/1024'. For example, if 'innodb_page_size=16K', and 'FILE_BLOCK_SIZE=8K', the 'KEY_BLOCK_SIZE' of the table must be'USE LOGFILE GROUP': Required for 'NDB', this is the name of a log file group previously created using *note 'CREATE LOGFILE GROUP': create-logfile-group. Not supported for 'InnoDB', where it fails with an error.
'EXTENT_SIZE': This option is specific to NDB, and is not supported by InnoDB, where it fails with an error. 'EXTENT_SIZE' sets the size, in bytes, of the extents used by any files belonging to the tablespace. The default value is 1M. The minimum size is 32K, and theoretical maximum is 2G, although the practical maximum size depends on a number of factors. In most cases, changing the extent size does not have any measurable effect on performance, and the default value is recommended for all but the most unusual situations.
An extent is a unit of disk space allocation. One extent is filled with as much data as that extent can contain before another extent is used. In theory, up to 65,535 (64K) extents may used per data file; however, the recommended maximum is 32,768 (32K). The recommended maximum size for a single data file is 32G--that is, 32K extents x 1 MB per extent. In addition, once an extent is allocated to a given partition, it cannot be used to store data from a different partition; an extent cannot store data from more than one partition. This means, for example that a tablespace having a single datafile whose 'INITIAL_SIZE' (described in the following item) is 256 MB and whose 'EXTENT_SIZE' is 128M has just two extents, and so can be used to store data from at most two different disk data table partitions.
You can see how many extents remain free in a given data file by querying the Information Schema note 'FILES': information-schema-files-table. table, and so derive an estimate for how much space remains free in the file. For further discussion and examples, see note information-schema-files-table::.
'INITIAL_SIZE': This option is specific to 'NDB', and is not supported by 'InnoDB', where it fails with an error.
The 'INITIAL_SIZE' parameter sets the total size in bytes of the data file that was specific using 'ADD DATATFILE'. Once this file has been created, its size cannot be changed; however, you can add more data files to the tablespace using *note 'ALTER TABLESPACE ... ADD DATAFILE': alter-tablespace.
'INITIAL_SIZE' is optional; its default value is 134217728 (128 MB).
On 32-bit systems, the maximum supported value for 'INITIAL_SIZE' is 4294967296 (4 GB).
'AUTOEXTEND_SIZE': Currently ignored by MySQL; reserved for possible future use. Has no effect in any release of MySQL 5.7 or MySQL NDB Cluster 7.5, regardless of the storage engine used.
'MAX_SIZE': Currently ignored by MySQL; reserved for possible future use. Has no effect in any release of MySQL 5.7 or MySQL NDB Cluster 7.5, regardless of the storage engine used.
'NODEGROUP': Currently ignored by MySQL; reserved for possible future use. Has no effect in any release of MySQL 5.7 or MySQL NDB Cluster 7.5, regardless of the storage engine used.
'WAIT': Currently ignored by MySQL; reserved for possible future use. Has no effect in any release of MySQL 5.7 or MySQL NDB Cluster 7.5, regardless of the storage engine used.
'COMMENT': Currently ignored by MySQL; reserved for possible future use. Has no effect in any release of MySQL 5.7 or MySQL NDB Cluster 7.5, regardless of the storage engine used.
'ENGINE': Defines the storage engine which uses the tablespace, where ENGINE_NAME is the name of the storage engine. Currently, only the 'InnoDB' storage engine is supported by standard MySQL 5.7 releases. MySQL NDB Cluster 7.5 supports both 'NDB' and 'InnoDB' tablespaces. The value of the 'default_storage_engine' system variable is used for 'ENGINE' if the option is not specified.
Notes
For the rules covering the naming of MySQL tablespaces, see *note identifiers::. In addition to these rules, the slash character ('/') is not permitted, nor can you use names beginning with 'innodb_', as this prefix is reserved for system use.
Tablespaces do not support temporary tables.
'innodb_file_per_table', 'innodb_file_format', and 'innodb_file_format_max' settings have no influence on 'CREATE TABLESPACE' operations. 'innodb_file_per_table' does not need to be enabled. General tablespaces support all table row formats regardless of file format settings. Likewise, general tablespaces support the addition of tables of any row format using *note 'CREATE TABLE ... TABLESPACE': create-table, regardless of file format settings.
'innodb_strict_mode' is not applicable to general tablespaces. Tablespace management rules are strictly enforced independently of 'innodb_strict_mode'. If 'CREATE TABLESPACE' parameters are incorrect or incompatible, the operation fails regardless of the 'innodb_strict_mode' setting. When a table is added to a general tablespace using note 'CREATE TABLE ... TABLESPACE': create-table. or note 'ALTER TABLE ... TABLESPACE': alter-table, 'innodb_strict_mode' is ignored but the statement is evaluated as if 'innodb_strict_mode' is enabled.
Use 'DROP TABLESPACE' to remove a tablespace. All tables must be dropped from a tablespace using note 'DROP TABLE': drop-table. prior to dropping the tablespace. Before dropping an NDB Cluster tablespace you must also remove all its data files using one or more note 'ALTER TABLESPACE ... DROP DATATFILE': alter-tablespace. statements. See *note mysql-cluster-disk-data-objects::.
All parts of an 'InnoDB' table added to an 'InnoDB' general tablespace reside in the general tablespace, including indexes and *note 'BLOB': blob. pages.
For an 'NDB' table assigned to a tablespace, only those columns which are not indexed are stored on disk, and actually use the tablespace data files. Indexes and indexed columns for all 'NDB' tables are always kept in memory.
Similar to the system tablespace, truncating or dropping tables stored in a general tablespace creates free space internally in the general tablespace .ibd data file which can only be used for new 'InnoDB' data. Space is not released back to the operating system as it is for file-per-table tablespaces.
A general tablespace is not associated with any database or schema.
note 'ALTER TABLE ... DISCARD TABLESPACE': alter-table. and note 'ALTER TABLE ...IMPORT TABLESPACE': alter-table. are not supported for tables that belong to a general tablespace.
The server uses tablespace-level metadata locking for DDL that references general tablespaces. By comparison, the server uses table-level metadata locking for DDL that references file-per-table tablespaces.
A generated or existing tablespace cannot be changed to a general tablespace.
Tables stored in a general tablespace can only be opened in MySQL 5.7.6 or later due to the addition of new table flags.
There is no conflict between general tablespace names and file-per-table tablespace names. The '/' character, which is present in file-per-table tablespace names, is not permitted in general tablespace names.
note 'mysqldump': mysqldump. and note 'mysqlpump': mysqlpump. do not dump 'InnoDB' *note 'CREATE TABLESPACE': create-tablespace. statements.
InnoDB Examples
This example demonstrates creating a general tablespace and adding three uncompressed tables of different row formats.
mysql> CREATE TABLESPACE `ts1` ADD DATAFILE 'ts1.ibd' ENGINE=INNODB;
mysql> CREATE TABLE t1 (c1 INT PRIMARY KEY) TABLESPACE ts1 ROW_FORMAT=REDUNDANT;
mysql> CREATE TABLE t2 (c1 INT PRIMARY KEY) TABLESPACE ts1 ROW_FORMAT=COMPACT;
mysql> CREATE TABLE t3 (c1 INT PRIMARY KEY) TABLESPACE ts1 ROW_FORMAT=DYNAMIC;
This example demonstrates creating a general tablespace and adding a compressed table. The example assumes a default 'innodb_page_size' value of 16K. The 'FILE_BLOCK_SIZE' of 8192 requires that the compressed table have a 'KEY_BLOCK_SIZE' of 8.
mysql> CREATE TABLESPACE `ts2` ADD DATAFILE 'ts2.ibd' FILE_BLOCK_SIZE = 8192 Engine=InnoDB;
mysql> CREATE TABLE t4 (c1 INT PRIMARY KEY) TABLESPACE ts2 ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8;
NDB Example
Suppose that you wish to create an NDB Cluster Disk Data tablespace named 'myts' using a datafile named 'mydata-1.dat'. An 'NDB' tablespace always requires the use of a log file group consisting of one or more undo log files. For this example, we first create a log file group named 'mylg' that contains one undo long file named 'myundo-1.dat', using the *note 'CREATE LOGFILE GROUP': create-logfile-group. statement shown here:
mysql> CREATE LOGFILE GROUP myg1
-> ADD UNDOFILE 'myundo-1.dat'
-> ENGINE=NDB;
Query OK, 0 rows affected (3.29 sec)
Now you can create the tablespace previously described using the following statement:
mysql> CREATE TABLESPACE myts
-> ADD DATAFILE 'mydata-1.dat'
-> USE LOGFILE GROUP mylg
-> ENGINE=NDB;
Query OK, 0 rows affected (2.98 sec)
You can now create a Disk Data table using a *note 'CREATE TABLE': create-table. statement with the 'TABLESPACE' and 'STORAGE DISK' options, similar to what is shown here:
mysql> CREATE TABLE mytable (
-> id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> lname VARCHAR(50) NOT NULL,
-> fname VARCHAR(50) NOT NULL,
-> dob DATE NOT NULL,
-> joined DATE NOT NULL,
-> INDEX(last_name, first_name)
-> )
-> TABLESPACE myts STORAGE DISK
-> ENGINE=NDB;
Query OK, 0 rows affected (1.41 sec)
It is important to note that only the 'dob' and 'joined' columns from 'mytable' are actually stored on disk, due to the fact that the 'id', 'lname', and 'fname' columns are all indexed.
As mentioned previously, when 'CREATE TABLESPACE' is used with 'ENGINE [=] NDB', a tablespace and associated data file are created on each NDB Cluster data node. You can verify that the data files were created and obtain information about them by querying the Information Schema *note 'FILES': information-schema-files-table. table, as shown here:
mysql> SELECT FILE_NAME, FILE_TYPE, LOGFILE_GROUP_NAME, STATUS, EXTRA
-> FROM INFORMATION_SCHEMA.FILES
-> WHERE TABLESPACE_NAME = 'myts';
+--------------+------------+--------------------+--------+----------------+
| file_name | file_type | logfile_group_name | status | extra |
+--------------+------------+--------------------+--------+----------------+
| mydata-1.dat | DATAFILE | mylg | NORMAL | CLUSTER_NODE=5 |
| mydata-1.dat | DATAFILE | mylg | NORMAL | CLUSTER_NODE=6 |
| NULL | TABLESPACE | mylg | NORMAL | NULL |
+--------------+------------+--------------------+--------+----------------+
3 rows in set (0.01 sec)
For additional information and examples, see *note mysql-cluster-disk-data-objects::.
File: manual.info.tmp, Node: create-trigger, Next: create-view, Prev: create-tablespace, Up: sql-data-definition-statements
CREATE
[DEFINER = USER]
TRIGGER TRIGGER_NAME
TRIGGER_TIME TRIGGER_EVENT
ON TBL_NAME FOR EACH ROW
[TRIGGER_ORDER]
TRIGGER_BODY
TRIGGER_TIME: { BEFORE | AFTER }
TRIGGER_EVENT: { INSERT | UPDATE | DELETE }
TRIGGER_ORDER: { FOLLOWS | PRECEDES } OTHER_TRIGGER_NAME
This statement creates a new trigger. A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. The trigger becomes associated with the table named TBL_NAME, which must refer to a permanent table. You cannot associate a trigger with a 'TEMPORARY' table or a view.
Trigger names exist in the schema namespace, meaning that all triggers must have unique names within a schema. Triggers in different schemas can have the same name.
This section describes note 'CREATE TRIGGER': create-trigger. syntax. For additional discussion, see note trigger-syntax::.
note 'CREATE TRIGGER': create-trigger. requires the 'TRIGGER' privilege for the table associated with the trigger. If the 'DEFINER' clause is present, the privileges required depend on the USER value, as discussed in note stored-objects-security::. If binary logging is enabled, note 'CREATE TRIGGER': create-trigger. might require the 'SUPER' privilege, as discussed in note stored-programs-logging::.
The 'DEFINER' clause determines the security context to be used when checking access privileges at trigger activation time, as described later in this section.
TRIGGER_TIME is the trigger action time. It can be 'BEFORE' or 'AFTER' to indicate that the trigger activates before or after each row to be modified.
Basic column value checks occur prior to trigger activation, so you cannot use 'BEFORE' triggers to convert values inappropriate for the column type to valid values.
TRIGGER_EVENT indicates the kind of operation that activates the trigger. These TRIGGER_EVENT values are permitted:
note 'INSERT': insert.: The trigger activates whenever a new row is inserted into the table (for example, through note 'INSERT': insert, note 'LOAD DATA': load-data, and note 'REPLACE': replace. statements).
note 'UPDATE': update.: The trigger activates whenever a row is modified (for example, through note 'UPDATE': update. statements).
note 'DELETE': delete.: The trigger activates whenever a row is deleted from the table (for example, through note 'DELETE': delete. and note 'REPLACE': replace. statements). note 'DROP TABLE': drop-table. and note 'TRUNCATE TABLE': truncate-table. statements on the table do not activate this trigger, because they do not use note 'DELETE': delete. Dropping a partition does not activate *note 'DELETE': delete. triggers, either.
The TRIGGER_EVENT does not represent a literal type of SQL statement that activates the trigger so much as it represents a type of table operation. For example, an note 'INSERT': insert. trigger activates not only for note 'INSERT': insert. statements but also *note 'LOAD DATA': load-data. statements because both statements insert rows into a table.
A potentially confusing example of this is the 'INSERT INTO ... ON DUPLICATE KEY UPDATE ...' syntax: a 'BEFORE INSERT' trigger activates for every row, followed by either an 'AFTER INSERT' trigger or both the 'BEFORE UPDATE' and 'AFTER UPDATE' triggers, depending on whether there was a duplicate key for the row.
Note:
Cascaded foreign key actions do not activate triggers.
It is possible to define multiple triggers for a given table that have the same trigger event and action time. For example, you can have two 'BEFORE UPDATE' triggers for a table. By default, triggers that have the same trigger event and action time activate in the order they were created. To affect trigger order, specify a TRIGGER_ORDER clause that indicates 'FOLLOWS' or 'PRECEDES' and the name of an existing trigger that also has the same trigger event and action time. With 'FOLLOWS', the new trigger activates after the existing trigger. With 'PRECEDES', the new trigger activates before the existing trigger.
TRIGGER_BODY is the statement to execute when the trigger activates. To execute multiple statements, use the note 'BEGIN ... END': begin-end. compound statement construct. This also enables you to use the same statements that are permitted within stored routines. See note begin-end::. Some statements are not permitted in triggers; see *note stored-program-restrictions::.
Within the trigger body, you can refer to columns in the subject table (the table associated with the trigger) by using the aliases 'OLD' and 'NEW'. 'OLD.COL_NAME' refers to a column of an existing row before it is updated or deleted. 'NEW.COL_NAME' refers to the column of a new row to be inserted or an existing row after it is updated.
Triggers cannot use 'NEW.COL_NAME' or use 'OLD.COL_NAME' to refer to generated columns. For information about generated columns, see *note create-table-generated-columns::.
MySQL stores the 'sql_mode' system variable setting in effect when a trigger is created, and always executes the trigger body with this setting in force, regardless of the current server SQL mode when the trigger begins executing.
The 'DEFINER' clause specifies the MySQL account to be used when checking access privileges at trigger activation time. If the 'DEFINER' clause is present, the USER value should be a MySQL account specified as ''USER_NAME'@'HOST_NAME'', 'CURRENT_USER', or 'CURRENT_USER()'. The permitted USER values depend on the privileges you hold, as discussed in *note stored-objects-security::. Also see that section for additional information about trigger security.
If the 'DEFINER' clause is omitted, the default definer is the user who executes the *note 'CREATE TRIGGER': create-trigger. statement. This is the same as specifying 'DEFINER = CURRENT_USER' explicitly.
MySQL takes the 'DEFINER' user into account when checking trigger privileges as follows:
At *note 'CREATE TRIGGER': create-trigger. time, the user who issues the statement must have the 'TRIGGER' privilege.
At trigger activation time, privileges are checked against the 'DEFINER' user. This user must have these privileges:
* The 'TRIGGER' privilege for the subject table.
* The 'SELECT' privilege for the subject table if references to
table columns occur using 'OLD.COL_NAME' or 'NEW.COL_NAME' in
the trigger body.
* The 'UPDATE' privilege for the subject table if table columns
are targets of 'SET NEW.COL_NAME = VALUE' assignments in the
trigger body.
* Whatever other privileges normally are required for the
statements executed by the trigger.
Within a trigger body, the 'CURRENT_USER' function returns the account used to check privileges at trigger activation time. This is the 'DEFINER' user, not the user whose actions caused the trigger to be activated. For information about user auditing within triggers, see *note account-activity-auditing::.
If you use note 'LOCK TABLES': lock-tables. to lock a table that has triggers, the tables used within the trigger are also locked, as described in note lock-tables-and-triggers::.
For additional discussion of trigger use, see *note trigger-syntax::.
File: manual.info.tmp, Node: create-view, Next: drop-database, Prev: create-trigger, Up: sql-data-definition-statements
CREATE
[OR REPLACE]
[ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
[DEFINER = USER]
[SQL SECURITY { DEFINER | INVOKER }]
VIEW VIEW_NAME [(COLUMN_LIST)]
AS SELECT_STATEMENT
[WITH [CASCADED | LOCAL] CHECK OPTION]
The note 'CREATE VIEW': create-view. statement creates a new view, or replaces an existing view if the 'OR REPLACE' clause is given. If the view does not exist, note 'CREATE OR REPLACE VIEW': create-view. is the same as note 'CREATE VIEW': create-view. If the view does exist, note 'CREATE OR REPLACE VIEW': create-view. replaces it.
For information about restrictions on view use, see *note view-restrictions::.
The SELECT_STATEMENT is a note 'SELECT': select. statement that provides the definition of the view. (Selecting from the view selects, in effect, using the note 'SELECT': select. statement.) The SELECT_STATEMENT can select from base tables or from other views.
The view definition is 'frozen' at creation time and is not affected by subsequent changes to the definitions of the underlying tables. For example, if a view is defined as 'SELECT *' on a table, new columns added to the table later do not become part of the view, and columns dropped from the table result in an error when selecting from the view.
The 'ALGORITHM' clause affects how MySQL processes the view. The 'DEFINER' and 'SQL SECURITY' clauses specify the security context to be used when checking access privileges at view invocation time. The 'WITH CHECK OPTION' clause can be given to constrain inserts or updates to rows in tables referenced by the view. These clauses are described later in this section.
The note 'CREATE VIEW': create-view. statement requires the 'CREATE VIEW' privilege for the view, and some privilege for each column selected by the note 'SELECT': select. statement. For columns used elsewhere in the note 'SELECT': select. statement, you must have the 'SELECT' privilege. If the 'OR REPLACE' clause is present, you must also have the 'DROP' privilege for the view. If the 'DEFINER' clause is present, the privileges required depend on the USER value, as discussed in note stored-objects-security::.
When a view is referenced, privilege checking occurs as described later in this section.
A view belongs to a database. By default, a new view is created in the default database. To create the view explicitly in a given database, use DB_NAME.VIEW_NAME syntax to qualify the view name with the database name:
CREATE VIEW test.v AS SELECT * FROM t;
Unqualified table or view names in the *note 'SELECT': select. statement are also interpreted with respect to the default database. A view can refer to tables or views in other databases by qualifying the table or view name with the appropriate database name.
Within a database, base tables and views share the same namespace, so a base table and a view cannot have the same name.
Columns retrieved by the *note 'SELECT': select. statement can be simple references to table columns, or expressions that use functions, constant values, operators, and so forth.
A view must have unique column names with no duplicates, just like a base table. By default, the names of the columns retrieved by the note 'SELECT': select. statement are used for the view column names. To define explicit names for the view columns, specify the optional COLUMN_LIST clause as a list of comma-separated identifiers. The number of names in COLUMN_LIST must be the same as the number of columns retrieved by the note 'SELECT': select. statement.
A view can be created from many kinds of note 'SELECT': select. statements. It can refer to base tables or other views. It can use joins, note 'UNION': union, and subqueries. The *note 'SELECT': select. need not even refer to any tables:
CREATE VIEW v_today (today) AS SELECT CURRENT_DATE;
The following example defines a view that selects two columns from another table as well as an expression calculated from those columns:
mysql> CREATE TABLE t (qty INT, price INT);
mysql> INSERT INTO t VALUES(3, 50);
mysql> CREATE VIEW v AS SELECT qty, price, qty*price AS value FROM t;
mysql> SELECT * FROM v;
+------+-------+-------+
| qty | price | value |
+------+-------+-------+
| 3 | 50 | 150 |
+------+-------+-------+
A view definition is subject to the following restrictions:
The *note 'SELECT': select. statement cannot refer to system variables or user-defined variables.
Within a stored program, the *note 'SELECT': select. statement cannot refer to program parameters or local variables.
The *note 'SELECT': select. statement cannot refer to prepared statement parameters.
Any table or view referred to in the definition must exist. If, after the view has been created, a table or view that the definition refers to is dropped, use of the view results in an error. To check a view definition for problems of this kind, use the *note 'CHECK TABLE': check-table. statement.
The definition cannot refer to a 'TEMPORARY' table, and you cannot create a 'TEMPORARY' view.
You cannot associate a trigger with a view.
Aliases for column names in the *note 'SELECT': select. statement are checked against the maximum column length of 64 characters (not the maximum alias length of 256 characters).
'ORDER BY' is permitted in a view definition, but it is ignored if you select from a view using a statement that has its own 'ORDER BY'.
For other options or clauses in the definition, they are added to the options or clauses of the statement that references the view, but the effect is undefined. For example, if a view definition includes a 'LIMIT' clause, and you select from the view using a statement that has its own 'LIMIT' clause, it is undefined which limit applies. This same principle applies to options such as 'ALL', 'DISTINCT', or 'SQL_SMALL_RESULT' that follow the *note 'SELECT': select. keyword, and to clauses such as 'INTO', 'FOR UPDATE', 'LOCK IN SHARE MODE', and 'PROCEDURE'.
The results obtained from a view may be affected if you change the query processing environment by changing system variables:
mysql> CREATE VIEW v (mycol) AS SELECT 'abc';
Query OK, 0 rows affected (0.01 sec)
mysql> SET sql_mode = '';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT "mycol" FROM v;
+-------+
| mycol |
+-------+
| mycol |
+-------+
1 row in set (0.01 sec)
mysql> SET sql_mode = 'ANSI_QUOTES';
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT "mycol" FROM v;
+-------+
| mycol |
+-------+
| abc |
+-------+
1 row in set (0.00 sec)
The 'DEFINER' and 'SQL SECURITY' clauses determine which MySQL account to use when checking access privileges for the view when a statement is executed that references the view. The valid 'SQL SECURITY' characteristic values are 'DEFINER' (the default) and 'INVOKER'. These indicate that the required privileges must be held by the user who defined or invoked the view, respectively.
If the 'DEFINER' clause is present, the USER value should be a MySQL account specified as ''USER_NAME'@'HOST_NAME'', 'CURRENT_USER', or 'CURRENT_USER()'. The permitted USER values depend on the privileges you hold, as discussed in *note stored-objects-security::. Also see that section for additional information about view security.
If the 'DEFINER' clause is omitted, the default definer is the user who executes the *note 'CREATE VIEW': create-view. statement. This is the same as specifying 'DEFINER = CURRENT_USER' explicitly.
Within a view definition, the 'CURRENT_USER' function returns the view's 'DEFINER' value by default. For views defined with the 'SQL SECURITY INVOKER' characteristic, 'CURRENT_USER' returns the account for the view's invoker. For information about user auditing within views, see *note account-activity-auditing::.
Within a stored routine that is defined with the 'SQL SECURITY DEFINER' characteristic, 'CURRENT_USER' returns the routine's 'DEFINER' value. This also affects a view defined within such a routine, if the view definition contains a 'DEFINER' value of 'CURRENT_USER'.
MySQL checks view privileges like this:
At view definition time, the view creator must have the privileges needed to use the top-level objects accessed by the view. For example, if the view definition refers to table columns, the creator must have some privilege for each column in the select list of the definition, and the 'SELECT' privilege for each column used elsewhere in the definition. If the definition refers to a stored function, only the privileges needed to invoke the function can be checked. The privileges required at function invocation time can be checked only as it executes: For different invocations, different execution paths within the function might be taken.
The user who references a view must have appropriate privileges to access it ('SELECT' to select from it, 'INSERT' to insert into it, and so forth.)
When a view has been referenced, privileges for objects accessed by the view are checked against the privileges held by the view 'DEFINER' account or invoker, depending on whether the 'SQL SECURITY' characteristic is 'DEFINER' or 'INVOKER', respectively.
If reference to a view causes execution of a stored function, privilege checking for statements executed within the function depend on whether the function 'SQL SECURITY' characteristic is 'DEFINER' or 'INVOKER'. If the security characteristic is 'DEFINER', the function runs with the privileges of the 'DEFINER' account. If the characteristic is 'INVOKER', the function runs with the privileges determined by the view's 'SQL SECURITY' characteristic.
Example: A view might depend on a stored function, and that function might invoke other stored routines. For example, the following view invokes a stored function 'f()':
CREATE VIEW v AS SELECT * FROM t WHERE t.id = f(t.name);
Suppose that 'f()' contains a statement such as this:
IF name IS NULL then
CALL p1();
ELSE
CALL p2();
END IF;
The privileges required for executing statements within 'f()' need to be checked when 'f()' executes. This might mean that privileges are needed for 'p1()' or 'p2()', depending on the execution path within 'f()'. Those privileges must be checked at runtime, and the user who must possess the privileges is determined by the 'SQL SECURITY' values of the view 'v' and the function 'f()'.
The 'DEFINER' and 'SQL SECURITY' clauses for views are extensions to standard SQL. In standard SQL, views are handled using the rules for 'SQL SECURITY DEFINER'. The standard says that the definer of the view, which is the same as the owner of the view's schema, gets applicable privileges on the view (for example, 'SELECT') and may grant them. MySQL has no concept of a schema 'owner', so MySQL adds a clause to identify the definer. The 'DEFINER' clause is an extension where the intent is to have what the standard has; that is, a permanent record of who defined the view. This is why the default 'DEFINER' value is the account of the view creator.
The optional 'ALGORITHM' clause is a MySQL extension to standard SQL. It affects how MySQL processes the view. 'ALGORITHM' takes three values: 'MERGE', 'TEMPTABLE', or 'UNDEFINED'. For more information, see note view-algorithms::, as well as note derived-table-optimization::.
Some views are updatable. That is, you can use them in statements such as note 'UPDATE': update, note 'DELETE': delete, or *note 'INSERT': insert. to update the contents of the underlying table. For a view to be updatable, there must be a one-to-one relationship between the rows in the view and the rows in the underlying table. There are also certain other constructs that make a view nonupdatable.
A generated column in a view is considered updatable because it is possible to assign to it. However, if such a column is updated explicitly, the only permitted value is 'DEFAULT'. For information about generated columns, see *note create-table-generated-columns::.
The 'WITH CHECK OPTION' clause can be given for an updatable view to prevent inserts or updates to rows except those for which the 'WHERE' clause in the SELECT_STATEMENT is true.
In a 'WITH CHECK OPTION' clause for an updatable view, the 'LOCAL' and 'CASCADED' keywords determine the scope of check testing when the view is defined in terms of another view. The 'LOCAL' keyword restricts the 'CHECK OPTION' only to the view being defined. 'CASCADED' causes the checks for underlying views to be evaluated as well. When neither keyword is given, the default is 'CASCADED'.
For more information about updatable views and the 'WITH CHECK OPTION' clause, see note view-updatability::, and note view-check-option::.
Views created before MySQL 5.7.3 containing 'ORDER BY INTEGER' can result in errors at view evaluation time. Consider these view definitions, which use 'ORDER BY' with an ordinal number:
CREATE VIEW v1 AS SELECT x, y, z FROM t ORDER BY 2;
CREATE VIEW v2 AS SELECT x, 1, z FROM t ORDER BY 2;
In the first case, 'ORDER BY 2' refers to a named column 'y'. In the second case, it refers to a constant 1. For queries that select from either view fewer than 2 columns (the number named in the 'ORDER BY' clause), an error occurs if the server evaluates the view using the MERGE algorithm. Examples:
mysql> SELECT x FROM v1;
ERROR 1054 (42S22): Unknown column '2' in 'order clause'
mysql> SELECT x FROM v2;
ERROR 1054 (42S22): Unknown column '2' in 'order clause'
As of MySQL 5.7.3, to handle view definitions like this, the server writes them differently into the '.frm' file that stores the view definition. This difference is visible with *note 'SHOW CREATE VIEW': show-create-view. Previously, the '.frm' file contained this for the 'ORDER BY 2' clause:
For v1: ORDER BY 2
For v2: ORDER BY 2
As of 5.7.3, the '.frm' file contains this:
For v1: ORDER BY `t`.`y`
For v2: ORDER BY ''
That is, for 'v1', 2 is replaced by a reference to the name of the column referred to. For 'v2', 2 is replaced by a constant string expression (ordering by a constant has no effect, so ordering by any constant works).
If you experience view-evaluation errors such as just described, drop and recreate the view so that the '.frm' file contains the updated view representation. Alternatively, for views like 'v2' that order by a constant value, drop and recreate the view with no 'ORDER BY' clause.
File: manual.info.tmp, Node: drop-database, Next: drop-event, Prev: create-view, Up: sql-data-definition-statements
DROP {DATABASE | SCHEMA} [IF EXISTS] DB_NAME
note 'DROP DATABASE': drop-database. drops all tables in the database and deletes the database. Be very careful with this statement! To use note 'DROP DATABASE': drop-database, you need the 'DROP' privilege on the database. note 'DROP SCHEMA': drop-database. is a synonym for note 'DROP DATABASE': drop-database.
Important:
When a database is dropped, privileges granted specifically for the database are not automatically dropped. They must be dropped manually. See *note grant::.
'IF EXISTS' is used to prevent an error from occurring if the database does not exist.
If the default database is dropped, the default database is unset (the 'DATABASE()' function returns 'NULL').
If you use *note 'DROP DATABASE': drop-database. on a symbolically linked database, both the link and the original database are deleted.
*note 'DROP DATABASE': drop-database. returns the number of tables that were removed. This corresponds to the number of '.frm' files removed.
The *note 'DROP DATABASE': drop-database. statement removes from the given database directory those files and directories that MySQL itself may create during normal operation:
All files with the following extensions:
* '.BAK'
* '.DAT'
* '.HSH'
* '.MRG'
* '.MYD'
* '.MYI'
* '.TRG'
* '.TRN'
* '.cfg'
* '.db'
* '.frm'
* '.ibd'
* '.ndb'
* '.par'
The 'db.opt' file, if it exists.
If other files or directories remain in the database directory after MySQL removes those just listed, the database directory cannot be removed. In this case, you must remove any remaining files or directories manually and issue the *note 'DROP DATABASE': drop-database. statement again.
Dropping a database does not remove any 'TEMPORARY' tables that were created in that database. 'TEMPORARY' tables are automatically removed when the session that created them ends. See *note create-temporary-table::.
You can also drop databases with note 'mysqladmin': mysqladmin. See note mysqladmin::.
File: manual.info.tmp, Node: drop-event, Next: drop-function, Prev: drop-database, Up: sql-data-definition-statements
DROP EVENT [IF EXISTS] EVENT_NAME
This statement drops the event named EVENT_NAME. The event immediately ceases being active, and is deleted completely from the server.
If the event does not exist, the error 'ERROR 1517 (HY000): Unknown event 'EVENT_NAME'' results. You can override this and cause the statement to generate a warning for nonexistent events instead using 'IF EXISTS'.
This statement requires the 'EVENT' privilege for the schema to which the event to be dropped belongs.
File: manual.info.tmp, Node: drop-function, Next: drop-index, Prev: drop-event, Up: sql-data-definition-statements
The *note 'DROP FUNCTION': drop-function. statement is used to drop stored functions and loadable functions:
For information about dropping stored functions, see *note drop-procedure::.
For information about dropping loadable functions, see *note drop-function-loadable::.
File: manual.info.tmp, Node: drop-index, Next: drop-logfile-group, Prev: drop-function, Up: sql-data-definition-statements
DROP INDEX INDEX_NAME ON TBL_NAME
[ALGORITHM_OPTION | LOCK_OPTION] ...
ALGORITHM_OPTION:
ALGORITHM [=] {DEFAULT | INPLACE | COPY}
LOCK_OPTION:
LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}
note 'DROP INDEX': drop-index. drops the index named INDEX_NAME from the table TBL_NAME. This statement is mapped to an note 'ALTER TABLE': alter-table. statement to drop the index. See *note alter-table::.
To drop a primary key, the index name is always 'PRIMARY', which must be specified as a quoted identifier because 'PRIMARY' is a reserved word:
DROP INDEX `PRIMARY` ON t;
Indexes on variable-width columns of *note 'NDB': mysql-cluster. tables are dropped online; that is, without any table copying. The table is not locked against access from other NDB Cluster API nodes, although it is locked against other operations on the same API node for the duration of the operation. This is done automatically by the server whenever it determines that it is possible to do so; you do not have to use any special SQL syntax or server options to cause it to happen.
'ALGORITHM' and 'LOCK' clauses may be given to influence the table copying method and level of concurrency for reading and writing the table while its indexes are being modified. They have the same meaning as for the note 'ALTER TABLE': alter-table. statement. For more information, see note alter-table::
NDB Cluster formerly supported online 'DROP INDEX' operations using the 'ONLINE' and 'OFFLINE' keywords. These keywords are no longer supported in MySQL NDB Cluster 7.5 and later, and their use causes a syntax error. Instead, MySQL NDB Cluster 7.5 and later support online operations using the same 'ALGORITHM=INPLACE' syntax used with the standard MySQL Server. See *note mysql-cluster-online-operations::, for more information.
File: manual.info.tmp, Node: drop-logfile-group, Next: drop-procedure, Prev: drop-index, Up: sql-data-definition-statements
DROP LOGFILE GROUP LOGFILE_GROUP
ENGINE [=] ENGINE_NAME
This statement drops the log file group named LOGFILE_GROUP. The log file group must already exist or an error results. (For information on creating log file groups, see *note create-logfile-group::.)
Important:
Before dropping a log file group, you must drop all tablespaces that use that log file group for 'UNDO' logging.
The required 'ENGINE' clause provides the name of the storage engine used by the log file group to be dropped. Currently, the only permitted values for ENGINE_NAME are note 'NDB': mysql-cluster. and note 'NDBCLUSTER': mysql-cluster.
note 'DROP LOGFILE GROUP': drop-logfile-group. is useful only with Disk Data storage for NDB Cluster. See note mysql-cluster-disk-data::.
File: manual.info.tmp, Node: drop-procedure, Next: drop-server, Prev: drop-logfile-group, Up: sql-data-definition-statements
DROP {PROCEDURE | FUNCTION} [IF EXISTS] SP_NAME
These statements are used to drop a stored routine (a stored procedure or function). That is, the specified routine is removed from the server. ('DROP FUNCTION' is also used to drop loadable functions; see *note drop-function-loadable::.)
To drop a stored routine, you must have the 'ALTER ROUTINE' privilege for it. (If the 'automatic_sp_privileges' system variable is enabled, that privilege and 'EXECUTE' are granted automatically to the routine creator when the routine is created and dropped from the creator when the routine is dropped. See *note stored-routines-privileges::.)
The 'IF EXISTS' clause is a MySQL extension. It prevents an error from occurring if the procedure or function does not exist. A warning is produced that can be viewed with *note 'SHOW WARNINGS': show-warnings.
note 'DROP FUNCTION': drop-function. is also used to drop loadable functions (see note drop-function-loadable::).
File: manual.info.tmp, Node: drop-server, Next: drop-table, Prev: drop-procedure, Up: sql-data-definition-statements
DROP SERVER [ IF EXISTS ] SERVER_NAME
Drops the server definition for the server named 'SERVER_NAME'. The corresponding row in the 'mysql.servers' table is deleted. This statement requires the 'SUPER' privilege.
Dropping a server for a table does not affect any 'FEDERATED' tables that used this connection information when they were created. See *note create-server::.
'DROP SERVER' causes an implicit commit. See *note implicit-commit::.
'DROP SERVER' is not written to the binary log, regardless of the logging format that is in use.
File: manual.info.tmp, Node: drop-table, Next: drop-tablespace, Prev: drop-server, Up: sql-data-definition-statements
DROP [TEMPORARY] TABLE [IF EXISTS]
TBL_NAME [, TBL_NAME] ...
[RESTRICT | CASCADE]
*note 'DROP TABLE': drop-table. removes one or more tables. You must have the 'DROP' privilege for each table.
Be careful with this statement! For each table, it removes the table definition and all table data. If the table is partitioned, the statement removes the table definition, all its partitions, all data stored in those partitions, and all partition definitions associated with the dropped table.
Dropping a table also drops any triggers for the table.
note 'DROP TABLE': drop-table. causes an implicit commit, except when used with the 'TEMPORARY' keyword. See note implicit-commit::.
Important:
When a table is dropped, privileges granted specifically for the table are not automatically dropped. They must be dropped manually. See *note grant::.
If any tables named in the argument list do not exist, *note 'DROP TABLE': drop-table. behavior depends on whether the 'IF EXISTS' clause is given:
Without 'IF EXISTS', the statement drops all named tables that do exist, and returns an error indicating which nonexisting tables it was unable to drop.
With 'IF EXISTS', no error occurs for nonexisting tables. The statement drops all named tables that do exist, and generates a 'NOTE' diagnostic for each nonexistent table. These notes can be displayed with note 'SHOW WARNINGS': show-warnings. See note show-warnings::.
'IF EXISTS' can also be useful for dropping tables in unusual circumstances under which there is an '.frm' file but no table managed by the storage engine. (For example, if an abnormal server exit occurs after removal of the table from the storage engine but before '.frm' file removal.)
The 'TEMPORARY' keyword has the following effects:
The statement drops only 'TEMPORARY' tables.
The statement does not cause an implicit commit.
No access rights are checked. A 'TEMPORARY' table is visible only with the session that created it, so no check is necessary.
Including the 'TEMPORARY' keyword is a good way to prevent accidentally dropping non-'TEMPORARY' tables.
The 'RESTRICT' and 'CASCADE' keywords do nothing. They are permitted to make porting easier from other database systems.
note 'DROP TABLE': drop-table. is not supported with all 'innodb_force_recovery' settings. See note forcing-innodb-recovery::.
File: manual.info.tmp, Node: drop-tablespace, Next: drop-trigger, Prev: drop-table, Up: sql-data-definition-statements
DROP TABLESPACE TABLESPACE_NAME
[ENGINE [=] ENGINE_NAME]
This statement drops a tablespace that was previously created using *note 'CREATE TABLESPACE': create-tablespace. It is supported with all MySQL NDB Cluster 7.5 releases, and with 'InnoDB' in the standard MySQL Server as well.
'ENGINE' sets the storage engine that uses the tablespace, where ENGINE_NAME is the name of the storage engine. Currently, the values 'InnoDB' and 'NDB' are supported. If not set, the value of 'default_storage_engine' is used. If it is not the same as the storage engine used to create the tablespace, the 'DROP TABLESPACE' statement fails.
For an 'InnoDB' tablespace, all tables must be dropped from the tablespace prior to a 'DROP TABLESPACE' operation. If the tablespace is not empty, 'DROP TABLESPACE' returns an error.
As with the 'InnoDB' system tablespace, truncating or dropping 'InnoDB' tables stored in a general tablespace creates free space in the tablespace .ibd data file, which can only be used for new 'InnoDB' data. Space is not released back to the operating system by such operations as it is for file-per-table tablespaces.
An 'NDB' tablespace to be dropped must not contain any data files; in other words, before you can drop an 'NDB' tablespace, you must first drop each of its data files using *note 'ALTER TABLESPACE ... DROP DATAFILE': alter-tablespace.
Notes
Tablespaces are not deleted automatically. A tablespace must be dropped explicitly using 'DROP TABLESPACE'. *note 'DROP DATABASE': drop-database. has no effect in this regard, even if the operation drops all tables belonging to the tablespace.
A *note 'DROP DATABASE': drop-database. operation can drop tables that belong to a general tablespace but it cannot drop the tablespace, even if the operation drops all tables that belong to the tablespace. The tablespace must be dropped explicitly using 'DROP TABLESPACE TABLESPACE_NAME'.
Similar to the system tablespace, truncating or dropping tables stored in a general tablespace creates free space internally in the general tablespace .ibd data file which can only be used for new 'InnoDB' data. Space is not released back to the operating system as it is for file-per-table tablespaces.
InnoDB Example
This example demonstrates how to drop an 'InnoDB' general tablespace. The general tablespace 'ts1' is created with a single table. Before dropping the tablespace, the table must be dropped.
mysql> CREATE TABLESPACE `ts1` ADD DATAFILE 'ts1.ibd' Engine=InnoDB;
mysql> CREATE TABLE t1 (c1 INT PRIMARY KEY) TABLESPACE ts1 Engine=InnoDB;
mysql> DROP TABLE t1;
mysql> DROP TABLESPACE ts1;
NDB Example
This example shows how to drop an 'NDB' tablespace 'myts' having a data file named 'mydata-1.dat' after first creating the tablespace, and assumes the existence of a log file group named 'mylg' (see *note create-logfile-group::).
mysql> CREATE TABLESPACE myts
-> ADD DATAFILE 'mydata-1.dat'
-> USE LOGFILE GROUP mylg
-> ENGINE=NDB;
You must remove all data files from the tablespace using *note 'ALTER TABLESPACE': alter-tablespace, as shown here, before it can be dropped:
mysql> ALTER TABLESPACE myts
-> DROP DATAFILE 'mydata-1.dat'
-> ENGINE=NDB;
mysql> DROP TABLESPACE myts;
File: manual.info.tmp, Node: drop-trigger, Next: drop-view, Prev: drop-tablespace, Up: sql-data-definition-statements
DROP TRIGGER [IF EXISTS] [SCHEMA_NAME.]TRIGGER_NAME
This statement drops a trigger. The schema (database) name is optional. If the schema is omitted, the trigger is dropped from the default schema. *note 'DROP TRIGGER': drop-trigger. requires the 'TRIGGER' privilege for the table associated with the trigger.
Use 'IF EXISTS' to prevent an error from occurring for a trigger that does not exist. A 'NOTE' is generated for a nonexistent trigger when using 'IF EXISTS'. See *note show-warnings::.
Triggers for a table are also dropped if you drop the table.
File: manual.info.tmp, Node: drop-view, Next: rename-table, Prev: drop-trigger, Up: sql-data-definition-statements
DROP VIEW [IF EXISTS]
VIEW_NAME [, VIEW_NAME] ...
[RESTRICT | CASCADE]
*note 'DROP VIEW': drop-view. removes one or more views. You must have the 'DROP' privilege for each view.
If any views named in the argument list do not exist, the statement returns an error indicating by name which nonexisting views it was unable to drop, but also drops all views in the list that do exist.
Note:
In MySQL 8.0, note 'DROP VIEW': drop-view. fails if any views named in the argument list do not exist. Due to the change in behavior, a partially completed note 'DROP VIEW': drop-view. operation on a MySQL 5.7 source fails when replicated to a MySQL 8.0 replica. To avoid this failure scenario, use 'IF EXISTS' syntax in *note 'DROP VIEW': drop-view. statements to prevent an error from occurring for views that do not exist. For more information, see Atomic Data Definition Statement Support (https://dev.mysql.com/doc/refman/8.0/en/atomic-ddl.html).
The 'IF EXISTS' clause prevents an error from occurring for views that don't exist. When this clause is given, a 'NOTE' is generated for each nonexistent view. See *note show-warnings::.
'RESTRICT' and 'CASCADE', if given, are parsed and ignored.
File: manual.info.tmp, Node: rename-table, Next: truncate-table, Prev: drop-view, Up: sql-data-definition-statements
RENAME TABLE
TBL_NAME TO NEW_TBL_NAME
[, TBL_NAME2 TO NEW_TBL_NAME2] ...
*note 'RENAME TABLE': rename-table. renames one or more tables. You must have 'ALTER' and 'DROP' privileges for the original table, and 'CREATE' and 'INSERT' privileges for the new table.
For example, to rename a table named 'old_table' to 'new_table', use this statement:
RENAME TABLE old_table TO new_table;
That statement is equivalent to the following *note 'ALTER TABLE': alter-table. statement:
ALTER TABLE old_table RENAME new_table;
'RENAME TABLE', unlike *note 'ALTER TABLE': alter-table, can rename multiple tables within a single statement:
RENAME TABLE old_table1 TO new_table1,
old_table2 TO new_table2,
old_table3 TO new_table3;
Renaming operations are performed left to right. Thus, to swap two table names, do this (assuming that a table with the intermediary name 'tmp_table' does not already exist):
RENAME TABLE old_table TO tmp_table,
new_table TO old_table,
tmp_table TO new_table;
Metadata locks on tables are acquired in name order, which in some cases can make a difference in operation outcome when multiple transactions execute concurrently. See *note metadata-locking::.
To execute 'RENAME TABLE', there must be no active transactions or tables locked with 'LOCK TABLES'. With the transaction table locking conditions satisfied, the rename operation is done atomically; no other session can access any of the tables while the rename is in progress.
If any errors occur during a 'RENAME TABLE', the statement fails and no changes are made.
You can use 'RENAME TABLE' to move a table from one database to another:
RENAME TABLE CURRENT_DB.TBL_NAME TO OTHER_DB.TBL_NAME;
Using this method to move all tables from one database to a different one in effect renames the database (an operation for which MySQL has no single statement), except that the original database continues to exist, albeit with no tables.
Like 'RENAME TABLE', 'ALTER TABLE ... RENAME' can also be used to move a table to a different database. Regardless of the statement used, if the rename operation would move the table to a database located on a different file system, the success of the outcome is platform specific and depends on the underlying operating system calls used to move table files.
If a table has triggers, attempts to rename the table into a different database fail with a 'Trigger in wrong schema' ('ER_TRG_IN_WRONG_SCHEMA' (https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_trg_in_wrong_schema)) error.
To rename 'TEMPORARY' tables, 'RENAME TABLE' does not work. Use *note 'ALTER TABLE': alter-table. instead.
'RENAME TABLE' works for views, except that views cannot be renamed into a different database.
Any privileges granted specifically for a renamed table or view are not migrated to the new name. They must be changed manually.
'RENAME TABLE TBL_NAME TO NEW_TBL_NAME' changes internally generated foreign key constraint names and user-defined foreign key constraint names that begin with the string 'TBL_NAME_ibfk_' to reflect the new table name. 'InnoDB' interprets foreign key constraint names that begin with the string 'TBL_NAME_ibfk_' as internally generated names.
Foreign key constraint names that point to the renamed table are automatically updated unless there is a conflict, in which case the statement fails with an error. A conflict occurs if the renamed constraint name already exists. In such cases, you must drop and re-create the foreign keys for them to function properly.
File: manual.info.tmp, Node: truncate-table, Prev: rename-table, Up: sql-data-definition-statements
TRUNCATE [TABLE] TBL_NAME
*note 'TRUNCATE TABLE': truncate-table. empties a table completely. It requires the 'DROP' privilege.
Logically, note 'TRUNCATE TABLE': truncate-table. is similar to a note 'DELETE': delete. statement that deletes all rows, or a sequence of note 'DROP TABLE': drop-table. and note 'CREATE TABLE': create-table. statements. To achieve high performance, it bypasses the DML method of deleting data. Thus, it cannot be rolled back, it does not cause 'ON DELETE' triggers to fire, and it cannot be performed for 'InnoDB' tables with parent-child foreign key relationships.
Although note 'TRUNCATE TABLE': truncate-table. is similar to note 'DELETE': delete, it is classified as a DDL statement rather than a DML statement. It differs from *note 'DELETE': delete. in the following ways:
Truncate operations drop and re-create the table, which is much faster than deleting rows one by one, particularly for large tables.
Truncate operations cause an implicit commit, and so cannot be rolled back. See *note implicit-commit::.
Truncation operations cannot be performed if the session holds an active table lock.
note 'TRUNCATE TABLE': truncate-table. fails for an 'InnoDB' table or note 'NDB': mysql-cluster. table if there are any 'FOREIGN KEY' constraints from other tables that reference the table. Foreign key constraints between columns of the same table are permitted.
Truncation operations do not return a meaningful value for the number of deleted rows. The usual result is '0 rows affected,' which should be interpreted as 'no information.'
As long as the table format file 'TBL_NAME.frm' is valid, the table can be re-created as an empty table with *note 'TRUNCATE TABLE': truncate-table, even if the data or index files have become corrupted.
Any 'AUTO_INCREMENT' value is reset to its start value. This is true even for 'MyISAM' and 'InnoDB', which normally do not reuse sequence values.
When used with partitioned tables, *note 'TRUNCATE TABLE': truncate-table. preserves the partitioning; that is, the data and index files are dropped and re-created, while the partition definitions ('.par') file is unaffected.
The *note 'TRUNCATE TABLE': truncate-table. statement does not invoke 'ON DELETE' triggers.
*note 'TRUNCATE TABLE': truncate-table. is treated for purposes of binary logging and replication as DDL rather than DML, and is always logged as a statement.
note 'TRUNCATE TABLE': truncate-table. for a table closes all handlers for the table that were opened with note 'HANDLER OPEN': handler.
On a system with a large 'InnoDB' buffer pool and 'innodb_adaptive_hash_index' enabled, 'TRUNCATE TABLE' operations may cause a temporary drop in system performance due to an LRU scan that occurs when removing an 'InnoDB' table's adaptive hash index entries. The problem was addressed for *note 'DROP TABLE': drop-table. in MySQL 5.5.23 (Bug #13704145, Bug #64284) but remains a known issue for 'TRUNCATE TABLE' (Bug #68184).
note 'TRUNCATE TABLE': truncate-table. can be used with Performance Schema summary tables, but the effect is to reset the summary columns to 0 or 'NULL', not to remove rows. See note performance-schema-summary-tables::.
File: manual.info.tmp, Node: sql-data-manipulation-statements, Next: sql-transactional-statements, Prev: sql-data-definition-statements, Up: sql-statements