CREATE TABLE LIKE

CREATE TABLE LIKE 语句用于复制已有表的定义,但不复制任何数据。

语法图

CreateTableLikeStmt

CREATE TABLE LIKE - 图1

OptTemporary

CREATE TABLE LIKE - 图2

LikeTableWithOrWithoutParen

CREATE TABLE LIKE - 图3

OnCommitOpt

CREATE TABLE LIKE - 图4

  1. CreateTableLikeStmt ::=
  2. 'CREATE' OptTemporary 'TABLE' IfNotExists TableName LikeTableWithOrWithoutParen OnCommitOpt
  3. OptTemporary ::=
  4. ( 'TEMPORARY' | ('GLOBAL' 'TEMPORARY') )?
  5. LikeTableWithOrWithoutParen ::=
  6. 'LIKE' TableName
  7. | '(' 'LIKE' TableName ')'
  8. OnCommitOpt ::=
  9. ('ON' 'COMMIT' 'DELETE' 'ROWS')?

示例

  1. CREATE TABLE t1 (a INT NOT NULL);
  1. Query OK, 0 rows affected (0.13 sec)
  1. INSERT INTO t1 VALUES (1),(2),(3),(4),(5);
  1. Query OK, 5 rows affected (0.02 sec)
  2. Records: 5 Duplicates: 0 Warnings: 0
  1. SELECT * FROM t1;
  1. +---+
  2. | a |
  3. +---+
  4. | 1 |
  5. | 2 |
  6. | 3 |
  7. | 4 |
  8. | 5 |
  9. +---+
  10. 5 rows in set (0.00 sec)
  1. CREATE TABLE t2 LIKE t1;
  1. Query OK, 0 rows affected (0.10 sec)
  1. SELECT * FROM t2;
  1. Empty set (0.00 sec)

Region 的预切分

如果被复制的表定义了 PRE_SPLIT_REGIONS 属性,则通过 CREATE TABLE LIKE 语句复制的表,会继承该属性并在建表时预切分 Region。关于 PRE_SPLIT_REGIONS 属性的说明,参见 CREATE TABLE 语句

MySQL 兼容性

CREATE TABLE LIKE 语句与 MySQL 完全兼容。如发现任何兼容性差异,请尝试 TiDB 支持资源

另请参阅