开发者

Copy table without copying data

开发者 https://www.devze.com 2022-12-13 05:39 出处:网络
CREATE TABLE foo SELECT * FR开发者_如何学COM bar copies the table foo and duplicates it as a new table called bar.
CREATE TABLE foo SELECT * FR开发者_如何学COM 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?


Try

CREATE TABLE foo LIKE bar;

so the keys and indexes are copied over as, well.

Documentation


Try:

CREATE TABLE foo SELECT * FROM bar LIMIT 0

Or:

CREATE TABLE foo SELECT * FROM bar WHERE 1=0


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.


Only want to clone the structure of table:

CREATE TABLE foo SELECT * FROM bar WHERE 1 = 2;

Also wants to copy the data:

CREATE TABLE foo as SELECT * FROM bar;
0

精彩评论

暂无评论...
验证码 换一张
取 消