How to Copy Data From One Database to Another in MySQL

Mehvish Ashiq Feb 02, 2024
  1. Copy Data From One Database to Another
  2. Copy Data From the MySQL Server to the MS SQL Server
  3. Conclusion
How to Copy Data From One Database to Another in MySQL

We will learn about different ways to copy data from one database to another.

This strategy of copying data is very useful in data replication. Due to data replication, data can be served to different servers and made available to all users without any inconsistency.

The benefits of data replication include increased availability and reliability of data and less communication overhead. We can copy the full or partial database from one database server to another.

In this tutorial, we’ll see how to copy data within MySQL (with the same user), MS SQL Server (with the same user), and from MySQL to MS SQL Server (having different users).

Copy Data From One Database to Another

Using the MySQL Server

We have two databases named students and person in our MySQL Server. The person database has a table named customers, and the students database has a table named student that we want to copy into the person database.

It is important to note that we copy data within one server (MySQL 8.0.27) with one user (root). We have the following databases (see red boxes) and tables (see green boxes).

copy data from one database to another - current data in mysql server

We want to copy the student table from the students database to the person database using the following SQL query.

#MySQL Version 8.0.27
CREATE TABLE person.students SELECT * FROM students.student; 

Observe the databases and tables after copying the data (see the screenshot below). You can see two tables in the person database, customers and students.

You might have noticed that we wanted to copy the student table from the source database, so why is it named students in the destination database? It is because we named it students in the SQL query.

copy data from one database to another - data after copying in mysql server

Using the MS SQL Server

Here, we have two databases named teachers and person in our MS SQL Server. The person database has a table called customers, and the teachers database has a table named teacher that we want to copy into the person database.

We copy data within one server (MS SQL Server) with one user. You can see the current databases (see red boxes) and tables (see green boxes) in the screenshot below.

copy data from one database to another - current data in mssql server

We’ll copy a table named teacher from the Teachers Database into the Person Database using the following command.

#MSSQL Server
SELECT * INTO Person..teacher FROM Teachers..teacher;

You can see the copied table in the screenshot given below. We now have two tables in the Person database.

copy data from one database to another - data after copying in mssql server

Copy Data From the MySQL Server to the MS SQL Server

Have you ever imagined if you have to copy data from your database to another user’s database? It means there are different users involved (and sometimes different machines).

Here we’ll see how to copy data from the MySQL Server to the MS SQL Server (note that there are two users on the same computer, one for MySQL and the other for MS SQL Server).

Right-click on your database in MS SQL Server and select Task -> Import Data.

copy data from one database to another - select import data in mssql server

Click on the Next button in the following screenshot.

copy data from one database to another - click next in mssql server

In the following screenshot, make sure that the data source is .Net Framework Data Provider for MySQL, write the MySQL database name, port number, and server as students, 3306, and localhost, respectively. Then, click Next.

It is important to note that you may have different database names and Server IP Addresses. We are using two servers on the same machine; that’s why we used localhost.

copy data from one database to another - select source port server in mssql server

Enter the username and password of MySQL Server (see the screenshot below) and click Next.

copy data from one database to another - user login info

Ensure that destination is selected as SQL Server Native Client 11.0, confirm your server’s name and database name, and select the authentication. We are using Windows Authentication for this tutorial. Then click Next.

copy data from one database to another - select destination in mssql server

Click Next on the following screen.

copy data from one database to another - write query part a in mssql server

Write SQL Query as per your requirements; we are copying all records from the student table using the following command. Click Next.

copy data from one database to another - write query part b in mssql server

Write your Destination table name (the name that will appear in MS SQL Server) and click Next.

copy data from one database to another - destination table name in mssql server

Click Next on the screen given below.

copy data from one database to another - info in mssql server

In the following screenshot, select Run Immediately (see green box), or you can save the package and run later (see red box). Click Next.

copy data from one database to another - run now in mssql server

Click Finish on the following screen.

copy data from one database to another - complete wizard in mssql server

Here, the following screen shows that the data has been copied. Click on Close.

copy data from one database to another - successful transfer in mssql server

Check your MS SQL Server for the copied table, and you can see the green box in the following screenshot.

copy data from one database to another - after copying mysql to mssql server

Conclusion

Considering the above discussion, we’ve concluded that copying data from one database to another is important to learn for various reasons, including security concerns, data availability, and reliability, etc. We can also provide access to data to different users worldwide.

Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook