13.1.17 CREATE TABLE Syntax
13.1.17.1 CREATE TABLE ... LIKE Syntax
13.1.17.2 CREATE TABLE ... SELECT Syntax
13.1.17.3 Using FOREIGN KEY Constraints
13.1.17.4 Silent Column Specification Changes
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
| [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
[index_option] ...
| {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
[index_option] ...
| [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY]
[index_name] [index_type] (index_col_name,...)
[index_option] ...
| {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...)
[index_option] ...
| [CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name,...) reference_definition
| CHECK (expr)
column_definition:
data_type [NOT NULL | NULL] [DEFAULT default_value]
[AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
[COMMENT 'string']
[COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]
[STORAGE {DISK|MEMORY|DEFAULT}]
[reference_definition]
data_type:
BIT[(length)]
| TINYINT[(length)] [UNSIGNED] [ZEROFILL]
| SMALLINT[(length)] [UNSIGNED] [ZEROFILL]
| MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL]
| INT[(length)] [UNSIGNED] [ZEROFILL]
| INTEGER[(length)] [UNSIGNED] [ZEROFILL]
| BIGINT[(length)] [UNSIGNED] [ZEROFILL]
| REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
| DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]
| FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL]
| DECIMAL[(length[,decimals])] [UNSIGNED] [ZEROFILL]
| NUMERIC[(length[,decimals])] [UNSIGNED] [ZEROFILL]
| DATE
| TIME[(fsp)]
| TIMESTAMP[(fsp)]
| DATETIME[(fsp)]
| YEAR
| CHAR[(length)] [BINARY]
[CHARACTER SET charset_name] [COLLATE collation_name]
| VARCHAR(length) [BINARY]
[CHARACTER SET charset_name] [COLLATE collation_name]
| BINARY[(length)]
| VARBINARY(length)
| TINYBLOB
| BLOB
| MEDIUMBLOB
| LONGBLOB
| TINYTEXT [BINARY]
[CHARACTER SET charset_name] [COLLATE collation_name]
| TEXT [BINARY]
[CHARACTER SET charset_name] [COLLATE collation_name]
| MEDIUMTEXT [BINARY]
[CHARACTER SET charset_name] [COLLATE collation_name]
| LONGTEXT [BINARY]
[CHARACTER SET charset_name] [COLLATE collation_name]
| ENUM(value1,value2,value3,...)
[CHARACTER SET charset_name] [COLLATE collation_name]
| SET(value1,value2,value3,...)
[CHARACTER SET charset_name] [COLLATE collation_name]
| spatial_type
index_col_name:
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 (index_col_name,...)
[MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]
[ON DELETE reference_option]
[ON UPDATE reference_option]
reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION
table_options:
table_option [[,] table_option] ...
table_option:
ENGINE [=] engine_name
| AUTO_INCREMENT [=] value
| AVG_ROW_LENGTH [=] value
| [DEFAULT] CHARACTER SET [=] charset_name
| CHECKSUM [=] {0 | 1}
| [DEFAULT] COLLATE [=] collation_name
| COMMENT [=] 'string'
| CONNECTION [=] 'connect_string'
| DATA DIRECTORY [=] 'absolute path to directory'
| DELAY_KEY_WRITE [=] {0 | 1}
| INDEX DIRECTORY [=] 'absolute path to directory'
| 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|DEFAULT}]
| 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 [=] 'comment_text' ]
[DATA DIRECTORY [=] 'data_dir']
[INDEX DIRECTORY [=] 'index_dir']
[MAX_ROWS [=] max_number_of_rows]
[MIN_ROWS [=] min_number_of_rows]
[TABLESPACE [=] tablespace_name]
[NODEGROUP [=] node_group_id]
[(subpartition_definition [, subpartition_definition] ...)]
subpartition_definition:
SUBPARTITION logical_name
[[STORAGE] ENGINE [=] engine_name]
[COMMENT [=] 'comment_text' ]
[DATA DIRECTORY [=] 'data_dir']
[INDEX DIRECTORY [=] 'index_dir']
[MAX_ROWS [=] max_number_of_rows]
[MIN_ROWS [=] min_number_of_rows]
[TABLESPACE [=] tablespace_name]
[NODEGROUP [=] node_group_id]
query_expression:
SELECT ... (Some valid select or union statement)
CREATE TABLE creates a table with the given name. You must have the CREATE privilege for the table.
Rules for permissible table names are given in Section 9.2, “Schema Object Names”. By default, the table is created in the default database, using the InnoDB storage engine. An error occurs if the table exists, if there is no default database, or if the database does not exist.
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`
.
Cloning or Copying a Table
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 Section 13.1.17.1, “CREATE TABLE ... LIKE Syntax”.
To create one table from another, add a SELECT statement at the end of the CREATE TABLE statement:
CREATE TABLE new_tbl SELECT * FROM orig_tbl;
For more information, see Section 13.1.17.2, “CREATE TABLE ... SELECT Syntax”.
Temporary Tables
You can use the
TEMPORARY
keyword when creating a table. A
TEMPORARY
table is visible only to 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.) To create temporary tables, you must have the CREATE TEMPORARY TABLES privilege.
Note
CREATE TABLE does not automatically commit the current active transaction if you use the
TEMPORARY
keyword.
Note
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.
Existing Table with Same Name
The keywords
IF NOT EXISTS
prevent 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 CREATE TABLE statement.
Physical Representation
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
InnoDB
tables, the file storage is controlled by the innodb_file_per_table configuration option. For each
InnoDB
table created when this option is turned on, the table data and all associated indexes are stored in a .ibd file located inside the database directory. When this option is turned off, all
InnoDB
tables and indexes are stored in the system tablespace, represented by one or more ibdata* files.
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 |
| Table format (definition) file |
| Data file |
| Index file |
Chapter 15, Alternative 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 inSection 9.2.3, “Mapping of Identifiers to File Names”.
Data Types and Attributes for Columns
data_type
represents the data type in a column definition.
spatial_type
represents a spatial data type. The data type syntax shown is representative only. For a full description of the syntax available for specifying column data types, as well as information about the properties of each type, see Chapter 11, Data Types, and Section 11.5, “Extensions for Spatial Data”.
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 BLOB or TEXT types.
- If neither
nor NULL
is specified, the column is treated as though NOT NULL
had been specified.NULL
- An integer or floating-point column can have the additional attribute
. When you insert a value of AUTO_INCREMENT
(recommended) or NULL
into an indexed 0
column, the column is set to the next sequence value. Typically this is AUTO_INCREMENT
, where value+1
is the largest value for the column currently in the table. value
sequences begin with AUTO_INCREMENT
1
.
To retrieve an
AUTO_INCREMENT
value after inserting a row, use the LAST_INSERT_ID() SQL function or the mysql_insert_id() C API function. See Section 12.14, “Information Functions”, and Section 23.8.7.37, “mysql_insert_id()”.
If theNO_AUTO_VALUE_ON_ZERO SQL mode is enabled, you can store
in 0
columns as AUTO_INCREMENT
0
without generating a new sequence value. See Section 5.1.7, “Server SQL Modes”.
NoteThere can be only one
column per table, it must be indexed, and it cannot have a AUTO_INCREMENT
value. An DEFAULT
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 AUTO_INCREMENT
0
.
For
tables, you can specify an MyISAM
secondary column in a multiple-column key. See Section 3.6.9, “Using AUTO_INCREMENT”.To make MySQL compatible with some ODBC applications, you can find theAUTO_INCREMENT
value for the last inserted row with the following query:AUTO_INCREMENT
SELECT * FROM tbl_name WHERE auto_col
This method requires that sql_auto_is_null variable is not set to 0. See Section 5.1.4, “Server System Variables”.
For information about
InnoDB
and
AUTO_INCREMENT
, see Section 14.8.6, “AUTO_INCREMENT Handling in InnoDB”. For information about
AUTO_INCREMENT
and MySQL Replication, see Section 17.4.1.1, “Replication and AUTO_INCREMENT”.
- Character data types (CHAR, VARCHAR, TEXT) can include
and CHARACTER SET
attributes to specify the character set and collation for the column. For details, see Section 10.1, “Character Set Support”. COLLATE
is a synonym forCHARSET
. Example:CHARACTER SET
CREATE TABLE t (c CHAR(20) CHARACTER SET utf8 COLLATE utf8_bin);
MySQL 5.6 interprets length specifications in character column definitions in characters. Lengths for BINARY and VARBINARY are in bytes.
- The
DEFAULT
clause specifies a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such asNOW() orCURRENT_DATE. The exception is that you can specifyCURRENT_TIMESTAMP as the default for aTIMESTAMP or (as of MySQL 5.6.5)DATETIME column. SeeSection 11.3.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.
If a column definition includes no explicit
DEFAULT
value, MySQL determines the default value as described inSection 11.6, “Data Type Default Values”.
BLOB andTEXT columns cannot be assigned a default value.
If theNO_ZERO_DATE orNO_ZERO_IN_DATE SQL mode is enabled and a date-valued default is not correct according to that mode,CREATE TABLE produces a warning if strict SQL mode is not enabled and an error if strict mode is enabled. For example, withNO_ZERO_IN_DATE enabled,
produces a warning. (Before MySQL 5.6.6, the statement produces an error even if strict mode is not enabled.)c1 DATE DEFAULT '2010-00-00'
- A comment for a column can be specified with the
option, up to 1024 characters long. The comment is displayed by theSHOW CREATE TABLE andSHOW FULL COLUMNS statements.COMMENT
- In MySQL Cluster, it is also possible to specify a data storage format for individual columns ofNDB tables using
. Permissible column formats areCOLUMN_FORMAT
,FIXED
, andDYNAMIC
.DEFAULT
is used to specify fixed-width storage,FIXED
permits the column to be variable-width, andDYNAMIC
causes the column to use fixed-width or variable-width storage as determined by the column's data type (possibly overridden by aDEFAULT
ROW_FORMAT
specifier).
ForNDB tables, the default value for
isCOLUMN_FORMAT
DEFAULT
.
currently has no effect on columns of tables using storage engines other thanNDB. In MySQL 5.6 and later,COLUMN_FORMAT
is silently ignored.COLUMN_FORMAT
- ForNDB tables, it is also possible to specify whether the column is stored on disk or in memory by using a
clause.STORAGE
causes the column to be stored on disk, andSTORAGE DISK
causes in-memory storage to be used. TheCREATE TABLE statement used must still include aSTORAGE MEMORY
clause:TABLESPACE
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;
For NDB tables,
STORAGE DEFAULT
is equivalent to
STORAGE MEMORY
.
The
STORAGE
clause has no effect on tables using storage engines other than NDB. The
STORAGE
keyword is supported only in the build of mysqld that is supplied with MySQL Cluster; it is not recognized in any other version of MySQL, where any attempt to use the
STORAGE
keyword causes a syntax error.
-
is normally a synonym forKEY
. The key attributeINDEX
can also be specified as justPRIMARY KEY
when given in a column definition. This was implemented for compatibility with other database systems.KEY
- A
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, aUNIQUE
index permits multipleUNIQUE
values for columns that can containNULL
.NULL
- A
is a unique index where all key columns must be defined asPRIMARY KEY
. If they are not explicitly declared asNOT NULL
, MySQL declares them so implicitly (and silently). A table can have only oneNOT NULL
. The name of aPRIMARY KEY
is alwaysPRIMARY KEY
PRIMARY
, which thus cannot be used as the name for any other kind of index.
If you do not have a
and an application asks for thePRIMARY KEY
in your tables, MySQL returns the firstPRIMARY KEY
index that has noUNIQUE
columns as theNULL
PRIMARY KEY
.
In
tables, keep theInnoDB
short to minimize storage overhead for secondary indexes. Each secondary index entry contains a copy of the primary key columns for the corresponding row. (SeeSection 14.8.9, “Clustered and Secondary Indexes”.)PRIMARY KEY
- In the created table, a
is placed first, followed by allPRIMARY KEY
indexes, and then the nonunique indexes. This helps the MySQL optimizer to prioritize which index to use and also more quickly to detect duplicatedUNIQUE
keys.UNIQUE
- A
can be a multiple-column index. However, you cannot create a multiple-column index using thePRIMARY KEY
key attribute in a column specification. Doing so only marks that single column as primary. You must use a separatePRIMARY KEY
clause.PRIMARY KEY(index_col_name, ...)
- If a
orPRIMARY KEY
index consists of only one column that has an integer type, you can also refer to the column asUNIQUE
inSELECT statements._rowid
- In MySQL, the name of a
isPRIMARY KEY
. 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 (PRIMARY
,_2
,_3
) to make it unique. You can see index names for a table using...
. SeeSection 13.7.5.23, “SHOW INDEX Syntax”.SHOW INDEX FROM tbl_name
- Some storage engines permit you to specify an index type when creating an index. The syntax for the
specifier isindex_type
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 and will be removed in a future MySQL release.
index_option
values specify additional options for an index.
USING
is one such option. For details about permissible
index_option
values, see Section 13.1.13, “CREATE INDEX Syntax”.
For more information about indexes, see Section 8.3.1, “How MySQL Uses Indexes”.
- In MySQL 5.6, only the
,InnoDB
, andMyISAM
storage engines support indexes on columns that can haveMEMORY
values. In other cases, you must declare indexed columns asNULL
or an error results.NOT NULL
- ForCHAR,VARCHAR,BINARY, andVARBINARY columns, indexes can be created that use only the leading part of column values, using
syntax to specify an index prefix length.BLOB andTEXT columns also can be indexed, but a prefix lengthmustbe 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 col_name(length)
characters of each column value forCHAR,VARCHAR, andTEXT columns, and the first length
bytes of each column value forBINARY,VARBINARY, andBLOB columns. Indexing only a prefix of column values like this can make the index file much smaller. For additional information about index prefixes, seeSection 13.1.13, “CREATE INDEX Syntax”.Only thelength
andInnoDB
storage engines support indexing onBLOB andTEXT columns. For example:MyISAM
CREATE TABLE test (blob_col BLOB, INDEX(blob_col(10)));
Prefixes can be up to 767 bytes long for
InnoDB
tables or 3072 bytes if the innodb_large_prefix option is enabled.
Note
Prefix limits are measured in bytes, whereas the prefix length in CREATE TABLE, ALTER TABLE, and CREATE INDEX statements is interpreted as number of characters for nonbinary string types (CHAR, VARCHAR, TEXT) and number of bytes for binary string types (BINARY, VARBINARY, BLOB). Take this into account when specifying a prefix length for a nonbinary string column that uses a multibyte character set.
- An
specification can end withindex_col_name
orASC
. 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.DESC
- When you use
orORDER BY
on a column in aSELECT, the server sorts values using only the initial number of bytes indicated by themax_sort_length system variable.GROUP BY
- You can create special
indexes, which are used for full-text searches. Only theInnoDB andMyISAM storage engines supportFULLTEXT
indexes. They can be created only fromCHAR,VARCHAR, andTEXT columns. Indexing always happens over the entire column; column prefix indexing is not supported and any prefix length is ignored if specified. SeeSection 12.9, “Full-Text Search Functions”, for details of operation. AFULLTEXT
clause can be specified as anWITH PARSER
value to associate a parser plugin with the index if full-text indexing and searching operations need special handling. This clause is valid only forindex_option
indexes. SeeSection 24.2, “The MySQL Plugin API”, for details on creating plugins.FULLTEXT
- You can create
indexes on spatial data types. Spatial types are supported only forSPATIAL
tables and indexed columns must be declared asMyISAM
. SeeSection 11.5, “Extensions for Spatial Data”.NOT NULL
- In MySQL 5.6, index definitions can include an optional comment of up to 1024 characters.
- InnoDB andNDB tables support checking of foreign key constraints. The columns of the referenced table must always be explicitly named. Both
andON DELETE
actions on foreign keys are supported. For more detailed information and examples, seeSection 13.1.17.3, “Using FOREIGN KEY Constraints”. For information specific to foreign keys inON UPDATE
InnoDB
, seeSection 14.8.7, “InnoDB and FOREIGN KEY Constraints”.
For other storage engines, MySQL Server parses and ignores the
andFOREIGN KEY
syntax inCREATE TABLE statements. TheREFERENCES
CHECK
clause is parsed but ignored by all storage engines. SeeSection 1.7.2.3, “Foreign Key Differences”.
ImportantFor users familiar with the ANSI/ISO SQL Standard, please note that no storage engine, including
, recognizes or enforces theInnoDB
clause used in referential integrity constraint definitions. Use of an explicitMATCH
clause will not have the specified effect, and also causesMATCH
andON DELETE
clauses to be ignored. For these reasons, specifyingON UPDATE
MATCH
should be avoided.
The
clause in the SQL standard controls howMATCH
values in a composite (multiple-column) foreign key are handled when comparing to a primary key.NULL
essentially implements the semantics defined byInnoDB
, which permit a foreign key to be all or partiallyMATCH SIMPLE
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, it does not enforce any requirement that the referenced columns be declared
orUNIQUE
. The handling of foreign key references to nonunique keys or keys that containNOT NULL
values is not well defined for operations such asNULL
orUPDATE
. You are advised to use foreign keys that reference only keys that are bothDELETE CASCADE
(orUNIQUE
) andPRIMARY
NOT NULL
.
MySQL parses but ignores “inline
specifications” (as defined in the SQL standard) where the references are defined as part of the column specification. MySQL acceptsREFERENCES
clauses only when specified as part of a separateREFERENCES
FOREIGN KEY
specification.
NotePartitioned tables employing theInnoDB storage engine do not support foreign keys.NDB tables that are partitioned by
orKEY
are not affected by this restriction. SeeSection 19.6, “Restrictions and Limitations on Partitioning”, for more information.LINEAR KEY
- 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 inSection C.10.4, “Limits on Table Column Count and Row Size”.
The
TABLESPACE
and
STORAGE
table options are employed only with NDB tables. The tablespace named
tablespace_name
must already have been created using CREATE TABLESPACE.
STORAGE
determines the type of storage used (disk or memory), and can be one of
DISK
,
MEMORY
, or
DEFAULT
.
TABLESPACE ... STORAGE DISK
assigns a table to a MySQL Cluster Disk Data tablespace. See Section 18.5.12, “MySQL Cluster Disk Data Tables”, for more information.
Important
A
STORAGE
clause cannot be used in a CREATE TABLE statement without a
TABLESPACE
clause.
Storage Engines
The
ENGINE
table option 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 |
| Transaction-safe tables with row locking and foreign keys. The default storage engine for new tables. See Chapter 14, The InnoDB Storage Engine, and in particular Section 14.1, “Introduction to InnoDB” if you have MySQL experience but are new to . |
| The binary portable storage engine that is primarily used for read-only or read-mostly workloads. See Section 15.2, “The MyISAM Storage Engine”. |
| The data for this storage engine is stored only in memory. See Section 15.3, “The MEMORY Storage Engine”. |
| Tables that store rows in comma-separated values format. See Section 15.4, “The CSV Storage Engine”. |
| The archiving storage engine. See Section 15.5, “The ARCHIVE Storage Engine”. |
| An example engine. See Section 15.9, “The EXAMPLE Storage Engine”. |
| Storage engine that accesses remote tables. See Section 15.8, “The FEDERATED Storage Engine”. |
| This is a synonym for . |
| A collection of tables used as one table. Also known as . See Section 15.7, “The MERGE Storage Engine”. |
NDB | Clustered, fault-tolerant, memory-based tables, supporting transactions and foreign keys. Also known as NDBCLUSTER. See Chapter 18, MySQL Cluster NDB 7.3 and MySQL Cluster NDB 7.4. |
If a storage engine is specified that is not available, MySQL uses the default engine instead. Normally, this is
MyISAM
. For example, if a table definition includes the
ENGINE=INNODB
option but the MySQL server does not support
INNODB
tables, the table is created as a
MyISAM
table. This makes it possible to have a replication setup where you have transactional tables on the master but tables created on the slave are nontransactional (to get more speed). In MySQL 5.6, a warning occurs if the storage engine specification is not honored.
Engine substitution can be controlled by the setting of the NO_ENGINE_SUBSTITUTION SQL mode, as described in Section 5.1.7, “Server SQL Modes”.
Note
The older
TYPE
option that was synonymous with
ENGINE
was removed in MySQL 5.5. When upgrading to MySQL 5.5 or later, you must convert existing applications that rely on
TYPE
to use
ENGINE
instead.
Optimizing Performance
The other 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 ALTER TABLE to convert the table to use a different storage engine.
-
The initialAUTO_INCREMENT
value for the table. In MySQL 5.6, this works for AUTO_INCREMENT
, MyISAM
, MEMORY
, and InnoDB
tables. To set the first auto-increment value for engines that do not support the ARCHIVE
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
table option in CREATE TABLE statements, you can also use AUTO_INCREMENT
to reset the ALTER TABLE tbl_name AUTO_INCREMENT = N
value. The value cannot be set lower than the maximum value currently in the column.AUTO_INCREMENT
-
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
table, MySQL uses the product of the MyISAM
and MAX_ROWS
options to decide how big the resulting table is. If you don't specify either option, the maximum size for AVG_ROW_LENGTH
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 Section 5.1.4, “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.MyISAM
-
Specify a default character set for the table.[DEFAULT] CHARACTER SET
is a synonym for CHARSET
. If the character set name is CHARACTER SET
, the database character set is used.DEFAULT
-
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. TheCHECKSUM TABLE statement reports the checksum. (CHECKSUM
only.)MyISAM
-
Specify a default collation for the table.[DEFAULT] COLLATE
-
A comment for the table, up to 2048 characters long.COMMENT
-
The connection string for aCONNECTION
FEDERATED
table.
NoteOlder versions of MySQL used a
option for the connection string.COMMENT
-
, DATA DIRECTORY
ForINDEX DIRECTORY
, the InnoDB
option allows you to create DATA DIRECTORY='directory'
file-per-table tablespaces outside the MySQL data directory. Within the directory that you specify, MySQL creates a subdirectory corresponding to the database name, and within that a InnoDB
file for the table. The innodb_file_per_table configuration option must be enabled to use the .ibd
option with DATA DIRECTORY
InnoDB
. The full directory path must be specified. See Section 14.7.5, “Creating a File-Per-Table Tablespace Outside the Data Directory” for more information.
When creating
tables, you can use the MyISAM
clause, the DATA DIRECTORY='directory'
clause, or both. They specify where to put a INDEX DIRECTORY='directory'
table's data file and index file, respectively. Unlike MyISAM
tables, MySQL does not create subdirectories that correspond to the database name when creating a InnoDB
table with a MyISAM
or DATA DIRECTORY
INDEX DIRECTORY
option. Files are created in the directory that is specified.
ImportantTable-level
and DATA DIRECTORY
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 Section 8.12.4.2, “Using Symbolic Links for MyISAM Tables on Unix”, for more complete information.
If a
table is created with no MyISAM
option, the DATA DIRECTORY
file is created in the database directory. By default, if .MYD
finds an existing MyISAM
file in this case, it overwrites it. The same applies to .MYD
files for tables created with no .MYI
option. To suppress this behavior, start the server with the --keep_files_on_create option, in which case INDEX DIRECTORY
MyISAM
will not overwrite existing files and returns an error instead.
If a
table is created with a MyISAM
or DATA DIRECTORY
option and an existing INDEX DIRECTORY
or .MYD
.MYI
file is found, MyISAM always returns an error. It will not overwrite a file in the specified directory.
ImportantYou cannot use path names that contain the MySQL data directory with
or DATA DIRECTORY
. This includes partitioned tables and individual table partitions. (See Bug #32167.)INDEX DIRECTORY
-
Set this to 1 if you want to delay key updates for the table until the table is closed. See the description of thedelay_key_write system variable in Section 5.1.4, “Server System Variables”. (DELAY_KEY_WRITE
only.)MyISAM
-
If you want to insert data into aINSERT_METHOD
table, you must specify with MERGE
the table into which the row should be inserted. INSERT_METHOD
is an option useful for INSERT_METHOD
tables only. Use a value of MERGE
or FIRST
to have inserts go to the first or last table, or a value of LAST
to prevent inserts. See Section 15.7, “The MERGE Storage Engine”.NO
-
ForMyISAM 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
KEY_BLOCK_SIZE
value.
ForInnoDB tables,
optionally specifies the page size (in kilobytes) to use for compressed KEY_BLOCK_SIZE
tables. The InnoDB
value is treated as a hint; a different size could be used by KEY_BLOCK_SIZE
if necessary.InnoDB
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, possibleKEY_BLOCK_SIZE
KEY_BLOCK_SIZE
values include 0, 1, 2, 4, 8, and 16. See InnoDB Table Compression for more information.
Oracle recommends enablinginnodb_strict_mode when specifying
for KEY_BLOCK_SIZE
tables. When innodb_strict_mode is enabled, specifying an invalid InnoDB
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
KEY_BLOCK_SIZE
option is ignored.
only supports InnoDB
at the table level.KEY_BLOCK_SIZE
-
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.
TheNDB storage engine treats this value as a maximum. If you plan to create very large MySQL Cluster tables (containing millions of rows), you should use this option to insure that NDB allocates sufficient number of index slots in the hash table used for storing hashes of the table's primary keys by setting
, where MAX_ROWS = 2 * rows
rows
The maximum
value is 4294967295; larger values are truncated to this limit.MAX_ROWS
-
The minimum number of rows you plan to store in the table. TheMEMORY storage engine uses this option as a hint about memory use.MIN_ROWS
-
PACK_KEYS
takes effect only with PACK_KEYS
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 MyISAM
DEFAULT
tells the storage engine to pack only long CHAR, VARCHAR, BINARY, or VARBINARY columns.
If you do not use
, the default is to pack strings, but not numbers. If you use PACK_KEYS
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
.)
-
This option is unused. If you have a need to scramble yourPASSWORD
files and make them unusable to any other MySQL server, please contact our sales department..frm
-
ROW_FORMAT
Defines the physical format in which the rows are stored. The choices differ depending on the storage engine used for the table.
For
tables:InnoDB
- Rows are stored in compact format (
) by default.ROW_FORMAT=COMPACT
- The noncompact format used in older versions of MySQL can still be requested by specifying
.ROW_FORMAT=REDUNDANT
- To enable compression for
tables, specify InnoDB
and follow the procedures in Section 14.9, “InnoDB Table Compression”.ROW_FORMAT=COMPRESSED
- For more efficient
storage of data types, especially BLOB types, specify InnoDB
and follow the procedures in Section 14.11.3, “DYNAMIC and COMPRESSED Row Formats”. Both the ROW_FORMAT=DYNAMIC
and COMPRESSED
row formats require creating the table with the configuration settings innodb_file_per_table=1 and innodb_file_format=barracuda.DYNAMIC
- When you specify a non-default
clause, consider also enabling the innodb_strict_mode configuration option.ROW_FORMAT
-
is not supported. If ROW_FORMAT=FIXED
is specified while innodb_strict_mode is disabled, ROW_FORMAT=FIXED
issues a warning and assumes InnoDB
. If ROW_FORMAT=COMPACT
is specified whileinnodb_strict_mode is enabled, ROW_FORMAT=FIXED
returns an error.InnoDB
- For additional information about
row formats, see Section 14.11, “InnoDB Row Storage and Row Formats”.InnoDB
For
MyISAM
tables, the option value can be
FIXED
or
DYNAMIC
for static or variable-length row format. myisampack sets the type to
COMPRESSED
. See Section 15.2.3, “MyISAM Table Storage Formats”.
Note
When executing a CREATE TABLE statement, if you specify a row format that is not supported by the storage engine that is used for the table, the table is created using that storage engine's default row format. The information reported in this column in response to SHOW TABLE STATUS is the actual row format used. This may differ from the value in the
Create_options
column because the original CREATE TABLE definition is retained during creation.
-
Specifies whether to automatically recalculatepersistent statistics for an STATS_AUTO_RECALC
table. The value InnoDB
causes the persistent statistics setting for the table to be determined by the innodb_stats_auto_recalc configuration option. The value DEFAULT
causes statistics to be recalculated when 10% of the data in the table has changed. The value 1
prevents automatic recalculation for this table; with this setting, issue an ANALYZE TABLE statement to recalculate the statistics after making substantial changes to the table. For more information about the persistent statistics feature, see Section 14.6.11.1, “Configuring Persistent Optimizer Statistics Parameters”.0
-
Specifies whether to enablepersistent statistics for an STATS_PERSISTENT
table. The value InnoDB
causes the persistent statistics setting for the table to be determined by the innodb_stats_persistent configuration option. The value DEFAULT
enables persistent statistics for the table, while the value 1
turns off this feature. After enabling persistent statistics through a 0
or CREATE TABLE
statement, issue an ANALYZE TABLE statement to calculate the statistics, after loading representative data into the table. For more information about the persistent statistics feature, see Section 14.6.11.1, “Configuring Persistent Optimizer Statistics Parameters”.ALTER TABLE
-
The number of index pages to sample when estimating cardinality and other statistics for an indexed column, such as those calculated byANALYZE TABLE. For more information, see Section 14.6.11.1, “Configuring Persistent Optimizer Statistics Parameters”.STATS_SAMPLE_PAGES
- UNIONUNION is used when you want to access a collection of identical
tables as one. This works only with MyISAM
MERGE
tables. See Section 15.7, “The MERGE Storage Engine”.
You must haveSELECT, UPDATE, and DELETE privileges for the tables you map to a
MERGE
table.
NoteFormerly, all tables used had to be in the same database as the
table itself. This restriction no longer applies.MERGE
Creating Partitioned Tables
partition_options
can be used to control partitioning of the table created with CREATE TABLE.
Important
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 Chapter 19, 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.
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
-
: Hashes one or more columns to create a key for placing and locating rows. HASH(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 CREATE TABLE statements using expr
: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 Section 19.2.4, “HASH Partitioning”.
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 Section 19.2.4.1, “LINEAR HASH Partitioning”.
-
: This is similar to KEY(column_list)
, except that MySQL supplies the hashing function so as to guarantee an even data distribution. The HASH
column_list
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 Section 19.2.4.1, “LINEAR HASH Partitioning”, and Section 19.2.5, “KEY Partitioning”, 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
beginning with MySQL 5.6.11.
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 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. For more information, see Section 13.1.7.1, “ALTER TABLE Partition Operations”.
mysqldump in MySQL 5.6.11 and later 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. If you plan to load a dump made on a MySQL 5.5.31 or later MySQL 5.5 server where you use tables that are partitioned or subpartitioned by
KEY
into a MySQL 5.6 server previous to version 5.6.11, be sure to consult Section 2.11.1.1, “Changes Affecting Upgrades to MySQL 5.6”, before proceeding. (The information found there also applies if you are loading a dump containing
KEY
partitioned or subpartitioned tables made from a MySQL 5.6.11 or later server into a MySQL 5.5.30 or earlier server.)
Also in MySQL 5.6.11 and later,
ALGORITHM=1
is shown when necessary in the output of SHOW CREATE TABLE using versioned comments in the same manner as 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
.
-
: In this case, RANGE(expr)
shows a range of values using a set of expr
operators. When using range partitioning, you must define at least one partition using VALUES LESS THAN
. You cannot use VALUES LESS THAN
VALUES IN
with range partitioning.
NoteFor tables partitioned by
, RANGE
must be used with either an integer literal value or an expression that evaluates to a single integer value. In MySQL 5.6, you can overcome this limitation in a table that is defined using VALUES LESS THAN
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: |
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 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.
-
: This variant on RANGE COLUMNS(column_list)
facilitates partition pruning for queries using range conditions on multiple columns (that is, having conditions such as RANGE
or WHERE a = 1 AND b < 10
). It enables you to specify value ranges in multiple columns by using a list of columns in the WHERE a = 1 AND b = 10 AND c < 10
clause and a set of column values in each COLUMNS
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 PARTITION ... VALUES LESS THAN (value_list)
and column_list
The value_list
used in the column_list
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 usingCOLUMNS
, BLOB
, TEXT
, SET
, ENUM
, 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 BIT
COLUMNS
clause.
The
clause used in a partition definition must specify a literal value for each column that appears in the VALUES LESS THAN
clause; that is, the list of values used for each COLUMNS()
clause must contain the same number of values as there are columns listed in the VALUES LESS THAN
clause. An attempt to use more or fewer values in a COLUMNS
clause than there are in the VALUES LESS THAN
clause causes the statement to fail with the error Inconsistency in usage of column lists for partitioning.... You cannot use COLUMNS
for any value appearing in NULL
. It is possible to use VALUES LESS THAN
more than once for a given column other than the first, as shown in this example:MAXVALUE
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 (MAXVALUE,15),
PARTITION p3 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 Section 19.2.1, “RANGE Partitioning”, and Section 19.4, “Partition Pruning”.
-
: 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 LIST(expr)
, except that only RANGE
VALUES IN
may be used to specify permissible values for each partition.
is used with a list of values to be matched. For instance, you could create a partitioning scheme such as the following:VALUES IN
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.6, you can overcome this limitation using partitioning by
LIST COLUMNS
, which is described later in this section.
-
: This variant on LIST COLUMNS(column_list)
facilitates partition pruning for queries using comparison conditions on multiple columns (that is, having conditions such as LIST
or WHERE a = 5 AND b = 5
). It enables you to specify values in multiple columns by using a list of columns in the WHERE a = 1 AND b = 10 AND c = 5
clause and a set of column values in each COLUMNS
PARTITION ... VALUES IN (value_list)
partition definition clause.
The rules governing regarding data types for the column list used in
and the value list used in LIST COLUMNS(column_list)
are the same as those for the column list used in VALUES IN(value_list)
and the value list used in RANGE COLUMNS(column_list)
, respectively, except that in the VALUES LESS THAN(value_list)
clause, VALUES IN
is not permitted, and you may use MAXVALUE
NULL
.
There is one important difference between the list of values used for
with VALUES IN
as opposed to when it is used with PARTITION BY LIST COLUMNS
. When used with PARTITION BY LIST
, each element in the PARTITION BY LIST COLUMNS
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 VALUES IN
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 COLUMNS
and in the elements making up the column_list
The table defined by the followingvalue_list
statement provides an example of a table using CREATE TABLE
partitioning:LIST COLUMNS
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) )
);
- The number of partitions may optionally be specified with a
clause, where PARTITIONS num
is the number of partitions. If both this clause and any num
clauses are used, PARTITION
must be equal to the total number of any partitions that are declared using num
PARTITION
clauses.
NoteWhether or not you use a
clause in creating a table that is partitioned by PARTITIONS
or RANGE
, you must still include at least one LIST
clause in the table definition (see below).PARTITION VALUES
- A partition may optionally be divided into a number of subpartitions. This can be indicated by using the optional
clause. Subpartitioning may be done by SUBPARTITION BY
or HASH
. Either of these may be KEY
. These work in the same way as previously described for the equivalent partitioning types. (It is not possible to subpartition by LINEAR
or LIST
RANGE
.)
The number of subpartitions can be indicated using the
keyword followed by an integer value.SUBPARTITIONS
- Rigorous checking of the value used in
or PARTITIONS
clauses is applied and this value must adhere to the following rules:SUBPARTITIONS
- 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,
is not permitted, even though PARTITIONS 0.2E+01
evaluates to 0.2E+01
. (Bug #15890)2
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)
Each partition may be individually defined using a
partition_definition
-
: This specifies a logical name for the partition.PARTITION partition_name
- A
clause: For range partitioning, each partition must include a VALUES
clause; for list partitioning, you must specify a VALUES LESS THAN
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 Chapter 19, Partitioning, for syntax examples.VALUES IN
- An optional
clause may be used to specify a string that describes the partition. Example:COMMENT
COMMENT = 'Data for the years previous to 1999'
Beginning with MySQL 5.6.6, the maximum length for a partition comment is 1024 characters. (Previously, this limit was not explicitly defined.)
-
and DATA DIRECTORY
may be used to indicate the directory where, respectively, the data and indexes for this partition are to be stored. Both the INDEX DIRECTORY
and the data_dir
must be absolute system path names. Example:index_dir
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 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 MyISAM tables, and the
INDEX DIRECTORY
option is not supported for individual partitions or subpartitions of InnoDB 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)
-
andMAX_ROWS
may be used to specify, respectively, the maximum and minimum number of rows to be stored in the partition. The values for MIN_ROWS
and max_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.min_number_of_rows
- The optional
clause may be used to designate a tablespace for the partition. Used for MySQL Cluster only.TABLESPACE
- The partitioning handler accepts a
option for both[STORAGE] ENGINE
andPARTITION
. 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 will give rise to 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.SUBPARTITION
- The partition definition may optionally contain one or more
clauses. Each of these consists at a minimum of thesubpartition_definition
, where SUBPARTITION name
is an identifier for the subpartition. Except for the replacement of thename
keyword withPARTITION
SUBPARTITION
, the syntax for a subpartition definition is identical to that for a partition definition.
Subpartitioning must be done by
orHASH
, and can be done only onKEY
orRANGE
partitions. SeeSection 19.2.6, “Subpartitioning”.LIST
Partitions can be modified, merged, added to tables, and dropped from tables. For basic information about the MySQL statements to accomplish these tasks, see Section 13.1.7, “ALTER TABLE Syntax”. For more detailed descriptions and examples, see Section 19.3, “Partition Management”.
Important
The original CREATE TABLE statement, including all specifications and table options are stored by MySQL when the table is created. The information is retained so that if you change storage engines, collations or other settings using an ALTER TABLE statement, the original table options specified are retained. This enables you to change between InnoDB and MyISAM table types even though the row formats supported by the two engines are different.
Because the text of the original statement is retained, but due to the way that certain values and options may be silently reconfigured (such as the
ROW_FORMAT
), the active table definition (accessible through DESCRIBE or with SHOW TABLE STATUS) and the table creation string (accessible through SHOW CREATE TABLE) will report different values.
http://dev.mysql.com/doc/refman/5.6/en/create-table.html