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
{ LIKEold_tbl_name
| (LIKEold_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_constraint_definition
}column_definition
: {data_type
[NOT NULL | NULL] [DEFAULT {literal
| (expr
)} ] [VISIBLE | INVISIBLE] [AUTO_INCREMENT] [UNIQUE [KEY]] [[PRIMARY] KEY] [COMMENT 'string
'] [COLLATEcollation_name
] [COLUMN_FORMAT {FIXED | DYNAMIC | DEFAULT}] [ENGINE_ATTRIBUTE [=] 'string
'] [SECONDARY_ENGINE_ATTRIBUTE [=] 'string
'] [STORAGE {DISK | MEMORY}] [reference_definition
] [check_constraint_definition
] |data_type
[COLLATEcollation_name
] [GENERATED ALWAYS] AS (expr
) [VIRTUAL | STORED] [NOT NULL | NULL] [VISIBLE | INVISIBLE] [UNIQUE [KEY]] [[PRIMARY] KEY] [COMMENT 'string
'] [reference_definition
] [check_constraint_definition
] }data_type
: (see Chapter 11, Data Types)key_part
: {col_name
[(length
)] | (expr
)} [ASC | DESC]index_type
: USING {BTREE | HASH}index_option
: { KEY_BLOCK_SIZE [=]value
|index_type
| WITH PARSERparser_name
| COMMENT 'string
' | {VISIBLE | INVISIBLE} |ENGINE_ATTRIBUTE
[=] 'string
' |SECONDARY_ENGINE_ATTRIBUTE
[=] 'string
' }check_constraint_definition
: [CONSTRAINT [symbol
]] CHECK (expr
) [[NOT] ENFORCED]reference_definition
: REFERENCEStbl_name
(key_part
,...) [MATCH FULL | MATCH PARTIAL | MATCH SIMPLE] [ON DELETEreference_option
] [ON UPDATEreference_option
]reference_option
: RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULTtable_options
:table_option
[[,]table_option
] ...table_option
: { AUTOEXTEND_SIZE [=]value
| 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
| ENGINE_ATTRIBUTE [=] 'string
' | 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} | SECONDARY_ENGINE_ATTRIBUTE [=] 'string
' | STATS_AUTO_RECALC [=] {DEFAULT | 0 | 1} | STATS_PERSISTENT [=] {DEFAULT | 0 | 1} | STATS_SAMPLE_PAGES [=]value
| TABLESPACEtablespace_name
[STORAGE {DISK | MEMORY}] | 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
)} } [PARTITIONSnum
] [SUBPARTITION BY { [LINEAR] HASH(expr
) | [LINEAR] KEY [ALGORITHM={1 | 2}] (column_list
) } [SUBPARTITIONSnum
] ] [(partition_definition
[,partition_definition
] ...)]partition_definition
: PARTITIONpartition_name
[VALUES {LESS THAN {(expr
|value_list
) | MAXVALUE} | IN (value_list
)}] [[STORAGE] ENGINE [=]engine_name
] [COMMENT [=] 'string
' ] [DATA DIRECTORY [=] ''] [INDEX DIRECTORY [=] '
data_dir
'] [MAX_ROWS [=]
index_dir
max_number_of_rows
] [MIN_ROWS [=]min_number_of_rows
] [TABLESPACE [=] tablespace_name] [(subpartition_definition
[,subpartition_definition
] ...)]subpartition_definition
: SUBPARTITIONlogical_name
[[STORAGE] ENGINE [=]engine_name
] [COMMENT [=] 'string
' ] [DATA DIRECTORY [=] ''] [INDEX DIRECTORY [=] '
data_dir
'] [MAX_ROWS [=]
index_dir
max_number_of_rows
] [MIN_ROWS [=]min_number_of_rows
] [TABLESPACE [=] tablespace_name]query_expression:
SELECT ... (Some valid select or union statement
)
CREATE TABLE
creates a table with the given name. CREATE TABLE
创建具有给定名称的表。You must have the 您必须具有表的CREATE
privilege for the table.CREATE
权限。
By default, tables are created in the default database, using the 默认情况下,使用InnoDB
storage engine. InnoDB
存储引擎在默认数据库中创建表。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. MySQL对表的数量没有限制。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.InnoDB
允许多达40亿个表。
For information about the physical representation of a table, see Section 13.1.20.1, “Files Created by CREATE TABLE”.有关表格物理表示的信息,请参阅第13.1.20.1节,“创建表格创建的文件”。
There are several aspects to the CREATE TABLE
statement, described under the following topics in this section:CREATE TABLE
语句有几个方面,在本节的以下主题下进行了描述:
tbl_name
The table name can be specified as 可以将表名指定为db_name.tbl_name
to create the table in a specific database. db_name.tbl_name
,以便在特定数据库中创建表。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`
.`mydb`.`mytbl`
,而不是`mydb.mytbl`
。
Rules for permissible table names are given in Section 9.2, “Schema Object Names”.第9.2节,“模式对象名称”中给出了允许表名称的规则。
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 但是,无法验证现有表是否具有与CREATE TABLE
statement.CREATE TABLE
语句所指示的结构相同的结构。
You can use the 创建表时可以使用TEMPORARY
keyword when creating a table. TEMPORARY
关键字。A TEMPORARY
table is visible only within the current session, and is dropped automatically when the session is closed. TEMPORARY
表仅在当前会话中可见,并在会话关闭时自动删除。For more information, see Section 13.1.20.2, “CREATE TEMPORARY TABLE Statement”.有关更多信息,请参阅第13.1.20.2节,“创建临时表语句”。
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 ... LIKE
以基于另一个表的定义创建空表,包括原始表中定义的任何列属性和索引:
CREATE TABLEnew_tbl
LIKEorig_tbl
;
For more information, see Section 13.1.20.3, “CREATE TABLE ... LIKE Statement”.有关更多信息,请参阅第13.1.20.3节,“CREATE TABLE ... LIKE语句”。
[AS]
query_expression
To create one table from another, add a 要从一个表创建另一个表,请在create table语句末尾添加SELECT语句:SELECT
statement at the end of the CREATE TABLE
statement:
CREATE TABLEnew_tbl
AS SELECT * FROMorig_tbl
;
For more information, see Section 13.1.20.4, “CREATE TABLE ... SELECT Statement”.有关更多信息,请参阅第13.1.20.4节,“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 SELECT
statement.IGNORE
和REPLACE
选项指示在使用SELECT
语句复制表时如何处理重复唯一键值的行。
For more information, see Section 13.1.20.4, “CREATE TABLE ... SELECT Statement”.有关更多信息,请参阅第13.1.20.4节,“CREATE TABLE ... SELECT语句”。
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 Section 8.4.7, “Limits on Table Column Count and Row Size”.每个表有4096列的硬限制,但给定表的有效最大值可能较小,取决于第8.4.7节,“表列计数和行大小限制”中讨论的因素。
data_type
data_type
represents the data type in a column definition. data_type
表示列定义中的数据类型。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.有关可用于指定列数据类型的语法的完整描述,以及有关每种类型的属性的信息,请参阅第11章,“数据类型”。
Some attributes do not apply to all data types. 某些属性并不适用于所有数据类型。AUTO_INCREMENT
applies only to integer and floating-point types. AUTO_INCREMENT
仅适用于整数和浮点类型。Prior to MySQL 8.0.13, 在MySQL 8.0.13之前,DEFAULT
does not apply to the BLOB
, TEXT
, GEOMETRY
, and JSON
types.DEFAULT
不适用于BLOB
、TEXT
、GEOMETRY
和JSON
类型。
Character data types (字符数据类型(CHAR
, VARCHAR
, the TEXT
types, ENUM
, SET
, and any synonyms) can include CHARACTER SET
to specify the character set for the column. CHAR
、VARCHAR
、TEXT
类型、ENUM
、SET
和任何同义词)可以包括字符集,以指定列的字符集。CHARSET
is a synonym for CHARACTER SET
. CHARSET
是CHARACTER SET
的同义词。A collation for the character set can be specified with the 可以使用COLLATE
attribute, along with any other attributes. COLLATE
属性以及任何其他属性指定字符集的排序规则。For details, see Chapter 10, Character Sets, Collations, Unicode. 有关详细信息,请参阅第10章,“字符集、排序规则和Unicode”。Example:例子:
CREATE TABLE t (c CHAR(20) CHARACTER SET utf8 COLLATE utf8_bin);
MySQL 8.0 interprets length specifications in character column definitions in characters. MySQL 8.0以字符解释字符列定义中的长度规范。Lengths for BINARY
and VARBINARY
are in bytes.BINARY
和VARBINARY
的长度以字节为单位。
For 对于CHAR
, VARCHAR
, BINARY
, and VARBINARY
columns, indexes can be created that use only the leading part of column values, using
syntax to specify an index prefix length. col_name
(length
)CHAR
、VARCHAR
、BINARY
和VARBINARY
列,可以创建只使用列值的前导部分的索引,使用
语法指定索引前缀长度。col_name
(length
)BLOB
and TEXT
columns also can be indexed, but a prefix length must be given. BLOB
和TEXT
列也可以编制索引,但必须指定前缀长度。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 CHAR
, VARCHAR
, and TEXT
columns, and the first length
bytes of each column value for BINARY
, VARBINARY
, and BLOB
columns. CHAR
、VARCHAR
和TEXT
列的每个列值的第一个length
字符,以及BINARY
、VARBINARY
和BLOB
列的每个列值的第一个length
字节。Indexing only a prefix of column values like this can make the index file much smaller. 仅对这样的列值的前缀进行索引可以使索引文件小得多。For additional information about index prefixes, see Section 13.1.15, “CREATE INDEX Statement”.有关索引前缀的更多信息,请参阅第13.1.15节,“CREATE INDEX语句”。
Only the 只有InnoDB
and MyISAM
storage engines support indexing on BLOB
and TEXT
columns.InnoDB
和MyISAM
存储引擎支持对BLOB和文本列进行索引。For example:例如:
CREATE TABLE test (blob_col BLOB, INDEX(blob_col(10)));
If a specified index prefix exceeds the maximum column data type size, 如果指定的索引前缀超过最大列数据类型大小,则CREATE TABLE
handles the index as follows:CREATE TABLE
将按如下方式处理索引:
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).对于非唯一索引,要么发生错误(如果启用了严格SQL模式),要么索引长度减小到最大列数据类型大小范围内,并生成警告(如果未启用严格SQL模式)。
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.对于唯一索引,无论SQL模式如何,都会发生错误,因为减少索引长度可能会导致插入不符合指定唯一性要求的非唯一项。
无法为JSON
columns cannot be indexed. JSON
列编制索引。You can work around this restriction by creating an index on a generated column that extracts a scalar value from the 您可以在生成的列上创建一个索引,从JSON
column. JSON
列中提取标量值,从而绕过此限制。See Indexing a Generated Column to Provide a JSON Column Index, for a detailed example.有关详细示例,请参阅为生成的列编制索引以提供JSON列索引。
NOT NULL | NULL
If neither 如果既不指定NULL
nor NOT NULL
is specified, the column is treated as though NULL
had been specified.NULL
也不指定NOT NULL
,则该列将被视为已指定NULL
。
In MySQL 8.0, only the 在MySQL 8.0中,只有InnoDB
, MyISAM
, and MEMORY
storage engines support indexes on columns that can have NULL
values. InnoDB
、MyISAM
和MEMORY
存储引擎支持可以有NULL
值的列上的索引。In other cases, you must declare indexed columns as 在其他情况下,必须将索引列声明为NOT NULL
or an error results.NOT NULL
否则会产生错误。
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 Section 11.6, “Data Type Default Values”.DEFAULT
的情况,请参阅第11.6节,“数据类型默认值”。
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, CREATE TABLE
produces a warning if strict SQL mode is not enabled and an error if strict mode is enabled. NO_ZERO_DATE
或NO_ZERO_IN_DATE
SQL模式,并且根据该模式,日期值默认值不正确,则如果未启用严格SQL模式,CREATE TABLE
将生成警告;如果启用严格模式,CREATE TABLE
将生成错误。For example, with 例如,如果启用了NO_ZERO_IN_DATE
enabled, c1 DATE DEFAULT '2010-00-00'
produces a warning.NO_ZERO_IN_DATE
,c1 DATE DEFAULT '2010-00-00'
将生成警告。
VISIBLE
, INVISIBLE
Specify column visibility. 指定列可见性。The default is 如果两个关键字都不存在,则默认值是VISIBLE
if neither keyword is present. VISIBLE
。A table must have at least one visible column. 表必须至少有一个可见列。Attempting to make all columns invisible produces an error. 试图使所有列不可见会产生错误。For more information, see Section 13.1.20.10, “Invisible Columns”.有关更多信息,请参阅第13.1.20.10节,“不可见列”。
The VISIBLE
and INVISIBLE
keywords are available as of MySQL 8.0.23. VISIBLE
关键字和INVISIBLE
关键字从MySQL 8.0.23开始提供。Prior to MySQL 8.0.23, all columns are visible.在MySQL 8.0.23之前,所有列都是可见的。
AUTO_INCREMENT
An integer or floating-point column can have the additional attribute 整数或浮点列可以具有附加属性AUTO_INCREMENT
. 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. NULL
(建议)或0
值时,该列将设置为下一个序列值。Typically this is 通常这是
, where value
+1value
is the largest value for the column currently in the table.
,其中值是表中当前列的最大值。value
+1AUTO_INCREMENT
sequences begin with 1
.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. AUTO_INCREMENT
值,请使用SQL函数LAST_INSERT_ID()
或C API函数mysql_insert_id()
。See Section 12.16, “Information Functions”, and mysql_insert_id().请参阅第12.16节,“信息函数”和mysql_insert_id()
。
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. NO_AUTO_VALUE_ON_ZERO
SQL 模式,则可以在AUTO_INCREMENT
列中将0
存储为0
,而无需生成新的序列值。See Section 5.1.11, “Server SQL Modes”.请参阅第5.1.11节,“服务器SQL模式”。
There can be only one 每个表只能有一个AUTO_INCREMENT
column per table, it must be indexed, and it cannot have a DEFAULT
value. AUTO_INCREMENT
列,它必须被索引,并且不能有DEFAULT
值。An 仅当AUTO_INCREMENT
column works properly only if it contains only positive values. AUTO_INCREMENT
列仅包含正值时,该列才能正常工作。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 这样做是为了避免数字从正数“换行”到负数时出现精度问题,也为了确保不会意外地得到包含0的AUTO_INCREMENT
column that contains 0
.AUTO_INCREMENT
列。
For 对于MyISAM
tables, you can specify an AUTO_INCREMENT
secondary column in a multiple-column key. MyISAM
表,您可以在多列键中指定AUTO_INCREMENT
辅助列。See Section 3.6.9, “Using AUTO_INCREMENT”.请参阅第3.6.9节,“使用自动增量”。
To make MySQL compatible with some ODBC applications, you can find the 要使MySQL与某些ODBC应用程序兼容,可以通过以下查询找到最后插入行的AUTO_INCREMENT
value for the last inserted row with the following query:AUTO_INCREMENT
值:
SELECT * FROMtbl_name
WHEREauto_col
IS NULL
This method requires that 此方法要求sql_auto_is_null
variable is not set to 0. sql_auto_is_null
变量未设置为0。See Section 5.1.8, “Server System Variables”.请参阅第5.1.8节,“服务器系统变量”。
For information about 有关InnoDB
and AUTO_INCREMENT
, see Section 15.6.1.6, “AUTO_INCREMENT Handling in InnoDB”. InnoDB
和自动增量的信息,请参阅第15.6.1.6节,“InnoDB中的自动增量处理”。For information about 有关AUTO_INCREMENT
and MySQL Replication, see Section 17.5.1.1, “Replication and AUTO_INCREMENT”.AUTO_INCREMENT
和MySQL复制的信息,请参阅第17.5.1.1节,“复制和自动增量”。
COMMENT
A comment for a column can be specified with the 可以使用COMMENT
option, up to 1024 characters long. COMMENT
选项指定列的注释,最长可达1024个字符。The comment is displayed by the 注释由SHOW CREATE TABLE
and SHOW FULL COLUMNS
statements.SHOW CREATE TABLE
和SHOW FULL COLUMNS
语句显示。
COLUMN_FORMAT
In NDB Cluster, it is also possible to specify a data storage format for individual columns of 在NDB
tables using COLUMN_FORMAT
. NDB
集群中,还可以使用COLUMN_FORMAT
为NDB表的各个列指定数据存储格式。Permissible column formats are 允许的列格式为FIXED
, DYNAMIC
, and DEFAULT
. FIXED
、DYNAMIC
和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).FIXED
用于指定固定宽度存储,DYNAMIC
允许列为可变宽度,而DEFAULT
会导致列使用由列的数据类型确定的固定宽度或可变宽度存储(可能由ROW_FORMAT
说明符覆盖)。
For 对于NDB
tables, the default value for COLUMN_FORMAT
is FIXED
.NDB
表格,COLUMN_FORMAT
的默认值是固定的。
In NDB Cluster, the maximum possible offset for a column defined with 在NDB集群中,使用COLUMN_FORMAT=FIXED
is 8188 bytes. COLUMN_FORMAT=FIXED
定义的列的最大可能偏移量为8188字节。For more information and possible workarounds, see Section 23.1.7.5, “Limits Associated with Database Objects in NDB Cluster”.有关更多信息和可能的解决方法,请参阅第23.1.7.5节,“NDB集群中与数据库对象相关的限制”。
COLUMN_FORMAT
currently has no effect on columns of tables using storage engines other than NDB
. COLUMN_FORMAT
目前对使用NDB
以外的存储引擎的表的列没有影响。MySQL 8.0 silently ignores MySQL 8.0默认忽略COLUMN_FORMAT
.COLUMN_FORMAT
。
ENGINE_ATTRIBUTE
and SECONDARY_ENGINE_ATTRIBUTE
options (available as of MySQL 8.0.21) are used to specify column attributes for primary and secondary storage engines. ENGINE_ATTRIBUTE
和SECONDARY_ENGINE_ATTRIBUTE
选项(从MySQL 8.0.21开始提供)用于指定主存储引擎和辅助存储引擎的列属性。The options are reserved for future use.这些选项保留供将来使用。
Permitted values are a string literal containing a valid 允许的值是包含有效JSON
document or an empty string (''). JSON
文档的字符串文字或空字符串(''
)。Invalid 无效的JSON
is rejected.JSON
会被拒绝。
CREATE TABLE t1 (c1 INT ENGINE_ATTRIBUTE='{"key
":"value
"}');
ENGINE_ATTRIBUTE
and SECONDARY_ENGINE_ATTRIBUTE
values can be repeated without error. ENGINE_ATTRIBUTE
和SECONDARY_ENGINE_ATTRIBUTE
值可以无错误地重复。In this case, the last specified value is used.在这种情况下,将使用最后指定的值。
服务器不会检查ENGINE_ATTRIBUTE
and SECONDARY_ENGINE_ATTRIBUTE
values are not checked by the server, nor are they cleared when the table's storage engine is changed.ENGINE_ATTRIBUTE
性和SECONDARY_ENGINE_ATTRIBUTE
值,也不会在更改表的存储引擎时清除它们。
STORAGE
For 对于NDB
tables, it is possible to specify whether the column is stored on disk or in memory by using a STORAGE
clause. NDB
表,可以使用存储子句指定列是存储在磁盘上还是存储在内存中。STORAGE DISK
causes the column to be stored on disk, and STORAGE MEMORY
causes in-memory storage to be used. STORAGE DISK
导致列存储在磁盘上,而STORAGE MEMORY
导致使用内存内存储。The 使用的CREATE TABLE
statement used must still include a TABLESPACE
clause:CREATE TABLE
语句必须仍然包含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;
Query OK, 0 rows affected (1.06 sec)
For 对于NDB
tables, STORAGE DEFAULT
is equivalent to STORAGE MEMORY
.NDB
表,STORAGE DEFAULT
相当于STORAGE MEMORY
。
The STORAGE
clause has no effect on tables using storage engines other than NDB
. STORAGE
子句对使用除NDB
以外的存储引擎的表没有影响。The STORAGE
keyword is supported only in the build of 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.STORAGE
关键字仅在NDB 群集提供的mysqld构建中受支持;它在任何其他版本的MySQL中都无法识别,任何使用STORAGE
关键字的尝试都会导致语法错误。
GENERATED ALWAYS
Used to specify a generated column expression. 用于指定生成的列表达式。For information about generated columns, see Section 13.1.20.8, “CREATE TABLE and Generated Columns”.有关生成列的信息,请参阅第13.1.20.8节,“创建表和生成列”。
Stored generated columns can be indexed. 可以为存储的生成列编制索引。InnoDB
supports secondary indexes on virtual generated columns. InnoDB
支持虚拟生成列上的辅助索引。See Section 13.1.20.9, “Secondary Indexes and Generated Columns”.请参阅第13.1.20.9节,“二级索引和生成列”。
Several keywords apply to creation of indexes, foreign keys, and 有几个关键字可用于创建索引、外键和检查约束。CHECK
constraints. For general background in addition to the following descriptions, see Section 13.1.15, “CREATE INDEX Statement”, Section 13.1.20.5, “FOREIGN KEY Constraints”, and Section 13.1.20.6, “CHECK Constraints”.除以下说明外,有关一般背景信息,请参见第13.1.15节,“创建索引语句”、第13.1.20.5节,“外键约束”和第13.1.20.6节,“检查约束”。
CONSTRAINT
symbol
The 可以使用CONSTRAINT
clause may be given to name a constraint. symbol
CONSTRAINT
子句来命名约束。symbol
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. CONSTRAINT
关键字后面没有包含symbol
,MySQL会自动生成一个约束名称,下面会指出一个例外。The 如果使用symbol
value, if used, must be unique per schema (database), per constraint type. symbol
值,则每个模式(数据库)和约束类型的符号值必须是唯一的。A duplicate 重复的symbol
results in an error. symbol
会导致错误。See also the discussion about length limits of generated constraint identifiers at Section 9.2.1, “Identifier Length Limits”.另见第9.2.1节,“标识符长度限制”中关于生成约束标识符长度限制的讨论。
If the 如果外键定义中没有给出CONSTRAINT
clause is not given in a foreign key definition, or a symbol
symbol
is not included following the CONSTRAINT
keyword, MySQL uses the foreign key index name up to MySQL 8.0.15, and automatically generates a constraint name thereafter.CONSTRAINT
子句,或者symbol
CONSTRAINT
关键字后面没有包含symbol
,MySQL将使用外键索引名,直到MySQL 8.0.15,并在其后自动生成约束名。
The SQL standard specifies that all types of constraints (primary key, unique index, foreign key, check) belong to the same namespace. SQL标准指定所有类型的约束(主键、唯一索引、外键、检查)都属于同一名称空间。In MySQL, each constraint type has its own namespace per schema. 在MySQL中,每个约束类型在每个模式中都有自己的名称空间。Consequently, names for each type of constraint must be unique per schema, but constraints of different types can have the same name.因此,每种类型的约束的名称在每个模式中都必须是唯一的,但不同类型的约束可以具有相同的名称。
PRIMARY KEY
A unique index where all key columns must be defined as 唯一索引,其中所有键列必须定义为NOT NULL
. NOT NULL
。If they are not explicitly declared as 如果它们没有显式声明为NOT NULL
, MySQL declares them so implicitly (and silently). NOT NULL
,MySQL就会隐式地(静默地)声明它们。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.PRIMARY
,因此不能用作任何其他类型索引的名称。
If you do not have a 如果您没有主键,并且应用程序在表中请求主键,MySQL将返回第一个不包含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
.NULL
列的UNIQUE
索引作为主键。
In 在InnoDB
tables, keep the PRIMARY KEY
short to minimize storage overhead for secondary indexes. InnoDB
表中,请保持PRIMARY KEY
简短,以最大限度地减少辅助索引的存储开销。Each secondary index entry contains a copy of the primary key columns for the corresponding row. 每个辅助索引项都包含对应行的主键列的副本。(See Section 15.6.2.1, “Clustered and Secondary Indexes”.)(请参阅第15.6.2.1节,“聚集索引和二级索引”。)
In the created table, a 在创建的表中,首先放置PRIMARY KEY
is placed first, followed by all UNIQUE
indexes, and then the nonunique indexes. PRIMARY KEY
,然后是所有UNIQUE
索引,然后是非唯一索引。This helps the MySQL optimizer to prioritize which index to use and also more quickly to detect duplicated 这有助于MySQL优化器确定要使用的索引的优先级,并更快地检测重复的UNIQUE
keys.UNIQUE
键。
A PRIMARY KEY
can be a multiple-column index. PRIMARY KEY
可以是多列索引。However, you cannot create a multiple-column index using the 但是,不能使用列规范中的PRIMARY KEY
key attribute in a column specification. PRIMARY KEY
属性创建多列索引。Doing so only marks that single column as primary. 这样做只会将单个列标记为主列。You must use a separate 您必须使用单独的主键PRIMARY KEY(
clause.key_part
, ...)PRIMARY KEY(
子句。key_part
, ...)
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 SELECT
statements, as described in Unique Indexes.PRIMARY KEY
或UNIQUE NOT NULL
索引由一个整型列组成,则可以使用_rowid
在SELECT
语句中引用索引列,如唯一索引中所述。
In MySQL, the name of a 在MySQL中,PRIMARY KEY
is PRIMARY
. PRIMARY KEY
的名称是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. _2
、_3
、…)使其唯一。You can see index names for a table using 您可以使用SHOW INDEX FROM
. tbl_name
SHOW INDEX FROM
查看表的索引名。tbl_name
See Section 13.7.7.22, “SHOW INDEX Statement”.请参阅第13.7.7.22节,“显示索引报表”。
KEY | INDEX
KEY
is normally a synonym for INDEX
. KEY
通常是INDEX
的同义词。The key attribute 在列定义中给定键属性PRIMARY KEY
can also be specified as just KEY
when given in a column definition. PRIMARY KEY
时,也可以将其指定为仅KEY
。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. UNIQUE
索引会创建一个约束,使得索引中的所有值都必须是不同的。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
. UNIQUE
索引允许包含NULL
的列具有多个NULL
值。If you specify a prefix value for a column in a 如果为UNIQUE
index, the column values must be unique within the prefix length.UNIQUE
索引中的列指定前缀值,则列值在前缀长度内必须唯一。
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 SELECT
statements, as described in Unique Indexes.PRIMARY KEY
或UNIQUE NOT NULL
索引由一个整型列组成,则可以使用_rowid
在SELECT
语句中引用索引列,如唯一索引中所述。
FULLTEXT
A FULLTEXT
index is a special type of index used for full-text searches. FULLTEXT
索引是用于全文搜索的一种特殊类型的索引。Only the 只有InnoDB
and MyISAM
storage engines support FULLTEXT
indexes. InnoDB
和MyISAM
存储引擎支持全文索引。They can be created only from 它们只能从CHAR
, VARCHAR
, and TEXT
columns. CHAR
、VARCHAR
和TEXT
列创建。Indexing always happens over the entire column; column prefix indexing is not supported and any prefix length is ignored if specified. 索引总是发生在整个列上;不支持列前缀索引,如果指定,则忽略任何前缀长度。See Section 12.10, “Full-Text Search Functions”, for details of operation. 有关操作的详细信息,请参阅第12.10节,“全文搜索功能”。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. WITH PARSER
子句指定为index_option
值,以将解析器插件与索引关联起来。This clause is valid only for 此子句仅对FULLTEXT
indexes. FULLTEXT
索引有效。InnoDB
and MyISAM
support full-text parser plugins. InnoDB
和MyISAM
支持全文解析器插件。See Full-Text Parser Plugins and Writing Full-Text Parser Plugins for more information.有关更多信息,请参阅全文解析器插件和编写全文解析器插件。
SPATIAL
You can create 可以在SPATIAL
indexes on spatial data types. SPATIAL
数据类型上创建空间索引。Spatial types are supported only for 仅InnoDB
and MyISAM
tables, and indexed columns must be declared as NOT NULL
. InnoDB
和MyISAM
表支持空间类型,索引列必须声明为NOT NULL
。See Section 11.4, “Spatial Data Types”.请参阅第11.4节,“空间数据类型”。
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. MySQL支持外键(允许跨表交叉引用相关数据)和外键约束(有助于保持数据分布的一致性)。For definition and option information, see 有关定义和选项信息,请参见reference_definition
, and reference_option
.reference_definition
和reference_option
。
Partitioned tables employing the 使用InnoDB
storage engine do not support foreign keys. InnoDB
存储引擎的分区表不支持外键。See Section 24.6, “Restrictions and Limitations on Partitioning”, for more information.有关更多信息,请参见第24.6节,“分区的约束和局限性”。
CHECK
The CHECK
clause enables the creation of constraints to be checked for data values in table rows. CHECK
子句允许为表行中的数据值检查约束的创建。See Section 13.1.20.6, “CHECK Constraints”.请参阅第13.1.20.6节,“检查约束”。
key_part
A key_part
specification can end with ASC
or DESC
to specify whether index values are stored in ascending or descending order. key_part
规范可以以ASC
或DESC
结尾,以指定索引值是按升序还是降序存储。The default is ascending if no order specifier is given.如果未指定顺序说明符,则默认值为升序。
Prefixes, defined by the 对于使用length
attribute, can be up to 767 bytes long for InnoDB
tables that use the REDUNDANT
or COMPACT
row format. REDUNDANT
(冗余)行格式或COMPACT
(紧凑)行格式的InnoDB
表,由length
属性定义的前缀最长可达767字节。The prefix length limit is 3072 bytes for 对于使用InnoDB
tables that use the DYNAMIC
or COMPRESSED
row format. DYNAMIC
(动态)行格式或COMPRESSED
(压缩)行格式的InnoDB
表,前缀长度限制为3072字节。For 对于MyISAM
tables, the prefix length limit is 1000 bytes.MyISAM
表,前缀长度限制为1000字节。
Prefix limits are measured in bytes. 前缀限制以字节为单位。However, prefix lengths for index specifications in 但是,CREATE TABLE
, ALTER TABLE
, and CREATE INDEX
statements are interpreted as number of characters for nonbinary string types (CHAR
, VARCHAR
, TEXT
) and number of bytes for binary string types (BINARY
, VARBINARY
, BLOB
). CREATE TABLE
语句、ALTER TABLE
语句和CREATE INDEX
语句中索引规范的前缀长度被解释为非二进制字符串类型(CHAR
、VARCHAR
、TEXT
)的字符数和二进制字符串类型(BINARY
、VARBINARY
、BLOB
)的字节数。Take this into account when specifying a prefix length for a nonbinary string column that uses a multibyte character set.为使用多字节字符集的非二进制字符串列指定前缀长度时,请考虑这一点。
Beginning with MySQL 8.0.17, the 从MySQL 8.0.17开始,expr
for a key_part
specification can take the form (CAST
to create a multi-valued index on a json_path
AS type
ARRAY)JSON
column. key_part
规范的expr
可以采用以下形式(CAST
在json_path
AS type
ARRAY)JSON
列上创建多值索引。Multi-Valued Indexes, provides detailed information regarding creation of, usage of, and restrictions and limitations on multi-valued indexes.多值索引,提供有关多值索引的创建、使用以及限制和限制的详细信息。
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
index_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. USING
位置是在索引列列表之后。It can be given before the column list, but support for use of the option in that position is deprecated and you should expect it to be removed in a future MySQL release.它可以在列列表之前给出,但是不推荐在该位置使用该选项,您应该期望在将来的MySQL版本中删除它。
index_option
index_option
values specify additional options for an index.index_option
值指定索引的其他选项。
KEY_BLOCK_SIZE
For 对于MyISAM
tables, KEY_BLOCK_SIZE
optionally specifies the size in bytes to use for index key blocks. MyISAM
表,KEY_BLOCK_SIZE
可以选择指定索引键块使用的大小(以字节为单位)。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.KEY_BLOCK_SIZE
值覆盖表级键块大小值。
For information about the table-level 有关表级KEY_BLOCK_SIZE
attribute, see Table Options.KEY_BLOCK_SIZE
属性的信息,请参见表选项。
WITH PARSER
The WITH PARSER
option can be used only with FULLTEXT
indexes. WITH PARSER
选项只能用于FULLTEXT
索引。It associates a parser plugin with the index if full-text indexing and searching operations need special handling. 如果全文索引和搜索操作需要特殊处理,它会将解析器插件与索引相关联。InnoDB
and MyISAM
support full-text parser plugins. InnoDB
和MyISAM
支持全文解析器插件。If you have a 如果您有一个带有相关全文解析器插件的MyISAM
table with an associated full-text parser plugin, you can convert the table to InnoDB
using ALTER TABLE
.MyISAM
表,则可以使用ALTER TABLE
将该表转换为InnoDB
。
COMMENT
Index definitions can include an optional comment of up to 1024 characters.索引定义可以包含最多1024个字符的可选注释。
You can set the 可以使用InnoDB
MERGE_THRESHOLD
value for an individual index using the index_option
COMMENT
clause. index_option
COMMENT
子句为单个索引设置InnoDB
MERGE_THRESHOLD
。See Section 15.8.11, “Configuring the Merge Threshold for Index Pages”.请参阅第15.8.11节,“为索引页配置合并阈值”。
VISIBLE
, INVISIBLE
Specify index visibility. 指定索引可见性。Indexes are visible by default. 默认情况下,索引是可见的。An invisible index is not used by the optimizer. 优化器不使用不可见索引。Specification of index visibility applies to indexes other than primary keys (either explicit or implicit). 索引可见性规范适用于主键以外的索引(显式或隐式)。For more information, see Section 8.3.12, “Invisible Indexes”.有关更多信息,请参见第8.3.12节,“不可见索引”。
ENGINE_ATTRIBUTE
and SECONDARY_ENGINE_ATTRIBUTE
options (available as of MySQL 8.0.21) are used to specify index attributes for primary and secondary storage engines. ENGINE_ATTRIBUTE
和SECONDARY_ENGINE_ATTRIBUTE
选项(从MySQL 8.0.21开始提供)用于指定主存储引擎和辅助存储引擎的索引属性。The options are reserved for future use.这些选项保留供将来使用。
For more information about permissible 有关允许index_option
values, see Section 13.1.15, “CREATE INDEX Statement”. index_option
值的更多信息,请参阅第13.1.15节,“创建索引语句”。For more information about indexes, see Section 8.3.1, “How MySQL Uses Indexes”.有关索引的更多信息,请参阅第8.3.1节,“MySQL如何使用索引”。
For 有关reference_definition
syntax details and examples, see Section 13.1.20.5, “FOREIGN KEY Constraints”.reference_definition
语法的详细信息和示例,请参见第13.1.20.5节,“外键约束”。
InnoDB
and NDB
tables support checking of foreign key constraints. InnoDB
和NDB
表支持检查外键约束。The columns of the referenced table must always be explicitly named. 引用表的列必须始终显式命名。Both 支持外键上的ON DELETE
and ON UPDATE
actions on foreign keys are supported. ON DELETE
和ON UPDATE
操作。For more detailed information and examples, see Section 13.1.20.5, “FOREIGN KEY Constraints”.有关更多详细信息和示例,请参阅第13.1.20.5节,“外键约束”。
For other storage engines, MySQL Server parses and ignores the 对于其他存储引擎,MySQL服务器解析并忽略FOREIGN KEY
and REFERENCES
syntax in CREATE TABLE
statements. CREATE TABLE
语句中的FOREIGN KEY
和REFERENCES
语法。See Section 1.7.2.3, “FOREIGN KEY Constraint Differences”.请参阅第1.7.2.3节,“外键约束差异”。
For users familiar with the ANSI/ISO SQL Standard, please note that no storage engine, including 对于熟悉ANSI/ISO SQL标准的用户,请注意,没有任何存储引擎(包括InnoDB
, recognizes or enforces the MATCH
clause used in referential integrity constraint definitions. InnoDB
)能够识别或强制执行引用完整性约束定义中使用的MATCH
子句。Use of an explicit 使用显式MATCH
clause does not have the specified effect, and also causes ON DELETE
and ON UPDATE
clauses to be ignored. MATCH
子句不会产生指定的效果,还会导致忽略ON DELETE
和ON UPDATE
子句。For these reasons, specifying 出于这些原因,应避免指定MATCH
should be avoided.MATCH
。
The SQL标准中的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. MATCH
子句控制在与主键进行比较时如何处理复合(多列)外键中的NULL
值。InnoDB
essentially implements the semantics defined by MATCH SIMPLE
, which permit a foreign key to be all or partially NULL
. InnoDB
本质上实现了MATCH 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. 此外,MySQL要求对引用的列进行索引以提高性能。However, 但是,InnoDB不强制要求引用的列声明为InnoDB
does not enforce any requirement that the referenced columns be declared UNIQUE
or NOT NULL
. UNIQUE
或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
. UPDATE
或DELETE CASCADE
之类的操作,没有很好地定义对非唯一键或包含NULL
值的键的外键引用的处理。You are advised to use foreign keys that reference only keys that are both 建议您使用只引用UNIQUE
(or PRIMARY
) and NOT NULL
.UNIQUE
(或PRIMARY
)且不为空的键的外键。
MySQL parses but ignores “inline MySQL解析但忽略“内联引用规范”(在SQL标准中定义),其中引用被定义为列规范的一部分。REFERENCES
specifications” (as defined in the SQL standard) where the references are defined as part of the column specification. MySQL accepts MySQL仅在作为单独REFERENCES
clauses only when specified as part of a separate FOREIGN KEY
specification.FOREIGN KEY
规范的一部分指定时才接受REFERENCES
子句。
For information about the 有关RESTRICT
, CASCADE
, SET NULL
, NO ACTION
, and SET DEFAULT
options, see Section 13.1.20.5, “FOREIGN KEY Constraints”.RESTRICT
(限制)、CASCADE
(级联)、SET NULL
、NO ACTION
(无操作)和SET DEFAULT
(设置默认)选项的信息,请参阅第13.1.20.5节,“外键约束”。
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.ALTER TABLE
将表转换为使用其他存储引擎,则这些选项将适用。
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.'DEFAULT'
,但已忽略该名称。
InnoDB | InnoDB . |
MyISAM | |
MEMORY | |
CSV | |
ARCHIVE | |
EXAMPLE | |
FEDERATED | |
HEAP | MEMORY .MEMORY 的同义词。 |
MERGE | MyISAM tables used as one table. MyISAM 表的集合。MRG_MyISAM . MRG_MyISAM 。 |
NDB | NDBCLUSTER . NDBCLUSTER 。 |
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 您可以通过从服务器SQL模式(请参阅第5.1.11节,“服务器SQL模式”)中删除NO_ENGINE_SUBSTITUTION
from the server SQL mode (see Section 5.1.11, “Server SQL Modes”) so that MySQL allows substitution of the specified engine with the default storage engine instead. NO_ENGINE_SUBSTITUTION
(无引擎替换)来覆盖此行为,以便MySQL允许使用默认存储引擎替换指定的引擎。Normally in such cases, this is 通常在这种情况下,这是InnoDB
, which is the default value for the default_storage_engine
system variable. InnoDB
,它是default_storage_engine
系统变量的默认值。When 当NO_ENGINE_SUBSTITUTION
is disabled, a warning occurs if the storage engine specification is not honored.NO_ENGINE_SUBSTITUTION
被禁用的时候,如果不遵守存储引擎规范,将出现警告。
AUTOEXTEND_SIZE
Defines the amount by which 定义InnoDB
extends the size of the tablespace when it becomes full. InnoDB
在表空间满时扩展表空间大小的量。Introduced in MySQL 8.0.23. The setting must be a multiple of 4MB. 在MySQL 8.0.23中引入。该设置必须是4MB的倍数。The default setting is 0, which causes the tablespace to be extended according to the implicit default behavior. 默认设置为0,这将根据隐式默认行为扩展表空间。For more information, see Section 15.6.3.9, “Tablespace AUTOEXTEND_SIZE Configuration”.有关更多信息,请参阅第15.6.3.9节,“表空间自动扩展大小配置”。
AUTO_INCREMENT
The initial 表的初始AUTO_INCREMENT
value for the table. AUTO_INCREMENT
值。In MySQL 8.0, this works for 在MySQL 8.0中,这适用于MyISAM
, MEMORY
, InnoDB
, and ARCHIVE
tables. MyISAM
、MEMORY
、InnoDB
和ARCHIVE
表。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.AUTO_INCREMENT
表选项的发动机设置第一个自动增量值,请在创建表后插入一个值小于所需值1的“虚拟”行,然后删除虚拟行。
For engines that support the 对于支持AUTO_INCREMENT
table option in CREATE TABLE
statements, you can also use ALTER TABLE
to reset the tbl_name
AUTO_INCREMENT = N
AUTO_INCREMENT
value. CREATE TABLE
语句中的AUTO_INCREMENT
表选项的引擎,还可以使用ALTER TABLE
以重置tbl_name
AUTO_INCREMENT = N
AUTO_INCREMENT
值。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. MyISAM
表时,MySQL使用MAX_ROWS
和AVG_ROW_LENGTH
选项的乘积来决定结果表的大小。If you don't specify either option, the maximum size for 如果不指定这两个选项,MyISAM
data and index files is 256TB by default. MyISAM
数据和索引文件的最大大小默认为256TB。(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. myisam_data_pointer_size
系统变量来减小默认指针大小。(See Section 5.1.8, “Server System Variables”.) (请参阅第5.1.8节“服务器系统变量”。)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.将该值设置为7允许表格大小最大为65536TB。
[DEFAULT] CHARACTER SET
Specifies a default character set for the table. 指定表格的默认字符集。CHARSET
is a synonym for CHARACTER SET
. CHARSET
是CHARACTER SET
的同义词。If the character set name is 如果字符集名称为DEFAULT
, the database character set is used.DEFAULT
,则使用数据库字符集。
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). 如果希望MySQL为所有行维护实时校验和(即,MySQL在表更改时自动更新的校验和),请将其设置为1。This makes the table a little slower to update, but also makes it easier to find corrupted tables. 这使得表的更新速度稍慢,但也使查找损坏的表变得更容易。The CHECKSUM TABLE
statement reports the checksum. CHECKSUM TABLE
语句报告校验和。((仅限MyISAM
only.)MyISAM
。)
[DEFAULT] COLLATE
Specifies a default collation for the table.指定表的默认排序规则。
COMMENT
A comment for the table, up to 2048 characters long.表的注释,最长2048个字符。
You can set the 可以使用InnoDB
MERGE_THRESHOLD
value for a table using the table_option
COMMENT
clause. table_option
COMMENT
子句为表设置InnoDB
MERGE_THRESHOLD
值。See Section 15.8.11, “Configuring the Merge Threshold for Index Pages”.请参阅第15.8.11节,“为索引页配置合并阈值”。
Setting NDB_TABLE options.设置NDB_表选项。 The table comment in a 创建CREATE TABLE
that creates an NDB
table or an ALTER TABLE
statement which alters one 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. NDB
表的CREATE TABLE
中的表注释或更改NDB
表的ALTER TABLE
语句也可用于指定一到四个NDB_TABLE
选项NOLOGING
、READ_BACKUP
、PARTITION_BALANCE
或FULLY_REPLICATED
为一组名称-值对,必要时用逗号分隔,紧跟在字符串NDB_TABLE=
之后,该字符串开始引用的注释文本。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 注释显示为SHOW CREATE TABLE
. SHOW CREATE TABLE
输出的一部分。The text of the comment is also available as the TABLE_COMMENT column of the MySQL Information Schema 注释的文本也可以作为MySQL信息架构TABLES
table.TABLES
的TABLE_COMMENT列使用。
This comment syntax is also supported with ALTER TABLE
statements for NDB
tables. NDB
表的ALTER TABLE
语句也支持此注释语法。Keep in mind that a table comment used with 请记住,与ALTER TABLE
replaces any existing comment which the table might have had perviously.ALTER TABLE
一起使用的表注释将替换表中可能存在的任何现有注释。
Setting the MERGE_THRESHOLD
option in table comments is not supported for NDB
tables (it is ignored).NDB
表不支持在表注释中设置MERGE_THRESHOLD
选项(忽略该选项)。
For complete syntax information and examples, see Section 13.1.20.11, “Setting NDB_TABLE Options”.有关完整的语法信息和示例,请参阅第13.1.20.11节,“设置NDB_TABLE选项”。
COMPRESSION
The compression algorithm used for page level compression for 用于InnoDB
tables. InnoDB
表的页面级压缩的压缩算法。Supported values include 支持的值包括Zlib
, LZ4
, and None
. Zlib
、LZ4
和None
。The COMPRESSION
attribute was introduced with the transparent page compression feature. COMPRESSION
属性是通过透明页面压缩特性引入的。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. 的InnoDB
表,并且仅在支持稀疏文件和打孔的Linux和Windows平台上可用。For more information, see Section 15.9.2, “InnoDB Page Compression”.有关更多信息,请参阅第15.9.2节,“InnoDB页面压缩”。
CONNECTION
The connection string for a FEDERATED
table.FEDERATED
(联合)表的连接字符串。
Older versions of MySQL used a 较旧版本的MySQL对连接字符串使用了COMMENT
option for the connection string.COMMENT
选项。
DATA DIRECTORY
, INDEX DIRECTORY
For 对于InnoDB
, the DATA DIRECTORY='
clause permits creating tables outside of the data directory. directory
'InnoDB
,DATA DIRECTORY='
子句允许在数据目录之外创建表。directory
'The 必须启用innodb_file_per_table
variable must be enabled to use the DATA DIRECTORY
clause. The full directory path must be specified. innodb_file_per_table
变量才能使用DATA DIRECTORY
子句。必须指定完整的目录路径。As of MySQL 8.0.21, the directory specified must be known to 从MySQL 8.0.21开始,InnoDB
. InnoDB
必须知道指定的目录。For more information, see Section 15.6.1.2, “Creating Tables Externally”.有关更多信息,请参阅第15.6.1.2节,“外部创建表”。
When creating 创建MyISAM
tables, you can use the DATA DIRECTORY='
clause, the directory
'INDEX DIRECTORY='
clause, or both. directory
'MyISAM
表时,可以使用DATA DIRECTORY='
子句、directory
'INDEX DIRECTORY='
子句或两者兼用。directory
'They specify where to put a 它们分别指定MyISAM
table's data file and index file, respectively. MyISAM
表的数据文件和索引文件的放置位置。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. InnoDB
表不同,在使用DATA DIRECTORY
选项或INDEX DIRECTORY
选项创建MyISAM
表时,MySQL不会创建与数据库名称对应的子目录。Files are created in the directory that is specified.在指定的目录中创建文件。
You must have the 您必须具有FILE
privilege to use the DATA DIRECTORY
or INDEX DIRECTORY
table option.FILE
权限才能使用DATA DIRECTORY
表选项或INDEX DIRECTORY
表选项。
Table-level 对于分区表,忽略表级DATA DIRECTORY
and INDEX DIRECTORY
options are ignored for partitioned tables. DATA DIRECTORY
选项和INDEX DIRECTORY
选项。(Bug #32091)
These options work only when you are not using the 这些选项仅在不使用--skip-symbolic-links
option. --skip-symbolic-links
选项时有效。Your operating system must also have a working, thread-safe 您的操作系统还必须有一个线程安全的realpath()
call. realpath()
调用。See Section 8.12.2.2, “Using Symbolic Links for MyISAM Tables on Unix”, for more complete information.有关更多完整信息,请参阅第8.12.2.2节,“在Unix上为MyISAM表使用符号链接”。
If a 如果创建MyISAM
table is created with no DATA DIRECTORY
option, the .MYD
file is created in the database directory. MyISAM
表时没有DATA DIRECTORY
选项,则会在数据库目录中创建.MYD
文件。By default, if 默认情况下,如果MyISAM
finds an existing .MYD
file in this case, it overwrites it. MyISAM
在本例中找到现有的.MYD
文件,它将覆盖该文件。The same applies to 这同样适用于不使用索引目录选项创建的表的.MYI
files for tables created with no INDEX DIRECTORY
option. .MYI
文件。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.--keep_files_on_create
选项启动服务器,在这种情况下,MyISAM
不会覆盖现有文件,而是返回一个错误。
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, and does not overwrite a file in the specified directory.DATA DIRECTORY
或INDEX DIRECTORY
选项创建MyISAM
表,并且找到现有的.MYD
或.MYI
文件,则MyISAM
始终返回错误,并且不会覆盖指定目录中的文件。
You cannot use path names that contain the MySQL data directory with 不能将包含MySQL数据目录的路径名与DATA DIRECTORY
or INDEX DIRECTORY
. DATA DIRECTORY
或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. 如果要延迟表的键更新直到关闭表,请将此设置为1。See the description of the 请参阅第5.1.8节,“服务器系统变量”中对delay_key_write
system variable in Section 5.1.8, “Server System Variables”. delay_key_write
系统变量的描述。(MyISAM
only.)
ENCRYPTION
The ENCRYPTION
clause enables or disables page-level data encryption for an InnoDB
table. ENCRYPTION
子句启用或禁用InnoDB
表的页面级数据加密。A keyring plugin must be installed and configured before encryption can be enabled. 在启用加密之前,必须安装并配置密钥环插件。Prior to MySQL 8.0.16, the 在MySQL 8.0.16之前,只有在每个表空间的文件中创建表时才能指定ENCRYPTION
clause can only be specified when creating a table in an a file-per-table tablespace. ENCRYPTION
子句。As of MySQL 8.0.16, the 从MySQL 8.0.16开始,在常规表空间中创建表时也可以指定ENCRYPTION
clause can also be specified when creating a table in a general tablespace.ENCRYPTION
子句。
As of MySQL 8.0.16, a table inherits the default schema encryption if an 从MySQL 8.0.16开始,如果未指定ENCRYPTION
clause is not specified. ENCRYPTION
子句,表将继承默认模式加密。If the 如果启用了table_encryption_privilege_check
variable is enabled, the TABLE_ENCRYPTION_ADMIN
privilege is required to create a table with an ENCRYPTION
clause setting that differs from the default schema encryption. table_encryption_privilege_check
变量,则需要TABLE_ENCRYPTION_ADMIN
权限来创建具有不同于默认架构加密的ENCRYPTION
子句设置的表。When creating a table in a general tablespace, table and tablespace encryption must match.在常规表空间中创建表时,表和表空间加密必须匹配。
As of MySQL 8.0.16, specifying an 从MySQL 8.0.16开始,在使用不支持加密的存储引擎时,不允许使用ENCRYPTION
clause with a value other than 'N'
or ''
is not permitted when using a storage engine that does not support encryption. 'N'
或''
以外的值指定ENCRYPTION
子句。Previously, the clause was accepted.此前,该子句被接受。
For more information, see Section 15.13, “InnoDB Data-at-Rest Encryption”.有关更多信息,请参阅第15.13节,“InnoDB静态数据加密”。
ENGINE_ATTRIBUTE
and SECONDARY_ENGINE_ATTRIBUTE
options (available as of MySQL 8.0.21) are used to specify table attributes for primary and secondary storage engines. ENGINE_ATTRIBUTE
选项和SECONDARY_ENGINE_ATTRIBUTE
选项(从MySQL 8.0.21开始提供)用于指定主存储引擎和辅助存储引擎的表属性。The options are reserved for future use.这些选项保留供将来使用。
Permitted values are a string literal containing a valid 允许的值是包含有效JSON
document or an empty string (''). JSON
文档的字符串文字或空字符串(''
)。Invalid 无效的JSON
is rejected.JSON
被拒绝。
CREATE TABLE t1 (c1 INT) ENGINE_ATTRIBUTE='{"key
":"value
"}';
ENGINE_ATTRIBUTE
and SECONDARY_ENGINE_ATTRIBUTE
values can be repeated without error. ENGINE_ATTRIBUTE
值和SECONDARY_ENGINE_ATTRIBUTE
值可以无错误地重复。In this case, the last specified value is used.在这种情况下,将使用最后指定的值。
服务器不会检查ENGINE_ATTRIBUTE
and SECONDARY_ENGINE_ATTRIBUTE
values are not checked by the server, nor are they cleared when the table's storage engine is changed.ENGINE_ATTRIBUTE
和SECONDARY_ENGINE_ATTRIBUTE
值,也不会在更改表的存储引擎时清除它们。
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. MERGE
表,必须使用INSERT_METHOD
指定应将行插入的表。INSERT_METHOD
is an option useful for MERGE
tables only. INSERT_METHOD
仅对MERGE
表有用。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. FIRST
或LAST
可使插入转到第一个或最后一个表,或使用值NO
可防止插入。See Section 16.7, “The MERGE Storage Engine”.请参阅第16.7节,“合并存储引擎”。
KEY_BLOCK_SIZE
For 对于MyISAM
tables, KEY_BLOCK_SIZE
optionally specifies the size in bytes to use for index key blocks. MyISAM
表,KEY_BLOCK_SIZE
可以选择指定索引键块使用的大小(以字节为单位)。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.KEY_BLOCK_SIZE
值覆盖表级KEY_BLOCK_SIZE
值。
For 对于InnoDB
tables, KEY_BLOCK_SIZE
specifies the page size in kilobytes to use for compressed InnoDB
tables. InnoDB
表,KEY_BLOCK_SIZE
指定用于压缩InnoDB
表的页面大小(以KB为单位)。The KEY_BLOCK_SIZE
value is treated as a hint; a different size could be used by InnoDB
if necessary. KEY_BLOCK_SIZE
值被视为提示;如果需要,InnoDB
可以使用不同的大小。KEY_BLOCK_SIZE
can only be less than or equal to the innodb_page_size
value. KEY_BLOCK_SIZE
只能小于或等于InnoDB
页面大小值。A value of 0 represents the default compressed page size, which is half of the 值0表示默认的压缩页面大小,它是innodb_page_size
value. innodb_page_size
值的一半。Depending on 根据innodb_page_size
, possible KEY_BLOCK_SIZE
values include 0, 1, 2, 4, 8, and 16. innodb_page_size
,可能的KEY_BLOCK_SIZE
值包括0、1、2、4、8和16。See Section 15.9.1, “InnoDB Table Compression” for more information.有关更多信息,请参阅第15.9.1节,“InnoDB
表压缩”。
Oracle recommends enabling Oracle建议在为innodb_strict_mode
when specifying KEY_BLOCK_SIZE
for InnoDB
tables. InnoDB
表指定KEY_BLOCK_SIZE
时启用innodb_strict_mode
。When 启用innodb_strict_mode
is enabled, specifying an invalid KEY_BLOCK_SIZE
value returns an error. innodb_strict_mode
时,指定无效的KEY_BLOCK_SIZE<
值将返回错误。If 如果禁用innodb_strict_mode
is disabled, an invalid KEY_BLOCK_SIZE
value results in a warning, and the KEY_BLOCK_SIZE
option is ignored.innodb_strict_mode
,则无效的KEY_BLOCK_SIZE
值将导致警告,并且忽略KEY_BLOCK_SIZE
选项。
The 响应Create_options
column in response to SHOW TABLE STATUS
reports the actual KEY_BLOCK_SIZE
used by the table, as does SHOW CREATE TABLE
.SHOW TABLE STATUS
(显示表格状态)的Create_options
(创建选项)列报告表格使用的实际键块大小,SHOW Create TABLE
(显示创建表格)也是如此。
InnoDB
only supports KEY_BLOCK_SIZE
at the table level.InnoDB
仅支持表级别的KEY_BLOCK_SIZE
。
32KB和64KB KEY_BLOCK_SIZE
is not supported with 32KB and 64KB innodb_page_size
values. innodb_page_size
值不支持KEY_BLOCK_SIZE
。InnoDB
table compression does not support these pages sizes.InnoDB
表压缩不支持这些页面大小。
在创建临时表时,InnoDB
does not support the KEY_BLOCK_SIZE
option when creating temporary tables.InnoDB
不支持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.这不是一个硬限制,而是一个提示,提示存储引擎表必须能够存储至少这么多行。
The use of 不推荐使用带有MAX_ROWS
with NDB
tables to control the number of table partitions is deprecated. NDB
表的MAX_ROW
来控制表分区的数量。It remains supported in later versions for backward compatibility, but is subject to removal in a future release. 为了向后兼容,它在以后的版本中仍然受支持,但在将来的版本中可能会被删除。Use PARTITION_BALANCE instead; see Setting NDB_TABLE options.用分区平衡代替;请参见设置NDB_TABLE选项。
The NDB
storage engine treats this value as a maximum. NDB
存储引擎将此值视为最大值。If you plan to create very large NDB Cluster tables (containing millions of rows), you should use this option to insure that 如果计划创建非常大的NDB集群表(包含数百万行),则应使用此选项确保NDB在哈希表中分配足够数量的索引槽,用于通过设置NDB
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 *
, where rows
rows
is the number of rows that you expect to insert into the table.MAX_ROWS = 2 *
存储表主键的哈希,其中rows
rows
是希望插入表中的行数。
The maximum MAX_ROWS
value is 4294967295; larger values are truncated to this limit.MAX_ROW
的上限值为4294967295;较大的值将被截断到此限制。
MIN_ROWS
The minimum number of rows you plan to store in the table. 计划存储在表中的最小行数。The MEMORY
storage engine uses this option as a hint about memory use.MEMORY
存储引擎使用此选项作为内存使用的提示。
PACK_KEYS
Takes effect only with 仅对MyISAM
tables. MyISAM
表生效。Set this option to 1 if you want to have smaller indexes. 如果希望索引更小,请将此选项设置为1。This usually makes updates slower and reads faster. 这通常会使更新速度变慢,读取速度变快。Setting the option to 0 disables all packing of keys. 将选项设置为0将禁用所有密钥打包。Setting it to 将其设置为DEFAULT
tells the storage engine to pack only long CHAR
, VARCHAR
, BINARY
, or VARBINARY
columns.DEFAULT
会告诉存储引擎仅打包长CHAR
、VARCHAR
、BINARY
或VARBINARY
列。
If you do not use 如果不使用PACK_KEYS
, the default is to pack strings, but not numbers. PACK_KEYS
,默认情况下会打包字符串,但不会打包数字。If you use 如果使用PACK_KEYS=1
, numbers are packed as well.PACK_key=1
,则数字也会被打包。
When packing binary number keys, MySQL uses prefix compression:打包二进制数字键时,MySQL使用前缀压缩:
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). storage_size_for_key + pointer_size
(其中指针大小通常为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. NULL
值的键。(In this case, the packed key length is stored in the same byte that is used to mark if a key is (在这种情况下,压缩密钥长度存储在用于标记密钥是否为NULL
.)NULL
的相同字节中。)
PASSWORD
This option is unused.此选项未使用。
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 SHOW TABLE STATUS
. Row_format
列中报告,以响应SHOW TABLE STATUS
。The Create_options
column shows the row format that was specified in the CREATE TABLE
statement, as does SHOW CREATE TABLE
.Create_options
列显示CREATE TABLE
语句中指定的行格式,如SHOW CREATE TABLE
。
Row format choices differ depending on the storage engine used for the table.行格式选择因用于表的存储引擎而异。
For 对于InnoDB
tables:InnoDB
表:
The default row format is defined by 默认行格式由innodb_default_row_format
, which has a default setting of DYNAMIC
. innodb_default_row_format
定义,默认设置为DYNAMIC
。The default row format is used when the 未定义ROW_FORMAT
option is not defined or when ROW_FORMAT=DEFAULT
is used.ROW_FORMAT
选项或使用ROW_FORMAT=DEFAULT
时,将使用默认行格式。
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
. ROW_FORMAT
选项,或者使用了ROW_FORMAT=DEFAULT
,则重建表的操作也会将表的行格式静默更改为innodb_default_row_format
定义的默认行格式。For more information, see Defining the Row Format of a Table.有关详细信息,请参阅定义表的行格式。
For more efficient 要更高效地InnoDB
storage of data types, especially BLOB
types, use the DYNAMIC
. InnoDB
存储数据类型(尤其是BLOB
类型),请使用DYNAMIC
。See DYNAMIC Row Format for requirements associated with the 有关与DYNAMIC
row format.NYNAMIC
行格式关联的要求,请参阅DYNAMIC Row Format
。
To enable compression for 要启用InnoDB
tables, specify ROW_FORMAT=COMPRESSED
. InnoDB
表的压缩,请指定ROW_FORMAT=COMPRESSED
。The 创建临时表时不支持ROW_FORMAT=COMPRESSED
option is not supported when creating temporary tables. ROW_FORMAT=COMPRESSED
选项。See Section 15.9, “InnoDB Table and Page Compression” for requirements associated with the 有关压缩行格式的相关要求,请参阅第15.9节,“COMPRESSED
row format.InnoDB
表格和页面压缩”。
The row format used in older versions of MySQL can still be requested by specifying the 通过指定REDUNDANT
row format.REDUNDANT
行格式,仍然可以请求MySQL旧版本中使用的行格式。
When you specify a non-default 当指定非默认ROW_FORMAT
clause, consider also enabling the innodb_strict_mode
configuration option.ROW_FORMAT
子句时,还要考虑启用innodb_strict_mode
配置选项。
不支持ROW_FORMAT=FIXED
is not supported. ROW_FORMAT=FIXED
。If 如果在禁用ROW_FORMAT=FIXED
is specified while innodb_strict_mode
is disabled, InnoDB
issues a warning and assumes ROW_FORMAT=DYNAMIC
. innodb_strict_mode
时指定了ROW_FORMAT=FIXED
,InnoDB
将发出警告并假定ROW_FORMAT=DYNAMIC
。If 如果在启用ROW_FORMAT=FIXED
is specified while innodb_strict_mode
is enabled, which is the default, InnoDB
returns an error.innodb_strict_mode
(默认模式)时指定了ROW_FORMAT=FIXED
,InnoDB
将返回一个错误。
For additional information about 有关InnoDB
row formats, see Section 15.10, “InnoDB Row Formats”.InnoDB
行格式的更多信息,请参阅第15.10节,“InnoDB
行格式”。
For 对于MyISAM
tables, the option value can be FIXED
or DYNAMIC
for static or variable-length row format. MyISAM
表,对于静态或可变长度行格式,选项值可以是FIXED
,也可以是DYNAMIC
。myisampack sets the type to COMPRESSED
. myisampack
将类型设置为COMPRESSED
。See Section 16.2.3, “MyISAM Table Storage Formats”.请参阅第16.2.3节,“MyISAM
表格存储格式”。
For 对于NDB
tables, the default ROW_FORMAT
is DYNAMIC
.NDB
表,默认的是
DYNAMIC
。
STATS_AUTO_RECALC
Specifies whether to automatically recalculate persistent statistics for an 指定是否自动重新计算InnoDB
table. InnoDB
表的持久统计信息。The value 默认值导致表的持久统计设置由DEFAULT
causes the persistent statistics setting for the table to be determined by the innodb_stats_auto_recalc
configuration option. innodb_stats_auto_recalc
配置选项确定。The value 当表中10%的数据发生更改时,值1
causes statistics to be recalculated when 10% of the data in the table has changed. 1
会导致重新计算统计信息。The value 值0
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. 0
防止自动重新计算此表;使用此设置,在对表进行重大更改后,发出ANALYZE TABLE
语句以重新计算统计信息。For more information about the persistent statistics feature, see Section 15.8.10.1, “Configuring Persistent Optimizer Statistics Parameters”.有关持久性统计特性的更多信息,请参阅第15.8.10.1节,“配置持久性优化器统计参数”。
STATS_PERSISTENT
Specifies whether to enable persistent statistics for an 指定是否为InnoDB
table. InnoDB
表启用持久统计信息。The value DEFAULT
causes the persistent statistics setting for the table to be determined by the innodb_stats_persistent
configuration option. DEFAULT
值导致表的持久统计设置由innodb_stats_persistent
配置选项确定。The value 值1
enables persistent statistics for the table, while the value 0
turns off this feature. 1
启用表的持久统计信息,而值0
禁用此功能。After enabling persistent statistics through a 通过CREATE TABLE
or ALTER TABLE
statement, issue an ANALYZE TABLE
statement to calculate the statistics, after loading representative data into the table. CREATE TABLE
或ALTER TABLE
语句启用持久统计信息后,在将代表性数据加载到表中后,发出ANALYZE TABLE
语句以计算统计信息。For more information about the persistent statistics feature, see Section 15.8.10.1, “Configuring Persistent Optimizer Statistics Parameters”.有关持久性统计特性的更多信息,请参阅第15.8.10.1节,“配置持久性优化器统计参数”。
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 在估计索引列的基数和其他统计信息(如由ANALYZE TABLE
. ANALYZE TABLE
计算的统计信息)时要采样的索引页数。For more information, see Section 15.8.10.1, “Configuring Persistent Optimizer Statistics Parameters”.有关更多信息,请参阅第15.8.10.1节,“配置持久优化器统计参数”。
TABLESPACE
The TABLESPACE
clause can be used to create a table in an existing general tablespace, a file-per-table tablespace, or the system tablespace.TABLESPACE
子句可用于在现有常规表空间、每个表的文件表空间或系统表空间中创建表。
CREATE TABLEtbl_name
... TABLESPACE [=]tablespace_name
The general tablespace that you specify must exist prior to using the 在使用TABLESPACE
clause. TABLESPACE
子句之前,指定的常规表空间必须存在。For information about general tablespaces, see Section 15.6.3.3, “General Tablespaces”.有关常规表空间的信息,请参阅第15.6.3.3节,“常规表空间”。
The
is a case-sensitive identifier. tablespace_name
tablespace_name
是区分大小写的标识符。It may be quoted or unquoted. 它可以是带引号的,也可以是不带引号的。The forward slash character (“/”) is not permitted. 不允许使用正斜杠字符(“/”)。Names beginning with “innodb_” are reserved for special use.以“innodb_”开头的名称保留供特殊使用。
To create a table in the system tablespace, specify 要在系统表空间中创建表,请指定innodb_system
as the tablespace name.innodb_system
作为表空间名称。
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. TABLESPACE [=] innodb_system
,您可以在系统表空间中放置任何未压缩行格式的表,而无需考虑innodb_file_per_table
设置。For example, you can add a table with 例如,可以使用ROW_FORMAT=DYNAMIC
to the system tablespace using TABLESPACE [=] innodb_system
.TABLESPACE [=] innodb_system
将ROW_FORMAT=DYNAMIC
的表添加到系统表空间。
To create a table in a file-per-table tablespace, specify 要在每个表空间的文件中创建表,请指定innodb_file_per_table
as the tablespace name.innodb_file_per_table
作为表空间名称。
CREATE TABLE tbl_name
... TABLESPACE [=] innodb_file_per_table
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_file_per_table
,则无需指定TABLESPACE=innodb_file_per_table
来创建InnoDB
file-per-table表空间。默认情况下,当启用InnoDB
tables are created in file-per-table tablespaces by default when innodb_file_per_table
is enabled.innodb_file_per_table
时,InnoDB
表将在每个表的文件表空间中创建。
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
clause. DATA DIRECTORY
子句允许与CREATE TABLE ... TABLESPACE=innodb_file_per_table
结合使用,但不支持与TABLESPACE
子句结合使用。As of MySQL 8.0.21, the directory specified in a 从MySQL 8.0.21开始,DATA DIRECTORY
clause must be known to InnoDB
. InnoDB
必须知道DATA DIRECTORY
子句中指定的目录。For more information, see Using the DATA DIRECTORY Clause.有关更多信息,请参阅使用DATA DIRECTORY子句。
Support for 从MySQL 8.0.13开始,不推荐使用TABLESPACE = innodb_file_per_table
and TABLESPACE = innodb_temporary
clauses with CREATE TEMPORARY TABLE
is deprecated as of MySQL 8.0.13; expect it to be removed in a future version of MySQL.TABLESPACE=innodb_file_per_table
和带有CREATE TEMPORARY TABLE
的TABLESPACE=innodb_temporary
子句;预计它将在MySQL的未来版本中删除。
The STORAGE
table option is employed only with NDB
tables. STORAGE
表选项仅用于NDB
表。STORAGE
determines the type of storage used (disk or memory), and can be either DISK
or MEMORY
.STORAGE
决定使用的存储类型(磁盘或内存),可以是DISK
或MEMORY
。
TABLESPACE ... STORAGE DISK
assigns a table to an NDB Cluster Disk Data tablespace. TABLESPACE ... STORAGE DISK
将表分配给NDB群集磁盘数据表空间。The tablespace must already have been created using 表空间必须已经使用CREATE TABLESPACE
. CREATE TABLESPACE
创建。See Section 23.5.10, “NDB Cluster Disk Data Tables”, for more information.有关更多信息,请参阅第23.5.10节,“NDB群集磁盘数据表”。
A 如果没有STORAGE
clause cannot be used in a CREATE TABLE
statement without a TABLESPACE
clause.TABLESPACE
子句,则不能在CREATE TABLE
语句中使用STORE
子句。
Used to access a collection of identical 用于作为一个表访问相同MyISAM
tables as one. MyISAM
表的集合。This works only with 这仅适用于MERGE
tables. MERGE
表。See Section 16.7, “The MERGE Storage Engine”.请参阅第16.7节,“合并存储引擎”。
You must have 对于映射到SELECT
, UPDATE
, and DELETE
privileges for the tables you map to a MERGE
table.MERGE
表的表,您必须具有SELECT
、UPDATE
和DELETE
权限。
Formerly, all tables used had to be in the same database as the 以前,使用的所有表必须与MERGE
table itself. MERGE
表本身位于同一数据库中。This restriction no longer applies.这一限制不再适用。
partition_options
can be used to control partitioning of the table created with CREATE TABLE
.partition_options
可用于控制使用CREATE TABLE
创建的表的分区。
Not all options shown in the syntax for 并非本节开头的partition_options
at the beginning of this section are available for all partitioning types. partition_options
语法中显示的所有选项都适用于所有分区类型。Please see the listings for the following individual types for information specific to each type, and see Chapter 24, 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.有关每种类型的特定信息,请参阅以下各个类型的列表;有关MySQL分区的工作和使用的更完整信息,请参见第24章,“分区”,以及表创建的其他示例和与MySQL分区相关的其他语句。
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.9, “ALTER TABLE Statement”. 分区可以修改、合并、添加到表中,也可以从表中删除。有关完成这些任务的MySQL语句的基本信息,请参阅第13.1.9节,“ALTER TABLE语句”。For more detailed descriptions and examples, see Section 24.3, “Partition Management”.有关更详细的说明和示例,请参阅第24.3节,“分区管理”。
PARTITION BY
If used, a 如果使用,partition_options
clause begins with PARTITION BY
. partition_options
子句以PARTITION BY
开头。This clause contains the function that is used to determine the partition; the function returns an integer value ranging from 1 to 此子句包含用于确定分区的函数;该函数返回一个从1到num
, where num
is the number of partitions. num
的整数值,其中num
是分区数。(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.)(一个表可以包含的用户定义分区的最大数量为1024;本节后面讨论的子分区数量包含在该最大值中。)
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. PARTITION BY
子句中使用的表达式(expr
)不能引用不在正在创建的表中的任何列;这种引用是特别不允许的,并且会导致语句失败并出现错误。(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. expr
是使用一个或多个表列的表达式。This can be any valid MySQL expression (including MySQL functions) that yields a single integer value. 这可以是任何生成单个整数值的有效MySQL表达式(包括MySQL函数)。For example, these are both valid 例如,这两条语句都是使用CREATE TABLE
statements using PARTITION BY HASH
:PARTITION BY HASH
的有效CREATE TABLE
语句:
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
.VALUES LESS THAN
子句或VALUES IN
子句配合PARTITION BY HASH
。
PARTITION BY HASH
uses the remainder of expr
divided by the number of partitions (that is, the modulus). PARTITION BY HASH
使用expr
的剩余部分除以分区数(即模数)。For examples and additional information, see Section 24.2.4, “HASH Partitioning”.有关示例和其他信息,请参阅第24.2.4节,“哈希分区”。
The LINEAR
keyword entails a somewhat different algorithm. LINEAR
关键字需要一个稍微不同的算法。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. AND
操作的结果,计算存储行的分区数。For discussion and examples of linear hashing, see Section 24.2.4.1, “LINEAR HASH Partitioning”.有关线性哈希的讨论和示例,请参阅第24.2.4.1节,“线性哈希分区”。
KEY(
column_list
)
This is similar to 这类似于HASH
, except that MySQL supplies the hashing function so as to guarantee an even data distribution. HASH
,只是MySQL提供了散列函数以保证数据分布均匀。The column_list
argument is simply a list of 1 or more table columns (maximum: 16). column_list
参数只是一个包含1个或多个表列的列表(最大值:16)。This example shows a simple table partitioned by key, with 4 partitions:此示例显示了一个按键分区的简单表,共有4个分区:
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. LINEAR
关键字采用线性分区。This has the same effect as with tables that are partitioned by 这与使用HASH
. HASH
分区的表具有相同的效果。That is, the partition number is found using the 也就是说,分区号是使用&
operator rather than the modulus (see Section 24.2.4.1, “LINEAR HASH Partitioning”, and Section 24.2.5, “KEY Partitioning”, for details). &
运算符而不是模数(有关详细信息,请参阅第24.2.4.1节,“线性哈希分区”和第24.2.5节,“键分区”)。This example uses linear partitioning by key to distribute data between 5 partitions:此示例使用按键的线性分区在5个分区之间分配数据:
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 | 2}
选项与[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 implemented and used by default for new KEY
partitioned tables in MySQL 5.5 and later. ALGORITHM=1
导致服务器使用与MySQL 5.1相同的密钥散列函数;ALGORITHM=2
意味着服务器使用默认情况下为MySQL 5.5及更高版本中的新KEY
分区表实现和使用的密钥散列函数。(Partitioned tables created with the key-hashing functions employed in MySQL 5.5 and later cannot be used by a MySQL 5.1 server.) (使用MySQL 5.5及更高版本中使用的密钥散列函数创建的分区表不能由MySQL 5.1服务器使用。)Not specifying the option has the same effect as using 不指定该选项的效果与使用ALGORITHM=2
. ALGORITHM=2
的效果相同。This option is intended for use chiefly when upgrading or downgrading 此选项主要用于在MySQL 5.1和更高版本的MySQL之间升级或降级[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. [LINEAR] KEY
分区表,或在MySQL 5.5或更高版本的服务器上创建可在MySQL 5.1服务器上使用的按键或线性键分区的表。For more information, see Section 13.1.9.1, “ALTER TABLE Partition Operations”.有关更多信息,请参阅第13.1.9.1节,“更改表分区操作”。
mysqldump in MySQL 5.7 (and later) writes this option encased in versioned comments, like this:MySQL 5.7(及更高版本)中的mysqldump将此选项封装在版本注释中,如下所示:
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. 这会导致MySQL 5.6.10和更早版本的服务器忽略该选项,否则会在这些版本中导致语法错误。If you plan to load a dump made on a MySQL 5.7 server where you use tables that are partitioned or subpartitioned by 如果您计划将MySQL 5.7服务器上的转储加载到5.6.11版本之前的MySQL 5.6服务器中,在该服务器上使用按KEY
into a MySQL 5.6 server previous to version 5.6.11, be sure to consult Changes in MySQL 5.6, before proceeding. KEY
分区或子分区的表,请确保在继续之前查阅MySQL 5.6中的更改。(The information found there also applies if you are loading a dump containing (如果您正在将包含从MySQL 5.7-5.6.11或更高版本服务器生成的KEY
partitioned or subpartitioned tables made from a MySQL 5.7—actually 5.6.11 or later—server into a MySQL 5.5.30 or earlier server.)KEY
分区表或子分区表的转储加载到MySQL 5.5.30或更高版本服务器,则此处找到的信息也适用。)
Also in MySQL 5.6.11 and later, 同样在MySQL 5.6.11及更高版本中,必要时,在ALGORITHM=1
is shown when necessary in the output of SHOW CREATE TABLE
using versioned comments in the same manner as mysqldump. SHOW CREATE TABLE
的输出中使用版本化注释以与mysqldump相同的方式显示ALGORITHM=1
。ALGORITHM=2
is always omitted from SHOW CREATE TABLE
output, even if this option was specified when creating the original table.SHOW CREATE TABLE
输出中始终忽略ALGORITHM=2
,即使在创建原始表时指定了此选项。
You may not use either 您不能将VALUES LESS THAN
or VALUES IN
clauses with PARTITION BY KEY
.VALUES LESS THAN
或VALUES IN
子句与PARTITION BY KEY
配合使用。
RANGE(
expr
)
In this case, 在这种情况下,expr
shows a range of values using a set of VALUES LESS THAN
operators. expr
使用一组VALUES LESS THAN
运算符的值来显示一系列值。When using range partitioning, you must define at least one partition using 使用范围分区时,必须使用VALUES LESS THAN
. VALUES LESS THAN
定义至少一个分区。You cannot use 不能将VALUES IN
with range partitioning.VALUES IN
与范围分区配合使用。
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. RANGE
分区的表,VALUES LESS THAN
必须与整型文字值或计算为单个整型值的表达式一起使用。In MySQL 8.0, you can overcome this limitation in a table that is defined using 在MySQL 8.0中,您可以在使用PARTITION BY RANGE COLUMNS
, as described later in this section.PARTITION BY RANGE COLUMNS
定义的表中克服此限制,如本节后面所述。
Suppose that you have a table that you wish to partition on a column containing year values, according to the following scheme.假设您有一个表,希望根据以下方案在包含年份值的列上进行分区。
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 实现这种分区方案的表可以通过如下所示的CREATE TABLE
statement shown here:CREATE TABLE
语句来实现:
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. PARTITION ... VALUES LESS THAN ...
语句以连续的方式工作。VALUES LESS THAN MAXVALUE
works to specify “leftover” values that are greater than the maximum value otherwise specified.VALUES LESS THAN MAXVALUE
用于指定大于其他指定的最大值的“剩余”值。
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). VALUES LESS THAN
子句以类似于switch ... case
块(在许多编程语言如C、Java和PHP中都可以找到)的case
部分的方式顺序工作。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.MAXVALUE
的值位于列表中的最后一个值。
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
). RANGE
变量有助于使用多列上的范围条件(即,具有诸如WHERE a = 1 AND b < 10
或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 (
partition definition clause. value_list
)COLUMNS
子句中的列列表和每个PARTITION ... VALUES LESS THAN (
分区定义子句中的列值集来指定多列中的值范围。value_list
)(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
is 16.column_list
和value_list
中可引用的最大列数为16。
The 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
子句中使用的column_list
可能只包含列的名称;列表中的每一列必须是以下MySQL数据类型之一:整数类型;字符串类型;和时间或日期列类型。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. BLOB
、TEXT
、SET
、ENUM
、BIT
或空间数据类型的列;也不允许使用浮点数类型的列。You also may not use functions or arithmetic expressions in the 您也不能在COLUMNS
clause.COLUMNS
子句中使用函数或算术表达式。
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. VALUES LESS THAN
子句必须为COLUMNS()
子句中出现的每一列指定一个文字值;也就是说,用于每个VALUES LESS THAN
子句的值列表必须包含与COLUMNS
子句中列出的列相同数目的值。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.... VALUES LESS THAN
子句中使用比COLUMNS
子句中更多或更少的值,则会导致语句失败,因为分区列列表的使用不一致……。You cannot use 对于NULL
for any value appearing in VALUES LESS THAN
. VALUES LESS THAN
中出现的任何值,不能使用NULL
。It is possible to use 对于给定列(第一列除外),可以多次使用MAXVALUE
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 (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. VALUES LESS THAN
值列表中使用的每个值必须与相应列的类型完全匹配;未进行任何转换。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'
).'1'
用于与使用整数类型的列匹配的值(必须改用数字1
),也不能将数字1
用于与使用字符串类型的列匹配的值(在这种情况下,必须使用带引号的字符串:'1'
)。
For more information, see Section 24.2.1, “RANGE Partitioning”, and Section 24.4, “Partition Pruning”.有关更多信息,请参阅第24.2.1节,“范围分区”和第24.4节,“分区修剪”。
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.RANGE
类似,不同的是只有VALUES IN
可用于指定每个分区的允许值。
VALUES IN
is used with a list of values to be matched. VALUES IN
与要匹配的值列表一起使用。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
. VALUES IN
定义至少一个分区。You cannot use 不能将VALUES LESS THAN
with PARTITION BY LIST
.VALUES LESS THAN
与PARTITION BY LIST
配合使用。
For tables partitioned by 对于按LIST
, the value list used with VALUES IN
must consist of integer values only. LIST
分区的表,与VALUES IN
一起使用的值列表必须仅包含整数值。In MySQL 8.0, you can overcome this limitation using partitioning by 在MySQL 8.0中,您可以使用按LIST COLUMNS
, which is described later in this section.LIST COLUMNS
进行分区来克服这一限制,本节稍后将对此进行描述。
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
). LIST
上的这个变量有助于使用多列上的比较条件(即,具有诸如WHERE a = 5 AND b = 5
或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 (
partition definition clause.value_list
)COLUMNS
子句中的列列表和每个PARTITION ... VALUES IN (
分区定义子句中的列值集来指定多列中的值。value_list
)
The rules governing regarding data types for the column list used in 关于LIST COLUMNS(
and the value list used in column_list
)VALUES IN(
are the same as those for the column list used in value_list
)RANGE COLUMNS(
and the value list used in column_list
)VALUES LESS THAN(
, respectively, except that in the value_list
)VALUES IN
clause, MAXVALUE
is not permitted, and you may use NULL
.LIST COLUMNS(
中使用的列列表和column_list
)VALUES IN(
中使用的值列表的数据类型的规则分别与value_list
)RANGE COLUMNS(
中使用的列列表和column_list
)VALUES LESS THAN(
中使用的值列表的规则相同,但value_list
)子句除外,不允许使用
MAXVALU
E,您可以使用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
. PARTITION BY LIST COLUMNS
中的值的值列表与用于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). PARTITION BY LIST COLUMNS
一起使用时,VALUES IN
子句中的每个元素都必须是一组列值;每个集合中的值数必须与COLUMNS
子句中使用的列数相同,并且这些值的数据类型必须与列的数据类型匹配(并且以相同的顺序出现)。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
is 16.column_list
和组成value_list
的元素中可使用的最大列数为16。
The table defined by the following 以下CREATE TABLE
statement provides an example of a table using LIST COLUMNS
partitioning:CREATE TABLE
语句定义的表提供了一个使用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) ) );
PARTITIONS
num
The number of partitions may optionally be specified with a 分区数可以选择使用PARTITIONS
clause, where num
num
is the number of partitions. PARTITIONS
子句指定,其中num
num
是分区数。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.PARTITION
子句,num
必须等于使用PARTITION
子句声明的任何分区的总数。
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).RANGE
或按RANGE
分区的表时是否使用PARTITIONS
子句,表定义中仍必须至少包含一个PARTIONE VALUES
子句(请参见下文)。
SUBPARTITION BY
A partition may optionally be divided into a number of subpartitions. 分区可以选择性地划分为若干子分区。This can be indicated by using the optional 这可以通过使用可选的SUBPARTITION BY
clause. SUBPARTITION BY
子句来表示。Subpartitioning may be done by 子分区可以通过HASH
or KEY
. HASH
或KEY
来完成。Either of these may be 这两者都可以是LINEAR
. 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
.)LIST
或按RANGE
细分。)
The number of subpartitions can be indicated using the 子分区的数量可以使用SUBPARTITIONS
keyword followed by an integer value.SUBPARTITIONS
关键字后跟一个整数值来表示。
Rigorous checking of the value used in 对PARTITIONS
or SUBPARTITIONS
clauses is applied and this value must adhere to the following rules:PARTITIONS
或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, 例如,PARTITIONS 0.2E+01
is not permitted, even though 0.2E+01
evaluates to 2
. PARTITIONS 0.2E+01
是不允许的,即使0.2E+01
的计算结果为2
。(Bug #15890)
partition_definition
Each partition may be individually defined using a 可以使用partition_definition
clause. partition_definition
子句单独定义每个分区。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. VALUES LESS THAN
子句;对于列表分区,必须为每个分区指定一个VALUES IN
子句。This is used to determine which rows are to be stored in this partition. 这用于确定哪些行要存储在此分区中。See the discussions of partitioning types in Chapter 24, Partitioning, for syntax examples.有关语法示例,请参阅第24章,“分区”中有关分区类型的讨论。
[STORAGE] ENGINE
MySQL accepts a MySQL接受[STORAGE] ENGINE
option for both PARTITION
and SUBPARTITION
. PARTITION
和SUBPARTITION
的[STORAGE] ENGINE
选项。Currently, the only way in which this option 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.目前,使用此选项的唯一方法是将所有分区或所有子分区设置为同一个存储引擎,如果试图为同一个表中的分区或子分区设置不同的存储引擎,则会引发错误ERROR 1469(HY000):此版本的MySQL不允许分区中混合使用处理程序。
COMMENT
An optional 可选的COMMENT
clause may be used to specify a string that describes the partition. COMMENT
子句可用于指定描述分区的字符串。Example:例子:
COMMENT = 'Data for the years previous to 1999'
The maximum length for a partition comment is 1024 characters.分区注释的最大长度为1024个字符。
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. DATA DIRECTORY
和INDEX DIRECTORY
可用于分别指示存储此分区的数据和索引的目录。Both the
and the data_dir
must be absolute system path names.index_dir
data_dir
和index_dir
都必须是绝对系统路径名。
As of MySQL 8.0.21, the directory specified in a 从MySQL 8.0.21开始,DATA DIRECTORY
clause must be known to InnoDB
. InnoDB
必须知道DATA DIRECTORY
子句中指定的目录。For more information, see Using the DATA DIRECTORY Clause.有关更多信息,请参阅使用DATA DIRECTORY子句。
You must have the 您必须具有FILE
privilege to use the DATA DIRECTORY
or INDEX DIRECTORY
partition option.FILE
权限才能使用DATA DIRECTORY
或INDEX DIRECTORY
分区选项。
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 CREATE TABLE
statement's table_option
clause as used for MyISAM
tables.DATA DIRECTORY
和INDEX DIRECTORY
的行为方式与CREATE TABLE
语句的table_option
子句中用于MyISAM
表的方式相同。
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.如果未指定,数据和索引默认存储在表的数据库目录中。
The 如果在创建分区表时,DATA DIRECTORY
and INDEX DIRECTORY
options are ignored for creating partitioned tables if NO_DIR_IN_CREATE
is in effect.NO_DIR_IN_CREATE
有效,则会忽略DATA DIRECTORY
和INDEX DIRECTORY
选项。
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. max_number_of_rows
和min_number_of_rows
的值必须是正整数。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 an 可以通过指定InnoDB
file-per-table tablespace for the partition by specifying TABLESPACE `innodb_file_per_table`
. TABLESPACE `innodb_file_per_table`
来为分区指定每个表空间的InnoDB
文件。All partitions must belong to the same storage engine.所有分区必须属于同一个存储引擎。
Placing 不支持在共享InnoDB
table partitions in shared InnoDB
tablespaces is not supported. InnoDB
表空间中放置InnoDB
表分区。Shared tablespaces include the 共享表空间包括InnoDB
system tablespace and general tablespaces.InnoDB
系统表空间和通用表空间。
subpartition_definition
The partition definition may optionally contain one or more 分区定义可以选择性地包含一个或多个subpartition_definition
clauses. subpartition_definition
子句。Each of these consists at a minimum of the 每个子分区至少包含SUBPARTITION
, where name
name
is an identifier for the subpartition. SUBPARTITION
,其中name
name
是子分区的标识符。Except for the replacement of the 除了用PARTITION
keyword with SUBPARTITION
, the syntax for a subpartition definition is identical to that for a partition definition.SUBPARTITION
替换PARTITION
关键字外,SUBPARTITION
定义的语法与PARTITION
定义的语法相同。
Subpartitioning must be done by 子分区必须通过HASH
or KEY
, and can be done only on RANGE
or LIST
partitions. HASH
或KEY
完成,并且只能在RANGE
分区或LIST
分区上完成。See Section 24.2.6, “Subpartitioning”.请参阅第24.2.6节,“子分区”。
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 Section 24.6.3, “Partitioning Limitations Relating to Functions”). 分区将生成的列视为常规列,这为分区不允许的函数限制提供了变通方法(请参阅第24.6.3节,“与函数相关的分区限制”)。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.EXP()
不能直接在PARTITION BY
子句中使用,但允许使用EXP()
定义生成的列。