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
;
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
仅适用于基表,不适用于视图。
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 DIRECTORY
或INDEX DIRECTORY
表选项,也不保留任何外键定义。
If the original table is a 如果原始表是TEMPORARY
table, CREATE TABLE ... LIKE
does not preserve TEMPORARY
. TEMPORARY
表,则CREATE TABLE ... LIKE
并不保留TEMPORARY
。To 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 TABLEnew_tbl
SELECT * FROMorig_tbl
LIMIT 0;
CREATE TABLE ... LIKE
operations apply all ENGINE_ATTRIBUTE
and SECONDARY_ENGINE_ATTRIBUTE
values to the new table.CREATE TABLE ... LIKE
操作将所有ENGINE_ATTRIBUTE
和SECONDARY_ENGINE_ATTRIBUTE
值应用于新表。