CREATE TABLE LIKE
CREATE TABLE LIKE
语句用于复制已有表的定义,但不复制任何数据。
语法图
- CreateTableLikeStmt
- OptTemporary
- LikeTableWithOrWithoutParen
- OnCommitOpt
CreateTableLikeStmt ::=
'CREATE' OptTemporary 'TABLE' IfNotExists TableName LikeTableWithOrWithoutParen OnCommitOpt
OptTemporary ::=
( 'TEMPORARY' | ('GLOBAL' 'TEMPORARY') )?
LikeTableWithOrWithoutParen ::=
'LIKE' TableName
| '(' 'LIKE' TableName ')'
OnCommitOpt ::=
('ON' 'COMMIT' 'DELETE' 'ROWS')?
示例
CREATE TABLE t1 (a INT NOT NULL);
Query OK, 0 rows affected (0.13 sec)
INSERT INTO t1 VALUES (1),(2),(3),(4),(5);
Query OK, 5 rows affected (0.02 sec)
Records: 5 Duplicates: 0 Warnings: 0
SELECT * FROM t1;
+---+
| a |
+---+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+---+
5 rows in set (0.00 sec)
CREATE TABLE t2 LIKE t1;
Query OK, 0 rows affected (0.10 sec)
SELECT * FROM t2;
Empty set (0.00 sec)
Region 的预切分
如果被复制的表定义了 PRE_SPLIT_REGIONS
属性,则通过 CREATE TABLE LIKE
语句复制的表,会继承该属性并在建表时预切分 Region。关于 PRE_SPLIT_REGIONS
属性的说明,参见 CREATE TABLE
语句。
MySQL 兼容性
CREATE TABLE LIKE
语句与 MySQL 完全兼容。如发现任何兼容性差异,请在 GitHub 上提交 issue。