mysql复制表和表数据

如果我们需要完全的复制MySQL的数据表,包括表的结构,索引,默认值等。 如果仅仅使用CREATE TABLE … SELECT 命令,是无法实现的。
本章节将为大家介绍如何完整的复制MySQL数据表,步骤如下:
使用 SHOW CREATE TABLE 命令获取创建数据表(CREATE TABLE) 语句,该语句包含了原数据表的结构,索引等。
复制以下命令显示的SQL语句,修改数据表名,并执行SQL语句,通过以上命令 将完全的复制数据表结构。
如果你想复制表的内容,你就可以使用 INSERT INTO … SELECT 语句来实现。

步骤一:
获取数据表的完整结构。
–show create table ‘表名称’;

–结果如下:
CREATE TABLE offset (
offset_id bigint(20) NOT NULL AUTO_INCREMENT,
create_time datetime NOT NULL,
update_time datetime NOT NULL,
version bigint(20) DEFAULT NULL,
batch_code varchar(255) DEFAULT NULL,
cargo_code varchar(255) DEFAULT NULL,
expiration_time datetime DEFAULT NULL,
in_out_storage int(11) DEFAULT NULL,
inventory_total_amount int(11) DEFAULT NULL,
offset_amount int(11) DEFAULT NULL,
raw_material_code varchar(255) DEFAULT NULL,
source_code varchar(255) DEFAULT NULL,
station_code varchar(255) DEFAULT NULL,
storage_code varchar(255) DEFAULT NULL,
surplus_amount int(11) DEFAULT NULL,
total_offset_amount int(11) DEFAULT NULL,
unit_cost decimal(19,2) DEFAULT NULL,
PRIMARY KEY (offset_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
步骤二:
复制建表语句,更改表名称,执行sql语句。
mysql> CREATE TABLE offset_clone (
offset_id bigint(20) NOT NULL AUTO_INCREMENT,
create_time datetime NOT NULL,
update_time datetime NOT NULL,
version bigint(20) DEFAULT NULL,
batch_code varchar(255) DEFAULT NULL,
cargo_code varchar(255) DEFAULT NULL,
expiration_time datetime DEFAULT NULL,
in_out_storage int(11) DEFAULT NULL,
inventory_total_amount int(11) DEFAULT NULL,
offset_amount int(11) DEFAULT NULL,
raw_material_code varchar(255) DEFAULT NULL,
source_code varchar(255) DEFAULT NULL,
station_code varchar(255) DEFAULT NULL,
storage_code varchar(255) DEFAULT NULL,
surplus_amount int(11) DEFAULT NULL,
total_offset_amount int(11) DEFAULT NULL,
unit_cost decimal(19,2) DEFAULT NULL,
PRIMARY KEY (offset_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
步骤三:拷贝数据
INSERT INTO clone_tbl (runoob_id,
-> runoob_title,
-> runoob_author,
-> submission_date)
-> SELECT runoob_id,runoob_title,
-> runoob_author,submission_date
-> FROM runoob_tbl;

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值