13

How to copy a table structure from one database to another

2
  • Needs question body, context, ... Commented 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. Commented Aug 25, 2012 at 11:28

7 Answers 7

9

Go to the database in which you want to create the new table, and use CREATE TABLE... LIKE..., fully qualifying the source table name.

Sign up to request clarification or add additional context in comments.

2 Comments

Please note that this only works if the user has permission to both databases.
9

Following line of code should do the job:

create table new_table_name like existing_table_name; 

Comments

7

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

6

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

4

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.

Comments

3

You can use SQLyog for this. SQLyog is an all-round Management Tool (/'GUI'/'Frontend') for MySQL.

Comments

0

It's a simple operation, I think is not necessary to dump the database.

My way to solve this:

USE TARGET_DB; CREATE NEW_TABLE LIKE SOURCE_DB.SOURCE_TABLE; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.