欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-26 08:48:35
一:在新表已经建立好的情况下 1,拷贝所有的字段 insert into new_table select * from old_table 2,拷贝部分字段表 insert into new_table(id,name,sex) select id,name,sex from old_table ......

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

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)