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?
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?
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
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;