Loading

Mysql 复制表 ( 表结构、表结构和数据 )

MySQL 中使用 命令行 复制表结构及数据的方法主要有以下几种:

1. 只复制表结构

  • 只复制表结构【 主键类型和自增方式是不会复制过去的 】
create table new_table select * from old_table where 1=2;
  • 复制表的结构【 所有的字段、索引都会复制过去 】
    create table new_table like old_table;

2. 复制表结构和数据

create table new_table select * from old_table;

3. 复制旧表中的数据到新表中【 假设:两个表的表结构是一样的

insert into new_table select * from old_table;

注意: new_table 表必须已存在。 若不存在,则可以用 第1种方式复制表结构: create table new_table like old_table;

 4. 复制旧表中的数据到新表中【 假设:两个表的表结构不一样

insert into new_table(filed1, filed2, ...) select  filed1, filed2, ... from old_table;

注意: new_table 和 old_table 两个表的字段必须一致,否则会出现数据转换错误。 

posted @ 2023-03-02 23:42  Carver大脸猫  阅读(808)  评论(0)    收藏  举报