mysql indices_Duplicating a MySQL table, indices, and data

How do I copy or clone or duplicate the data, structure,

and indices of a MySQL table to a new one?

This is what I've found so far.

This will copy the data and the structure,

but not the indices:

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

This will copy the structure and indices,

but not the data:

create table {new_table} like {old_table};

Haim Evgi

To copy with indexes and triggers do these 2 queries:

CREATE TABLE newtable LIKE oldtable;

INSERT newtable SELECT * FROM oldtable;

To copy just structure and data use this one:

CREATE TABLE tbl_new AS SELECT * FROM tbl_old;

I've asked this before:

Apart from the solution above, you can use AS to make it in one line.

CREATE TABLE tbl_new AS SELECT * FROM tbl_old;

Krishneil

MySQL way:

CREATE TABLE recipes_new LIKE production.recipes;

INSERT recipes_new SELECT * FROM production.recipes;

spuvi86

Go to phpMyAdmin and select your original table then select "Operations" tab in the "Copy table to (database.table)" area. Select the database where you want to copy and add a name for your new table.

e9cdbbe4db800972c85b8a4f33365c76.png

Jai

I found the same situation and the approach which I took was as follows:

Execute SHOW CREATE TABLE

Run the CREATE TABLE query by changing the table name to clone the table.

This will create exact replica of the table which you want to clone along with indexes. The only thing which you then need is to rename the indexes (if required).

Võ Minh

After I tried the solution above, I come up with my own way.

My solution a little manual and needs DBMS.

First, export the data.

Second, open the export data.

Third, replace old table name with new table name.

Fourth, change all the trigger name in the data (I use MySQL and it show error when I don't change trigger name).

Fifth, import your edited SQL data to the database.

Alessandro

The better way to duplicate a table is using only DDL statement. In this way, independently from the number of records in the table, you can perform the duplication instantly.

My purpose is:

DROP TABLE IF EXISTS table_name_OLD;

CREATE TABLE table_name_NEW LIKE table_name;

RENAME TABLE table_name TO table_name_OLD;

RENAME TABLE table_name _NEW TO table_name;

This avoids the INSERT AS SELECT statement that, in case of table with a lot of records can take time to be executed.

I suggest also to create a PLSQL procedure as the following example:

DELIMITER //

CREATE PROCEDURE backup_table(tbl_name varchar(255))

BEGIN

-- DROP TABLE IF EXISTS GLS_DEVICES_OLD;

SET @query = concat('DROP TABLE IF EXISTS ',tbl_name,'_OLD');

PREPARE stmt FROM @query;

EXECUTE stmt;

DEALLOCATE PREPARE stmt;

-- CREATE TABLE GLS_DEVICES_NEW LIKE GLS_DEVICES;

SET @query = concat('CREATE TABLE ',tbl_name,'_NEW LIKE ',tbl_name);

PREPARE stmt FROM @query;

EXECUTE stmt;

DEALLOCATE PREPARE stmt;

-- RENAME TABLE GLS_DEVICES TO GLS_DEVICES_OLD;

SET @query = concat('RENAME TABLE ',tbl_name,' TO ',tbl_name,'_OLD');

PREPARE stmt FROM @query;

EXECUTE stmt;

DEALLOCATE PREPARE stmt;

-- RENAME TABLE GLS_DEVICES_NEW TO GLS_DEVICES;

SET @query = concat('RENAME TABLE ',tbl_name,'_NEW TO ',tbl_name);

PREPARE stmt FROM @query;

EXECUTE stmt;

DEALLOCATE PREPARE stmt;

END//

DELIMITER ;

Have a nice day!

Alex

Expanding on this answer one could use a stored procedure:

CALL duplicate_table('tableName');

Which will result in a duplicate table called tableName_20181022235959 If called when

SELECT NOW();

results:

2018-10-22 23:59:59

Implementation

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 ;

Crazy_DT0

Try this :

`CREATE TABLE new-table (id INT(11) auto_increment primary key) SELECT old-table.name, old-table.group, old-table.floor, old-table.age from old-table;`

I selected 4 columns from old-table and made a new table.

FOR MySQL

CREATE TABLE newtable LIKE oldtable ;

INSERT newtable SELECT * FROM oldtable ;

FOR MSSQL

Use MyDatabase:

Select * into newCustomersTable from oldCustomersTable;

This SQL is used for copying tables, here the contents of oldCustomersTable will be copied to newCustomersTable.

Make sure the newCustomersTable does not exist in the database.

来源:https://stackoverflow.com/questions/3280006/duplicating-a-mysql-table-indices-and-data

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值