
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to copy a table from one MySQL database to another?
The following is the syntax to copy a table from one database to another.
INSERT INTO yourDestinationDatabaseName.yourTableName SELECT * from yourSourceDatabaseName.yourtableName;
Let us see an example.
The CREATE command is used to create a table in the database ‘business’. We are creating a new table here.
<business>
mysql> use business; Database changed mysql> create table OriginalTable -> ( -> id int -> ); Query OK, 0 rows affected (0.46 sec)
Creating a new table in the database ‘test’.
<test>
mysql> use test; Database changed mysql> create table OriginalTable -> ( -> id int -> ); Query OK, 0 rows affected (0.47 sec
Records are inserted with the help of INSERT command in the first table.
mysql> use business; Database changed mysql> insert into OriginalTable values(1),(2),(3),(4); Query OK, 4 rows affected (0.18 sec) Records: 4 Duplicates: 0 Warnings: 0
To display all the records.
mysql> select *from OriginalTable;
Here is the output.
+------+ | id | +------+ | 1 | | 2 | | 3 | | 4 | +------+ 4 rows in set (0.00 sec)
To copy the above table from database “business” into another database “test”.
mysql> INSERT INTO test.OriginalTable SELECT * from business.OriginalTable; Query OK, 4 rows affected (0.20 sec) Records: 4 Duplicates: 0 Warnings: 0
To check if the table has been copied.
mysql> use test; Database changed mysql> select *from OriginalTable;
The following output shows that the table “OriginalTable” have been copied successfully into the second database.
+------+ | id | +------+ | 1 | | 2 | | 3 | | 4 | +------+ 4 rows in set (0.00 sec)
- Related Articles
- How to copy rows from one table to another in MySQL?
- How to copy a collection from one database to another in MongoDB?
- Copy from one column to another (different tables same database) in MySQL?
- Simplest way to copy data from one table to another new table in MySQL?
- MySQL statement to copy data from one table and insert into another table
- MySQL query to copy records from one table to another with different columns
- Copy a few columns from a table to another in MySQL
- How to copy tables or databases from one MySQL server to another MySQL server?
- Copy column values from one table into another matching IDs in MySQL
- How to get username using ID from another table in MySQL database?
- Copy all rows of a table to another table in MySQL?
- How to copy data from one field to another on every row in MySQL?
- Move rows from one table to another in MySQL?
- Insert data from one table to another in MySQL?
- How to copy files from one folder to another using Python?

Advertisements