How to copy a table structure without copying data in MySQL?

In this post, we are going to show how you can copy a table structure without copying the data values present in it. So, let’s start it, nothing special has to be done for this just type a simple command on MYSQL console. You can do it without searching anywhere online, however, you have to think logically like a programmer. Well, if you are here then let us tell you in a simple way.

For this, we will create a new table to copy the structure of an old table. I will use simple MYSQL command CREATE and will also use LIKE operator copy structure. But before that, I want to tell you one more thing about how we will open the database and show the tables.

  1. Go to MYSQL console and press the Enter key.
  1. Type SHOW DATABASES; command and press enter key.
  1. Now the list of Database will be showing you as shown in the figure below.
SHOW DATABASES
show MySQL databases
  1. Type USE Your_Database_Name; command to select the database you want to use and press the Enter key.
  1. Now type SHOW TABLES; command and again press return key.
Show MySQL database tables
Show MySQL database tables
  1. After this, all table of this database is displaying as shown in the figure above like new and worker.
  1. If you want to see the structure of any table you can use the SELECT command as shown in the figure.
  1. Now the final step is to copy the structure of the table (worker) type the given command along with the new table name and old table that you want to copy without the data and press Enter key.
CREATE TABLE your_table_name LIKE old_table_name; 

Example:

Here my existing table name is “worker” and the new one is “new_table“, thus to copy the old table structure the above command in my case is like this:

CREATE TABLE new_table LIKE worker;

Now you can see Query is ok using SELECT command we are seeing new table is empty.

copy the structure of the old database table into a new table.

So, you can see how to copy the structure of the old database table into a new table.

 

2 thoughts on “How to copy a table structure without copying data in MySQL?”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.