Title

Tuesday, 20 January 2015

Copy table without copying data


CREATE TABLE foo SELECT * FROM bar

copies the table foo and duplicates it as a new table called bar

How can I copy the schema of foo to a new table called bar without copying over the data as well?

Answer

Wouldn't it be

CREATE TABLE foo LIKE bar;

so the keys and everything else but the data carries over as well?

Answer2
SHOW CREATE TABLE bar;

you will get a create statement for that table, edit the table name, or anything else you like, and then execute it.

This will allow you to copy the indexes and also manually tweak the table creation.

You can also run the query within a program.

Answer3
CREATE TABLE new_table AS (SELECT * FROM old_table WHERE 0);

different table copy scenarios are explained in this article How to copy a database table

Answer4

This can also serve the purpose: CREATE TABLE foo LIKE bar

No comments:

Post a Comment