13.1.20 CREATE TABLE Statement语句

13.1.20.1 Files Created by CREATE TABLE由CREATE TABLE创建的文件
13.1.20.2 CREATE TEMPORARY TABLE Statement语句
13.1.20.3 CREATE TABLE ... LIKE Statement语句
13.1.20.4 CREATE TABLE ... SELECT Statement语句
13.1.20.5 FOREIGN KEY Constraints外键约束
13.1.20.6 CHECK Constraints检查约束
13.1.20.7 Silent Column Specification Changes静默列规范更改
13.1.20.8 CREATE TABLE and Generated ColumnsCREATE TABLE和生成的列
13.1.20.9 Secondary Indexes and Generated Columns二级索引和生成的列
13.1.20.10 Invisible Columns不可见列
13.1.20.11 Setting NDB_TABLE Options设置NDB_TABLE选项
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
    (create_definition,...)
    [table_options]
    [partition_options]

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
    [(create_definition,...)]
    [table_options]
    [partition_options]
    [IGNORE | REPLACE]
    [AS] query_expression

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
    { LIKE old_tbl_name | (LIKE old_tbl_name) }
create_definition: {
col_name column_definition
  | {INDEX | KEY} [index_name] [index_type] (key_part,...)
      [index_option] ...
  | {FULLTEXT | SPATIAL} [INDEX | KEY] [index_name] (key_part,...)
      [index_option] ...
  | [CONSTRAINT [symbol]] PRIMARY KEY
      [index_type] (key_part,...)
      [index_option] ...
  | [CONSTRAINT [symbol]] UNIQUE [INDEX | KEY]
      [index_name] [index_type] (key_part,...)
      [index_option] ...
  | [CONSTRAINT [symbol]] FOREIGN KEY
      [index_name] (col_name,...)
reference_definition
  | check_constraint_definition
}
column_definition: {
data_type [NOT NULL | NULL] [DEFAULT {literal | (expr)} ]
      [VISIBLE | INVISIBLE]
      [AUTO_INCREMENT] [UNIQUE [KEY]] [[PRIMARY] KEY]
      [COMMENT 'string']
      [COLLATE collation_name]
      [COLUMN_FORMAT {FIXED | DYNAMIC | DEFAULT}]
      [ENGINE_ATTRIBUTE [=] 'string']
      [SECONDARY_ENGINE_ATTRIBUTE [=] 'string']
      [STORAGE {DISK | MEMORY}]
      [reference_definition]
      [check_constraint_definition]
  | data_type
      [COLLATE collation_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 PARSER parser_name
  | COMMENT 'string'
  | {VISIBLE | INVISIBLE}
  |ENGINE_ATTRIBUTE [=] 'string'
  |SECONDARY_ENGINE_ATTRIBUTE [=] 'string'
}
check_constraint_definition:
    [CONSTRAINT [symbol]] CHECK (expr) [[NOT] ENFORCED]
reference_definition:
    REFERENCES tbl_name (key_part,...)
      [MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]
      [ON DELETE reference_option]
      [ON UPDATE reference_option]
reference_option:
    RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT
table_options:
table_option [[,] table_option] ...
table_option: {
    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
  | TABLESPACE tablespace_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)} }
    [PARTITIONS num]
    [SUBPARTITION BY
        { [LINEAR] HASH(expr)
        | [LINEAR] KEY [ALGORITHM={1 | 2}] (column_list) }
      [SUBPARTITIONS num]
    ]
    [(partition_definition [, partition_definition] ...)]
partition_definition:
    PARTITION partition_name
        [VALUES
            {LESS THAN {(expr | value_list) | MAXVALUE}
            |
            IN (value_list)}]
        [[STORAGE] ENGINE [=] engine_name]
        [COMMENT [=] 'string' ]
        [DATA DIRECTORY [=] 'data_dir']
        [INDEX DIRECTORY [=] 'index_dir']
        [MAX_ROWS [=] max_number_of_rows]
        [MIN_ROWS [=] min_number_of_rows]
        [TABLESPACE [=] tablespace_name]
        [(subpartition_definition [, subpartition_definition] ...)]
subpartition_definition:
    SUBPARTITION logical_name
        [[STORAGE] ENGINE [=] engine_name]
        [COMMENT [=] 'string' ]
        [DATA DIRECTORY [=] 'data_dir']
        [INDEX DIRECTORY [=] 'index_dir']
        [MAX_ROWS [=] max_number_of_rows]
        [MIN_ROWS [=] min_number_of_rows]
        [TABLESPACE [=] tablespace_name]
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语句有几个方面,在本节的以下主题下进行了描述:

Table Name表名称

Temporary Tables临时表

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节,“创建临时表语句”

Table Cloning and Copying表克隆和表复制

Column Data Types and Attributes列数据类型和属性

There is a hard limit of 4096 columns per table, but the effective maximum may be less for a given table and depends on the factors discussed in Section 8.4.7, “Limits on Table Column Count and Row Size”.每个表有4096列的硬限制,但给定表的有效最大值可能较小,取决于第8.4.7节,“表列计数和行大小限制”中讨论的因素。

Indexes, Foreign Keys, and CHECK Constraints索引约束、外键约束和检查约束

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节,“检查约束”

Table Options表选项

Table options are used to optimize the behavior of the table. 表选项用于优化表的行为。In most cases, you do not have to specify any of them. 在大多数情况下,您不必指定它们中的任何一个。These options apply to all storage engines unless otherwise indicated. 除非另有说明,否则这些选项适用于所有存储引擎。Options that do not apply to a given storage engine may be accepted and remembered as part of the table definition. 不适用于给定存储引擎的选项可以作为表定义的一部分被接受和记住。Such options then apply if you later use ALTER TABLE to convert the table to use a different storage engine.如果以后使用ALTER TABLE将表转换为使用其他存储引擎,则这些选项将适用。

Table Partitioning表分区

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节,“分区管理”

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()定义生成的列。

13.1.20.1 Files Created by CREATE TABLE
13.1.20.2 CREATE TEMPORARY TABLE Statement
13.1.20.3 CREATE TABLE ... LIKE Statement
13.1.20.4 CREATE TABLE ... SELECT Statement
13.1.20.5 FOREIGN KEY Constraints
13.1.20.6 CHECK Constraints
13.1.20.7 Silent Column Specification Changes
13.1.20.8 CREATE TABLE and Generated Columns
13.1.20.9 Secondary Indexes and Generated Columns
13.1.20.10 Invisible Columns
13.1.20.11 Setting NDB_TABLE Options