Copying table in MySQL

5

I need to make a copy of a table in MySQL for a simulation, is there any function for this? How could I do that?

    
asked by anonymous 24.06.2015 / 14:00

1 answer

6

If you want to create a new table based on an existing one, there is a simple way to do it:

CREATE TABLE nova_tabela SELECT * FROM tabela_a_copiar

You have some remarks about using this. I explained this in this question:

How to pass data from a table to another SQL

SQLFiddle

Update

In order to copy auto_increment , primary keys , and the like, we have to use the command CREATE table LIKE

Here's how it would look:

CREATE TABLE nova_tabela LIKE tabela_a_copiar;
INSERT INTO nova_tabela SELECT * FROM tabela_a_copiar;
    
24.06.2015 / 14:04