mysql复制表结构和数据,mysql 表结构复制方法
2023-07-05 15:46 浏览: 次mysql复制表结构和数据,mysql中复制表结构
MySQL 中使用 命令行 复制表结构及数据的方法主要有以下几种:
1、只复制表结构
CREATE TABLE new_table SELECT * FROM old_table WHERE 1=2;
或者:
CREATE TABLE new_table LIKE old_table;
注意: 上面两种方式,第1种方式是 复制时,主键类型和自增方式是不会复制过去的;而第2种是把 old_table 的所有字段类型都复制到。
2、复制表结构和数据
CREATE TABLE new_table SELECT * FROM old_table;
3、复制旧表中的数据到新表中(假设:两个表的表结构是一样的)
INSERT INTO new_table SELECT * FROM old_table;
CREATE TABLE new_table LIKE old_table;
实例演示 :
mysql> create table tab_result_2020 like tab_result; Query OK, 0 rows affected (0.24 sec) mysql> desc tab_result_2020; +------------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------+--------------+------+-----+---------+-------+ | lotteryid | int(11) | NO | PRI | NULL | | | lotteryissue | varchar(12) | NO | PRI | NULL | | | ballid | varchar(10) | NO | PRI | NULL | | | source | varchar(100) | NO | PRI | NULL | | | hteam_half_score | varchar(30) | YES | | NULL | | | vteam_half_score | varchar(30) | YES | | NULL | | | hteam_full_score | varchar(30) | YES | | NULL | | | vteam_full_score | varchar(30) | YES | | NULL | | | result | varchar(10) | YES | | NULL | | | addtime | datetime | YES | | NULL | | | status | int(10) | YES | | NULL | | | spv | varchar(30) | YES | | NULL | | | spv_ext | varchar(30) | YES | | NULL | | | cron_times | int(10) | YES | | NULL | | | error_times | int(10) | YES | | NULL | | +------------------+--------------+------+-----+---------+-------+ 15 rows in set (0.01 sec) mysql> desc tab_result; +------------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------+--------------+------+-----+---------+-------+ | lotteryid | int(11) | NO | PRI | NULL | | | lotteryissue | varchar(12) | NO | PRI | NULL | | | ballid | varchar(10) | NO | PRI | NULL | | | source | varchar(100) | NO | PRI | NULL | | | hteam_half_score | varchar(30) | YES | | NULL | | | vteam_half_score | varchar(30) | YES | | NULL | | | hteam_full_score | varchar(30) | YES | | NULL | | | vteam_full_score | varchar(30) | YES | | NULL | | | result | varchar(10) | YES | | NULL | | | addtime | datetime | YES | | NULL | | | status | int(10) | YES | | NULL | | | spv | varchar(30) | YES | | NULL | | | spv_ext | varchar(30) | YES | | NULL | | | cron_times | int(10) | YES | | NULL | | | error_times | int(10) | YES | | NULL | | +------------------+--------------+------+-----+---------+-------+ 15 rows in set (0.01 sec)
从 tab_result 表中复制数据到 tab_result_2020 表中:
mysql> select count(*) from tab_result where addtime like "2020-%"; +----------+ | count(*) | +----------+ | 337760 | +----------+ 1 row in set, 1 warning (0.30 sec) mysql> insert into tab_result_2020 select * from tab_result where addtime like "2020-%"; Query OK, 337760 rows affected, 1 warning (12.85 sec) Records: 337760 Duplicates: 0 Warnings: 0
4、复制 旧表中的数据到新表中(假设:两个表的表结构不一样)
【免责声明】:部分内容、图片来源于互联网,如有侵权请联系删除,QQ:228866015