Question

My goal is to: *Copy a table* in the same database with the same contents, but with a different table name. If my table name is device, then I want to create another table with table name device_test in the same database. To do this, I tried the following steps:

In the Server menu:

1.  Data Export / Restore > Data Export
2.  Select the tables you want to export.
3.  Select "Export to Self-Contained File"
4.  Browse to the location for the SQL file.
5.  Start Export

Now, it's in the form of a .sql file on my desktop. Now if I import it into the same table using the following steps:

1.  Data Export / Restore > Data Import/Restore
2.  Import from Self-Contained File
3.  Browse to the location for the SQL file.
4.  Start Import

If I do this, is it going to overwrite the old table (device)? If so, where I should specify the new table name (device_test)?

I am using MySQL Workbench, and I don't want to overwrite the original table.

Was it helpful?

Solution 2

You have the steps right, but you're missing one. You have:

  • Export File (which you describe correctly in your question)
  • Import File (which you also describe correctly)

You should have:

  • Export File
  • Modify File
  • Import File

After you have exported the table, you will have a .sql file. You can go into this file and modify it. (You can open this file in any basic text editor. If you're on Windows, you can open it in Notepad by right-clicking the file icon and selecting Edit.) After some initial comments, you should have something like

DROP TABLE IF EXISTS `device`;    
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `device` ( 
 -- parameters, etc.
)

Note how it's dropping your table, then re-creating it? It's simple - in both of those places where the table name is, change it to your new table name.

DROP TABLE IF EXISTS `device_test`;  -- note the new name
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `device_test` (  -- note the new name
 -- parameters, etc.
)

There are two other places in the file that this needs to be done. One is in a LOCK TABLES statement, and the other is in the INSERT INTO statement. (If you wish, you could simply use a Find\Replace tool to replace all instances of the old table name with the new name.)

You can then import it, and you're all set!

EDIT: As Zak mentioned in the comments to the question, one way of doing this could be to use CREATE TABLE new_table LIKE old_table; SELECT INTO new_table SELECT * FROM old_table;. If the two tables in question are on the same machine, etc, both methods will work. However, the dump .sql file does have portability that cannot be mimicked using SQL statements.

OTHER TIPS

Simple solution:

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

or even shorter:

CREATE TABLE new_table SELECT * FROM old_table;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top