一:在新表已经建立好的情况下

1,拷贝所有的字段

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

insert into new_table select * from old_table

2,拷贝部分字段表

insert into new_table(id,name,sex) select id,name,sex from old_table

3,拷贝部分的行

insert into new_table select  *  from old_table where id="1"

4,拷贝部分的行和字段

insert into new_table(id,name,sex) select id,name,sex form old_table where id='1'

二:在新表还没有建的情况下

方案一:

create table new_table (select * from old_table)

这种方案建的话,只是拷贝的查询的结果,新表不会有主键和索引

方案二:

create table new_table LIKE old_table

该方案只能拷贝表结构到新表中,不会拷贝数据

方案三:

如果要真正的复制一个数据到新表,我们可以直接执行下面的语句

create table new_table LIKE old_table;

insert into new_table select * from old_table;

三:我们也可以操作其它的数据库中的表

create table new_table LIKE ortherdatabase.old_table;

insert into new_table select * from ortherdatabase.old_table;

ortherdatabase.old_table中的ortherdatabase是指定的数据库名

四:我们也可以在新建表时改名字

create table new_table (select id,name as username from old_table)

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄