MySQL 复制表而不复制数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1834472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:39:57  来源:igfitidea点击:

Copy table without copying data

mysql

提问by Matthew

CREATE TABLE foo SELECT * FROM bar

copies the table fooand duplicates it as a new table called bar.

复制该表foo并将其复制为名为 的新表bar

How can I copy the schema of footo a new table called barwithoutcopying over the data as well?

如何将 的模式复制foo到一个名为的新表中bar而不复制数据?

采纳答案by Andomar

Try:

尝试:

CREATE TABLE foo SELECT * FROM bar LIMIT 0

Or:

或者:

CREATE TABLE foo SELECT * FROM bar WHERE 1=0

回答by RCNeil

Try

尝试

CREATE TABLE foo LIKE bar;

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

所以键和索引也被复制了。

Documentation

文档

回答by Timo Huovinen

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.

您还可以在程序中运行查询。

回答by Ali Azaz Alam

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;