[solution] Copy entire table ( or partial table ) to a new table

By.

min read

My profile

Share this:

Example:
To make a copy of the table recipes which is in a different database called production into a new table called recipes_new in the currently selected database, use these two commands:

[code:1:7ab2abe5fb]CREATE TABLE recipes_new LIKE production.recipes;
INSERT recipes_new SELECT * FROM production.recipes;[/code:1:7ab2abe5fb]

This stuff actually works !

Now for a [b:7ab2abe5fb]partial table[/b:7ab2abe5fb]: ( oldtable contains 10 colums/fields, newtable only 4 including primary key, which we solve by NULL-ing the primary incremental key )
[code:1:7ab2abe5fb]INSERT `newtable` select NULL, `ip`,`ip_forwarded`,`time` FROM `oldtable`[/code:1:7ab2abe5fb]

[b:7ab2abe5fb]Source:[/b:7ab2abe5fb]
[url]http://www.tech-recipes.com/rx/1487/copy-an-existing-mysql-table-to-a-new-table/[/url]

Share this:

Leave a Reply

Your email address will not be published. Required fields are marked *