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.
- Go to MYSQL console and press the Enter key.
- Type SHOW DATABASES; command and press enter key.
- Now the list of Database will be showing you as shown in the figure below.
- Type USE Your_Database_Name; command to select the database you want to use and press the Enter key.
- Now type SHOW TABLES; command and again press return key.
- After this, all table of this database is displaying as shown in the figure above like new and worker.
- If you want to see the structure of any table you can use the SELECT command as shown in the figure.
- 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;
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.
So, you can see how to copy the structure of the old database table into a new table.