如何复制、克隆或复制数据、结构、,并将MySQL表的索引转换为新表?

这是我到目前为止发现的。

这将复制数据和结构,但不包括指数:

create table {new_table} select * from {old_table};

这将复制结构和索引,但不包括数据:

create table {new_table} like {old_table};

当前回答

// To copy specific column data use this one:
CREATE TABLE ut_axis_existrec LIKE ut_karvy_annexure; // To create new table

INSERT INTO ut_axis_existrec
(funding_ac,micr_no, warrant_no,
amount,invname,mfundcode,funding_dt,status,remarks1,amc_remark,created_at) 
SELECT  
t1.funding_ac,
t1.micr_no,
t1.warrant_no,
t1.amount,
t1.invname,
t1.mfund_code,
t1.funding_dt,
t1.status,
t1.remarks1,
t1.created_at
from ut_axis_karvy
inner join 
ut_axis_karvy_master as t2
on t1.micr_no = t2.micr_no;

其他回答

对于MySQL

CREATE TABLE newtable LIKE oldtable ; 
INSERT newtable SELECT * FROM oldtable ;

对于MSSQL使用MyDatabase:

Select * into newCustomersTable  from oldCustomersTable;

此SQL用于复制表,此处旧CustomersTable的内容将复制到新CustomersTable。确保数据库中不存在newCustomersTable。

扩展此答案时,可以使用存储过程:

CALL duplicate_table('tableName');

这将导致名为tableName_20181022235959的重复表如果在

SELECT NOW();

结果:

2018-10-22 23:59:59

实施

DELIMITER $$
CREATE PROCEDURE duplicate_table(IN tableName VARCHAR(255))
  BEGIN
    DECLARE schemaName VARCHAR(255) DEFAULT SCHEMA();
    DECLARE today VARCHAR(14) DEFAULT REPLACE(REPLACE(REPLACE(NOW(), '-', ''), ' ', ''), ':', ''); -- update @ year 10000
    DECLARE backupTableName VARCHAR(255) DEFAULT CONCAT(tableName, '_', today);

    IF fn_table_exists(schemaName, tableName)
      THEN
        CALL statement(CONCAT('CREATE TABLE IF NOT EXISTS ', backupTableName,' LIKE ', tableName));
        CALL statement(CONCAT('INSERT INTO ', backupTableName,' SELECT * FROM ', tableName));
        CALL statement(CONCAT('CHECKSUM TABLE ', backupTableName,', ', tableName));
      ELSE
        SELECT CONCAT('ERROR: Table "', tableName, '" does not exist in the schema "', schemaName, '".') AS ErrorMessage;
      END IF;
  END $$
DELIMITER ;

DELIMITER $$
CREATE FUNCTION fn_table_exists(schemaName VARCHAR(255), tableName VARCHAR(255))
  RETURNS TINYINT(1)
  BEGIN
    DECLARE totalTablesCount INT DEFAULT (
      SELECT COUNT(*)
      FROM information_schema.TABLES
      WHERE (TABLE_SCHEMA COLLATE utf8_general_ci = schemaName COLLATE utf8_general_ci)
        AND (TABLE_NAME COLLATE utf8_general_ci = tableName COLLATE utf8_general_ci)
    );
    RETURN IF(
      totalTablesCount > 0,
      TRUE,
      FALSE
    );
  END $$
DELIMITER ;

DELIMITER $$
CREATE PROCEDURE statement(IN dynamic_statement TEXT)
  BEGIN
      SET @dynamic_statement := dynamic_statement;
      PREPARE prepared_statement FROM @dynamic_statement;
      EXECUTE prepared_statement;
      DEALLOCATE PREPARE prepared_statement;
  END $$
DELIMITER ;

简单克隆:它从另一个表创建一个表,而不考虑任何列属性和索引。

CREATE TABLE new_table SELECT * FROM original_table;

浅层克隆:这将仅基于原始表的结构创建一个空表

CREATE TABLE new_table LIKE original_table;

以下命令将在原始表的基础上创建一个空表。

CREATE TABLE adminUsers LIKE users;

深度克隆:这意味着新表将具有现有表的每列和索引的所有属性。如果要维护现有表的索引和属性,这非常有用。

CREATE TABLE new_table LIKE original_table;

INSERT INTO new_table SELECT * FROM original_table;

https://towardsdatascience.com/how-to-clone-tables-in-sql-dd29586ec89c

// To copy specific column data use this one:
CREATE TABLE ut_axis_existrec LIKE ut_karvy_annexure; // To create new table

INSERT INTO ut_axis_existrec
(funding_ac,micr_no, warrant_no,
amount,invname,mfundcode,funding_dt,status,remarks1,amc_remark,created_at) 
SELECT  
t1.funding_ac,
t1.micr_no,
t1.warrant_no,
t1.amount,
t1.invname,
t1.mfund_code,
t1.funding_dt,
t1.status,
t1.remarks1,
t1.created_at
from ut_axis_karvy
inner join 
ut_axis_karvy_master as t2
on t1.micr_no = t2.micr_no;

我发现了同样的情况,我采取的方法如下:

执行SHOW CREATE TABLE<TABLE name to clone>:这将为您提供要克隆的表的CREATE TABLE语法通过更改表名来克隆表,从而运行CREATE TABLE查询。

这将创建要与索引一起克隆的表的精确副本。然后,您只需要重命名索引(如果需要)。