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?
Try:
CREATE TABLE foo SELECT * FROM bar LIMIT 0
Or:
CREATE TABLE foo SELECT * FROM bar WHERE 1=0
create table NewTable like OldTable
is an option for you. Link copied from deleted answer.
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.
show create table bar
into a dynamically executable statement?
show create table bar
is already executable, if the script has the permission to create tables, you could parse it and then execute alter table statements too.
Feb 5, 2015 at 20:38
create table...
will not copy indexes. this answer is the better option
Sep 27, 2019 at 13:56
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;
foo
andbar
are arbitrary meaningless variables, much like Alice and Bob.