How to copy existing table in mysql

In this post we will learn about how to copy the existing table in mysql. The newly copied table does not have data means it will be blank but it has similar rows information from its parent table.
After copying the table you have to insert the data by using INSERT command.

Follow the given below steps

Step (1): Login into mysql server. Replace the username with MySQL user name which you have.It will prompt you for password.

mysql -u username -p 

Step (2): Now connect with database.Replace database_name with your database.(show databases ; command will help you to list databases in MySQL server)

mysql > use database_name ;

Step 3: Now list the tables which you want to copy into new table. ( show tables ; command will help you to list tables).
Now copy the table

Original table name = table_name
New table name = table_name_NEW

mysql > create table table_name_NEW like table_name ;

Below given is reference of my MySQL server. Take it as an example.

mysql> use phpdirector;
Database changed
mysql> show tables;
+-----------------------+
| Tables_in_phpdirector |
+-----------------------+
| pp_categories         |
| pp_config             |
| pp_files              |
| pp_rating             |
+-----------------------+
4 rows in set (0.00 sec)

mysql> desc pp_config;
+---------------+--------------+------+-----+---------------+-------+
| Field         | Type         | Null | Key | Default       | Extra |
+---------------+--------------+------+-----+---------------+-------+
| name          | varchar(225) | YES  |     | NULL          |       |
| news          | varchar(225) | NO   |     | Welcome!      |       |
| vids_per_page | int(4)       | NO   |     | 10            |       |
| lang          | varchar(225) | NO   |     | en-gb.inc.php |       |
| version       | double       | NO   |     | NULL          |       |
| template      | varchar(255) | NO   |     | Photine       |       |
+---------------+--------------+------+-----+---------------+-------+
6 rows in set (0.00 sec)

mysql> create table pp_config_new like pp_config;
Query OK, 0 rows affected (0.16 sec)

mysql> desc pp_config_new;
+---------------+--------------+------+-----+---------------+-------+
| Field         | Type         | Null | Key | Default       | Extra |
+---------------+--------------+------+-----+---------------+-------+
| name          | varchar(225) | YES  |     | NULL          |       |
| news          | varchar(225) | NO   |     | Welcome!      |       |
| vids_per_page | int(4)       | NO   |     | 10            |       |
| lang          | varchar(225) | NO   |     | en-gb.inc.php |       |
| version       | double       | NO   |     | NULL          |       |
| template      | varchar(255) | NO   |     | Photine       |       |
+---------------+--------------+------+-----+---------------+-------+
6 rows in set (0.00 sec)

mysql> 

Leave a Comment

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