Copy table from one database to another mysql workbench

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.

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.

CREATETABLEdestination_tableSELECTcol_1,col_2,col_3..fromsource_tableWHEREcondition

In the syntax,

  1. destination_table: Specify the destination table name. We can create a table using the column structure of the source table. If the destination table is in a different database, you can use db_name.schema_name.table_name format
  2. column_name: If you want to populate the specific columns from the source table, you can specify the column names
  3. Source_table: Specify the source table name. If the source table is in a different database, then you can use db_name.schema_name.table_name format
  4. Condition: Specify the condition to populate to filter the records. If you want to copy the specific dataset from the source table, you can use the WHERE clause

When you copy the data from the source table to destination tables, MySQL performs the following tasks:

  1. Create a new table with the name specified in the destination_table parameter. The destination table structure contains the columns that the SELECT statement has returned
  2. Copy the data from the source table to the destination table

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 data

Suppose we want to copy the data from the movies table to the movies_backup table. To view the data, run below the SELECT statement.

Copy table from one database to another mysql workbench

To copy the data from the film (source table) to the film_backup (destination) table.

createtablemovies_backupselect*frommovies;

Copy table from one database to another mysql workbench

Once data is copied, run the SELECT statement to view the data.

Select*frommovies_backup;

Copy table from one database to another mysql workbench

As you can see, the database has been copied to the movies_backup table.

Example 2: Copy partial data using WHERE clause

Suppose 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.

createtabletbl_movies_Rating_NC17select*frommoviesWHERErating='NC-17';

Copy table from one database to another mysql workbench

Once data has been copied, run the SELECT statement on tbl_movies_rating_NC17.

select*fromtbl_movies_Rating_NC17

Copy table from one database to another mysql workbench

As you can see, data has been copied.

Example 3: Copy tables between the databases

In 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:

CreatedatabaseDEV_SakilaDB;

To copy the data, we will run the following query:

createtable`DEV_SakilaDB`.`tblactor`select*from`sakila`.`actor`;

Copy table from one database to another mysql workbench

Once data has been copied, run the following SELECT statement.

select*from`DEV_SakilaDB`.`tblactor`;

Copy table from one database to another mysql workbench

As you can see, the data has been copied to the Dev_SakilaDB.tblActor table.

Example 4: MySQL copy table process to clone the table

When 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.

  1. To create a table, use CREATE TABLE destination_table LIKE source_table
  2. To copy the data, use INSERT INTO destination_table SELECT * FROM source_table

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.

CREATETABLE`actor`(

    `actor_id`smallintunsignedNOTNULLAUTO_INCREMENT,

    `first_name` varchar(45)NOTNULL,

    `last_name`varchar(45)NOTNULL,

    `last_update` timestampNOTNULLDEFAULTCURRENT_TIMESTAMPONUPDATECURRENT_TIMESTAMP,

    PRIMARYKEY(`actor_id`),

    KEY `idx_actor_last_name`(`last_name`)

  )ENGINE=InnoDBAUTO_INCREMENT=201DEFAULTCHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

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.

createtable`tblActor_backup`select*from`actor`;

Once data has been copied, run SHOW CREATE TABLE statement to view the structure of the tblActor_backup table.

SHOWCREATETABLEtblActor_backup;

Structure of tblActor_backup table is the following:

CREATETABLE`tblactor_backup`(

    `actor_id`smallintunsignedNOTNULLDEFAULT'0',

    `first_name` varchar(45)NOTNULL,

    `last_name`varchar(45)NOTNULL,

    `last_update` timestampNOTNULLDEFAULTCURRENT_TIMESTAMPONUPDATECURRENT_TIMESTAMP

)ENGINE=InnoDBDEFAULTCHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

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.

createtable`tblActor_backup`LIKE`actor`;

Once data has been copied, run the SHOW CREATE TABLE to view the structure of tblActor_backup table.

SHOWCREATETABLE`tblActor_backup`;

CREATETABLE`tblactor_backup`(

    `actor_id`smallintunsignedNOTNULLAUTO_INCREMENT,

    `first_name` varchar(45)NOTNULL,

    `last_name`varchar(45)NOTNULL,

    `last_update` timestampNOTNULLDEFAULTCURRENT_TIMESTAMPONUPDATECURRENT_TIMESTAMP,

    PRIMARYKEY(`actor_id`),

    KEY `idx_actor_last_name`(`last_name`)

)ENGINE=InnoDBDEFAULTCHARSET=utf8mb4COLLATE=utf8mb4_0900_ai_ci

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.

INSERTINTO`tblActor_backup`select*from`actor`;

Copy table from one database to another mysql workbench

Run SELECT statement to view the data.

Select*from`tblActor_backup`

Query Output

Copy table from one database to another mysql workbench

As you can see, the data has been copied to the tblActor_backup table.

Summary

In 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

Learn MySQL: Querying data from MySQL server using the SELECT statement
Learn MySQL: What is pagination
Learn MySQL: Sorting and Filtering data in a table
Learn MySQL: Add data in tables using the INSERT statement
Learn MySQL: Create and drop temp tables
Learn MySQL: Delete and Update Statements
Learn MySQL: The Basics of MySQL Stored Procedures
Learn MySQL: The Basics of MySQL Views
Learn MySQL: An overview of MySQL Binary Logs
Learn MySQL: An overview of the mysqlbinlog utility
Learn MySQL: Run multiple instances of MySQL Server on Windows 10
Learn MySQL: MySQL String Functions
Learn MySQL: Control Flow functions
Learn MySQL: Install MySQL server 8.0.19 using a noinstall Zip archive
Learn MySQL: MySQL Copy table

  • Author
  • Recent Posts

Copy table from one database to another mysql workbench

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

Copy table from one database to another mysql workbench

How do I copy a table from one database to another in SQL Workbench?

10 Answers.
Connect to a MySQL Server..
Expand a Database..
Right Click on a table..
Select Copy To Clipboard..
Select Create Statement..

How do I copy a table from one MySQL database to another?

The fastest way to copy a table in MySQL:.
Right-click the table you want to copy in Database Explorer and select Duplicate Object..
In the dialog that opens, select the destination db..
Select to copy the table data or structure only..
Specify the name of the new table, and click OK..

How do I copy a table data from one database to another database?

Right-click on the database name, then select "Tasks" > "Export data..." from the object explorer. The SQL Server Import/Export wizard opens; click on "Next". Provide authentication and select the source from which you want to copy the data; click "Next". Specify where to copy the data to; click on "Next".

How do I copy a database from one MySQL workbench to another server?

5 Answers.
Open MySQL Workbench..
Create the old server's connection (if you haven't it).
Create the new server's connection (if you haven't it).
Go to Server Administration and click Manage Import / Export..
Select old server..
Select all schemas in Export to Disk tab..