1.复制表结构

create table table_new as select * from  table_old where 1=0;  	

或者

create table table_new as select * from table_old where  1<>1

或者

create table table_name_new like table_name_old

2.复制表结构和数据:

create table table_new as select * from table_old 

3.复制表的指定字段

create table table_new as select  column1,column2... from table_old where  1<>1 (前提是column1...是table_old 的列)

4、复制表的指定字段及数据:

create table new_table  as select  column1,column2... from old_table where(前提是column1...是old_table的列)

5.复制表数据

A.两个表结构一样

insert into table_new select * from table_old ;(前提是必须要有一个new_table 表才能查数据)

B.表结构不一样

insert into table_new ( column1,column2...select  column1,column2... from table_old (注意:两个表中的要复制的列数据类型和长度最好要一致,要注意长度大小问题)

以上语句能根据已有的表来创建新表及数据,但是已有表的索引和主键却复制不了,需要在新表中手动建立,而且注释什么的都不会被复制过来的。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐