How to create a table in MYSQL that uses the same structure as an existing table?

There maybe times when you need to create a new table that uses the same structure as an existing table. I had this situation today, where I was working on a Test table in a database and then I needed to create a new table that would be used in production. And obviously the columns and primary key had to be the same. There are many ways where this could be achieved. One of the ways is to do it manually, which is fine until you are looking at a table that has 20 columns with constraints in almost every other column!

The simpler and faster way to replicate the structure of an existing table would be to use a SQL Query. And by a  query I don’t mean a complicated, convoluted query! It is in fact very simple and straightforward.

This is the Query you need to use when you would want to create a new table that has the same structure of an existing table-

Create Table NewTable_Name Like OldTable_Name




Where NewTable_Name is the Name of the New Table

and

OldTable_Name is the name of the Old Table

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s