Mariadb 简明教程

MariaDB - Table Cloning

有些情况下需要生成现有表的精确副本。CREATE…​SELECT 语句无法生成此输出,因为它忽略了诸如索引和默认值之类的内容。

Some situations require producing an exact copy of an existing table. The CREATE…​SELECT statement cannot produce this output because it neglects things like indexes and default values.

复制表的步骤如下 −

The procedure for a duplicating a table is as follows −

  1. Utilize SHOW CREATE TABLE to produce a CREATE TABLE statement that details the entire structure of the source table.

  2. Edit the statement to give the table a new name, and execute it.

  3. Use an INSERT INTO…​SELECT statement if you also need the table data copied.

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 语句。该语句复制所有列、列定义,并利用源表的数据填充副本。

Another method for creating a duplicate uses a CREATE TABLE AS statement. The statement copies all columns, column definitions, and populates the copy with the source table’s data.

查看其在下面提供的语法 −

Review its syntax given below −

CREATE TABLE clone_tbl AS
   SELECT columns
   FROM original_tbl
   WHERE conditions];

查看其在下面提供的示例用法 −

Review an example of its use below −

CREATE TABLE products_copy_tbl AS
   SELECT *
   FROM products_tbl;