当前位置: 移动技术网 > IT编程>数据库>Mysql > mysql如何把一个表直接拷贝到一个新的表

mysql如何把一个表直接拷贝到一个新的表

2018年08月03日  | 移动技术网IT编程  | 我要评论

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

1,拷贝所有的字段

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)

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网