Cloning Table in MySQL
Last Updated :
26 Jul, 2020
There may be a number of occasions where you need to create an exact copy of an already defined (or created) table. MySQL enables you to perform this operation. Because we may need such duplicate tables for testing over the data without having any impact over the original table and the data stored in it.
Original_table –
ID |
F_name |
L_name |
Project_id |
Email |
Job_Title |
City |
Age |
Salary |
1. |
Madhav |
Mohan Sharma |
A-1 |
W_@.com |
SDE |
Agra |
21 |
70,000/- |
2. |
Mukund |
Mohan Sharma |
B-2 |
V_@.com |
SDE |
Delhi |
21 |
70,000/- |
3. |
Jay |
Sharma |
C-3 |
X_@.com |
Sr.SDE |
Banglore |
29 |
1,50,000/- |
4. |
Parag |
Sharma |
D-4 |
y_@.com |
SDE |
Mumbai |
27 |
80,000/- |
5. |
Anshika |
Goyal |
E-5 |
Z_@.com |
Hr Mgr |
Noida |
26 |
90,000/- |
Steps to replicate (Clone) an existing table schema (structure) and it’s content –
Step 1 : To clone a table, use the query below. Using this query an empty schema (structure) of the table gets created with the same attributes of original table :
CREATE TABLE Contact List(Clone_1) LIKE Original_table;
Output : Contact List (Clone_1)
ID |
F_name |
L_name |
projrct_id |
Email |
Job_Title |
City |
Age |
Salary |
Step 2 : If you want to create a table which is bedecked with all the content of your original table, use this MySQL query :
CREATE TABLE Contact List(Clone_1) AS SELECT *
FROM Original_table;
OR
INSERT INTO Contact List(Clone_1) SELECT *
FROM original_table;
Output : Contact List (Clone_1)
ID |
F_name |
L_name |
Project_id |
Email |
Job_Title |
City |
Age |
Salary |
1. |
Madhav |
Mohan Sharma |
A-1 |
W_@.com |
SDE |
Agra |
21 |
70,000/- |
2. |
Mukund |
Mohan Sharma |
B-2 |
V_@.com |
SDE |
Delhi |
21 |
70,000/- |
3. |
Jay |
Sharma |
C-3 |
X_@.com |
Sr.SDE |
Banglore |
29 |
1,50,000/- |
4. |
Parag |
Sharma |
D-4 |
y_@.com |
SDE |
Mumbai |
27 |
80,000/- |
5. |
Anshika |
Goyal |
E-5 |
Z_@.com |
Hr Mgr |
Noida |
26 |
90,000/- |
Share your thoughts in the comments
Please Login to comment...