MySQL中复制表(创建表的副本、备份表)

1. 第一种 复制表结构和数据

-- 复制表的结构和数据,但是不会复制表的约束、外键、触发器、索引等
create table test_duplication as select * from test; -- 此处的as可以省略

2. 第二种 只复制表的结构

create table test_duplication as select * from test where 1=0; -- 此处的as可以省略
或者
create table test_duplication like select * from test where 1=0;

3. 第三种,只复制表数据

insert into test_duplication select * from test; -- 复制全部的列,这要求表结构要一致
insert into test_duplication (id, username) select id, username from test; -- 复制部分字段

4. 第四种,通过拷贝建表语句拷贝所有的结构

SHOW CREATE TABLE test; -- 得到表的定义
更改表名执行一遍

参考资料

参考链接-程序一逸

posted on 2024-02-05 20:16  wucanqin  阅读(23)  评论(0编辑  收藏  举报

导航