This article explains the MySQL copy table process. In this article, you will learn how we can copy data from one table to another table. These tables could be in the same database or different databases. The MySQL copy table process can copy a specific dataset or all data from the source table to the destination table. We can use the MySQL copy table process to replicate the issues that occurred on production servers, which helps developers rectify the issues quickly. Show
Table of Contents
To copy the data between the tables, we use the combination of CREATE TABLE and SELECT statement. If you want to copy the specific dataset from the table, we can filter the records by adding a WHERE clause. The syntax to copy the table and data is following.
In the syntax,
When you copy the data from the source table to destination tables, MySQL performs the following tasks:
Now, let us see some examples. For demonstration, we are going to use the Sakila database. We are using MySQL workbench. Example 1: Copy the entire table with dataSuppose we want to copy the data from the movies table to the movies_backup table. To view the data, run below the SELECT statement. To copy the data from the film (source table) to the film_backup (destination) table.
Once data is copied, run the SELECT statement to view the data.
As you can see, the database has been copied to the movies_backup table. Example 2: Copy partial data using WHERE clauseSuppose you want to create a table that has a list of movies whose rating is NC-17. In this example, the source table is movies, and the destination table is tbl_movies_Rating_NC17. To filter the data, we are using the WHERE clause on the rating column.
Once data has been copied, run the SELECT statement on tbl_movies_rating_NC17.
As you can see, data has been copied. Example 3: Copy tables between the databasesIn this example, we will see how we can copy the data from the source table to the destination table in another database. To demonstrate, I have created a database named DEV_SakilaDB, and we will copy the data from the actor table of the sakila database to the tblActor table of the DEV_SakilaDB database. The following query creates a new database named DEV_SakilaDB:
To copy the data, we will run the following query:
Once data has been copied, run the following SELECT statement.
As you can see, the data has been copied to the Example 4: MySQL copy table process to clone the tableWhen we use MySQL copy table process using CREATE TABLE.. SELECT statement does not create indexes, constraints, primary keys, foreign keys associated with the source table. If you want to clone the source table, we can use the following method.
Let us understand with a simple example. We are using the actor table. To view the table structure, we can use SHOW CREATE TABLE [tbl_name] statement. Run the following query. Following is the table structure of the actor table.
Now, let us run the CREATE TABLE .. SELECT statement to copy the data. The source table is actor and destination table are tblActor_backup. Query is following.
Once data has been copied, run SHOW CREATE TABLE statement to view the structure of the tblActor_backup table.
Structure of tblActor_backup table is the following:
As you can see in the above script, the primary key of tblActor table is missing. Now, let us run the CREATE TABLE.. LIKE .. statement to create a table.
Once data has been copied, run the SHOW CREATE TABLE to view the structure of tblActor_backup table.
As you can see, the CREATE TABLE script has a primary key. Now run INSERT INTO.. SELECT statement to insert the data from source table to destination table.
Run SELECT statement to view the data.
Query Output As you can see, the data has been copied to the tblActor_backup table. SummaryIn this article, we learned about the MySQL copy table process. To copy the, We can use CREATE TABLE.. SELECT statement. We also learned CREATE TABLE USING statement to create a clone of the source table and use the INSERT INTO SELECT statement to copy the data. We learned these methods using various examples. Table of contents
Nisarg Upadhyay is a SQL Server Database Administrator and Microsoft certified professional who has more than 8 years of experience with SQL Server administration and 2 years with Oracle 10g database administration. He has expertise in database design, performance tuning, backup and recovery, HA and DR setup, database migrations and upgrades. He has completed the B.Tech from Ganpat University. He can be reached on How do you clone a table?Example. Step 1 − Get the complete structure about the table.. Step 2 − Rename this table and create another table.. Step 3 − After executing step 2, you will clone a table in your database. If you want to copy data from an old table, then you can do it by using the INSERT INTO... SELECT statement.. How do you clone a table in database?In Object Explorer right-click the table you want to copy and select Design. Select the columns in the existing table and, from the Edit menu, select Copy. Switch back to the new table and select the first row. From the Edit menu, select Paste. How do I copy a table from one schema to another in MySQL?Right-click on the database name then select "Tasks" > "Export data..." from the object explorer. 4. Provide authentication and select the source from which you want to copy the data; click "Next". How do I copy a table to another table in MySQL workbench?10 Answers. Connect to a MySQL Server.. Expand a Database.. Right Click on a table.. Select Copy To Clipboard.. Select Create Statement.. |