mysql复制表和表数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xj80231314/article/details/86147384

如果我们需要完全的复制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;

猜你喜欢

转载自blog.csdn.net/xj80231314/article/details/86147384