Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicating a MySQL table, indices, and data

Tags:

mysql

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}; 
like image 423
xkcd150 Avatar asked Jul 19 '10 09:07

xkcd150


People also ask

Can we duplicate a table in MySQL?

You can duplicate or "clone" a table's contents by executing a CREATE TABLE ... AS SELECT statement: CREATE TABLE new_table AS SELECT * FROM original_table; Please be careful when using this to clone big tables.

What is the MySQL query to make duplicate table in MySQL?

Use SHOW CREATE TABLE to get a CREATE TABLE statement that specifies the source table's structure, indexes and all. Modify the statement to change the table name to that of the clone table and execute the statement. This way, you will have the exact clone table.


1 Answers

To copy with indexes and triggers do these 2 queries:

CREATE TABLE new_table LIKE old_table;  INSERT INTO new_table SELECT * FROM old_table; 

To copy just structure and data use this one:

CREATE TABLE new_table AS SELECT * FROM old_table; 

I've asked this before:

Copy a MySQL table including indexes

like image 77
Haim Evgi Avatar answered Oct 14 '22 01:10

Haim Evgi