Copying table in Mysql

Asked

Viewed 1,174 times

5

Need to make a copy of a table in Mysql for a simulation, is there any function for it? How could I do that?

  • Use the trial version of Navicat, then CTRL+C CTRL+V in the table... E F2 to rename it...:v

  • I need a Mysql statement to do this. This will happen within a system

1 answer

6


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

CREATE TABLE nova_tabela SELECT * FROM tabela_a_copiar

There are some remarks about the use of this. I explained this in this question:

How to pass data from one table to another SQL

Sqlfiddle

Updating

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

See how you’d look in this case:

CREATE TABLE nova_tabela LIKE tabela_a_copiar;
INSERT INTO nova_tabela SELECT * FROM tabela_a_copiar;
  • 1

    @Kaduamaral, thank you so much for collaborating with the answer! did not know the Sqlfiddle

  • 1

    You’re welcome Wallace, I was going to answer, but when I came back from the Sqlfiddle you had already answered, so I just added. ;)

  • 1

    Wallace, I noticed that this modeling does not make the auto_increment field as in the original table: http://sqlfiddle.com/#! 9/ca482/2 Know how to get around this?

  • 1

    Take a look at this SQLFIDDLE. I used the ALTER TABLE next :)

  • 1

    @Kaduamaral, after a long time, I discovered a way, without having to give the ALTER TABLE. See the update.

  • True, Wallace, fine. You can’t vote again, or you would. rsrs :P

  • 1

    What matters is information. It will help us a lot

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.