Home
Arjen's Journal - 29th April 2008
Open Query: MySQL, Open Source & other ponderings

Arjen Lentz
Date: 2008-04-29 19:20
Subject: Copying a table in MySQL
Security: Public

This question often comes up, and the general answer given appears to be "CREATE TABLE ... SELECT ..."
But actually, that does not do what you might expect, as this statement creates a table structure based on the resultset of the select, so the column types may differ from your original table, and the table will not have indexes either.
The syntax does allow you to add and override pretty much everything, but since we were talking about copying, let's look at another way:

CREATE TABLE bar LIKE foo;
INSERT INTO bar SELECT * FROM foo;

This produces an exact copy of the original table, both structure and data, indexes and everything.
No, you can't combine these two into a single statement. Sorry ;-)

8 Comments | Post A Comment | Add to Memories | Tell a Friend | Link



browse
my journal
links
July 2008
High Performance MySQL (2nd ed.)
summary