How to Clone a Table in MySQL
Suppose we have the given table “Original_table”, We will create a duplicate table with the same structure and data.
Original_table
ID | F_name | L_name | Project_id | 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/- |
Follow the given steps to create a duplicate copy of the table with its structure and content.
Step 1: Create a Clone Table
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) Suppose we haveIKE Original_table;
Output : Contact List (Clone_1)
ID | F_name | L_name | projrct_id | Job_Title | City | Age | Salary |
---|
Step 2: Insert the duplicate data
After creating the table, insert data from original table into the clone table using the following query:
INSERT INTO Contact List(Clone_1) SELECT *
FROM original_table;
Output: Contact List (Clone_1)
ID | F_name | L_name | Project_id | 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/- |
As you can see, we have successfully created a clone of the table with duplicate structure and values.
Cloning Table in MySQL
Cloning a table in MySQL involves creating an exact copy of an existing table, including its structure and data. This process is useful for testing, backup, or migration purposes.
Cloning a table allows you to create a duplicate table with the same columns, indexes, and constraints as the original table and then populate it with the same data.
A table clone can be created using the CREATE TABLE statement with the LIKE keyword to create the table structure and then the INSERT INTO statement to copy the data.
In this article, we will look at the step-by-step guide to create a clone of the table in MySQL.