某些情况下需要生成现有表的精确副本。 create ... select语句不能产生此输出,因为它忽略了索引和默认值。
复制表的过程如下 -
使用show create table来生成详细描述源表的整个结构的create table语句。
编辑语句以给表一个新名称,并执行它。
如果还需要复制表数据,请使用insert into ... select语句。
mysql> insert into inventory_copy_tbl ( product_id,product_name,product_manufacturer,ship_date) select product_id,product_name,product_manufacturer,ship_date, from inventory_tbl;
另一种创建副本的方法使用create table as语句。 该语句复制所有列,列定义,并用源表数据填充副本。
检查其语法如下 -
create table clone_tbl as select columns from original_tbl where conditions];
查看其使用示例如下 -
create table products_copy_tbl as select * from products_tbl;