13.1.20.3 CREATE TABLE ... LIKE Statement语句

Use CREATE TABLE ... LIKE to create an empty table based on the definition of another table, including any column attributes and indexes defined in the original table:使用CREATE TABLE ... LIKE以基于另一个表的定义创建空表,包括原始表中定义的任何列属性和索引:

CREATE TABLE new_tbl LIKE orig_tbl;

The copy is created using the same version of the table storage format as the original table. 使用与原始表相同版本的表存储格式创建副本。The SELECT privilege is required on the original table.原始表上需要SELECT权限。

LIKE works only for base tables, not for views.LIKE仅适用于基表,不适用于视图。

Important重要

You cannot execute CREATE TABLE or CREATE TABLE ... LIKE while a LOCK TABLES statement is in effect.LOCK TABLES语句生效时,无法执行CREATE TABLE>CREATE TABLE ... LIKE

CREATE TABLE ... LIKE makes the same checks as CREATE TABLE. CREATE TABLE ... LIKE执行与CREATE TABLE相同的检查。This means that if the current SQL mode is different from the mode in effect when the original table was created, the table definition might be considered invalid for the new mode and cause the statement to fail.这意味着,如果当前SQL模式与创建原始表时有效的模式不同,则表定义可能被视为对新模式无效,并导致语句失败。

For CREATE TABLE ... LIKE, the destination table preserves generated column information from the original table.对于CREATE TABLE ... LIKE,目标表保留从原始表生成的列信息。

For CREATE TABLE ... LIKE, the destination table preserves expression default values from the original table.对于CREATE TABLE ... LIKE,目标表保留原始表中的表达式默认值。

For CREATE TABLE ... LIKE, the destination table preserves CHECK constraints from the original table, except that all the constraint names are generated.对于CREATE TABLE ... LIKE,目标表保留原始表中的CHECK约束,只是生成了所有约束名称。

CREATE TABLE ... LIKE does not preserve any DATA DIRECTORY or INDEX DIRECTORY table options that were specified for the original table, or any foreign key definitions.CREATE TABLE ... LIKE不保留为原始表指定的任何DATA DIRECTORYINDEX DIRECTORY表选项,也不保留任何外键定义。

If the original table is a TEMPORARY table, CREATE TABLE ... LIKE does not preserve TEMPORARY. 如果原始表是TEMPORARY表,则CREATE TABLE ... LIKE并不保留TEMPORARYTo create a TEMPORARY destination table, use CREATE TEMPORARY TABLE ... LIKE.要创建TEMPORARY目标表,请使用CREATE TEMPORARY TABLE ... LIKE

Tables created in the mysql tablespace, the InnoDB system tablespace (innodb_system), or general tablespaces include a TABLESPACE attribute in the table definition, which defines the tablespace where the table resides. mysql表空间、InnoDB系统表空间(InnoDB_system)或常规表空间中创建的表在表定义中包含TABLESPACE属性,该属性定义了表所在的表空间。Due to a temporary regression, CREATE TABLE ... LIKE preserves the TABLESPACE attribute and creates the table in the defined tablespace regardless of the innodb_file_per_table setting. 由于临时回归,CREATE TABLE ... LIKE保留TABLESPACE属性并在定义的表空间中创建表,而不考虑innodb_file_per_table设置。To avoid the TABLESPACE attribute when creating an empty table based on the definition of such a table, use this syntax instead:要在基于空表的定义创建空表时避免TABLESPACE属性,请改用以下语法:

CREATE TABLE new_tbl SELECT * FROM orig_tbl LIMIT 0;

CREATE TABLE ... LIKE operations apply all ENGINE_ATTRIBUTE and SECONDARY_ENGINE_ATTRIBUTE values to the new table.CREATE TABLE ... LIKE操作将所有ENGINE_ATTRIBUTESECONDARY_ENGINE_ATTRIBUTE值应用于新表。