MySQL 复制表

复制表结构与数据:CREATE TABLE ... LIKE / AS。

Beginner
Key PointsLIKE 复制结构(含索引),AS 复制数据(可能丢索引)。

两种复制

SQL
-- 只复制结构(含索引、自增),不含数据
CREATE TABLE users_bak LIKE users;

-- 复制结构 + 数据(但索引/默认值可能丢失)
CREATE TABLE users_snap AS SELECT * FROM users;
Output
Query OK, 0 rows affected (0.04 sec)
Query OK, 4 rows affected (0.03 sec)
Records: 4  Duplicates: 0  Warnings: 0
For a real backup use CREATE TABLE ... LIKE then INSERT INTO ... SELECT to keep indexes and constraints; AS SELECT drops them.

Friends & Partners