How to copy a table structure from one database to another
- Needs question body, context, ...Jeroen Baert– Jeroen Baert2012-04-24 10:55:32 +00:00Commented Apr 24, 2012 at 10:55
- I added the mysql detail, which makes it a little more relevant and focused. This question comes up high on the results for the google search. best not to fight it.ftrotter– ftrotter2012-08-25 11:28:43 +00:00Commented Aug 25, 2012 at 11:28
7 Answers
Go to the database in which you want to create the new table, and use CREATE TABLE... LIKE..., fully qualifying the source table name.
2 Comments
As per comments, it's not entirely clear what you want to achieve. Are trying to create a copy of the database (from scratch) or just copy a single table structure across to another database?
In any case, I'd recommend mysqldump.
If you want to copy all of the tables, you use:
> mysqldump -d $databaseName > $newFile Alternatively, if you only want to copy one table, you can use:
> mysqldump -d $databaseName.$tableName > $newFile Then you can you either import this file into the other database...
Comments
There are multiple tools that will allow you to view the schema of an existing database and export it as a file that you can then run against the new database. A few that cone to mind:
There are many others.
Comments
I am looking for the same solution. I tried below example it worked, go to the database where you want to create a table and try like below example.
CREATE TABLE new_table_name LIKE database.table_name This example will create a table with AI, Index, Key. its something like duplicate table structure.