Copying 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 replicating data 数据复制
is very useful in . 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 data availability and reliability and reduced communication overhead. We can replicate a complete or partial database from one database server to another.
In this tutorial, we will learn how to copy data between MySQL (using same user), MS SQL Server (using same user), and from MySQL to MS SQL Server (using different users).
Copy data from one database to another
Using MySQL Server
We have two databases named students
and in our MySQL server . The database has a table named and the database has a table named that we want to copy to the database.person
person
customers
students
student
person
It is important to note that we are replicating data in one server (MySQL 8.0.27) using one user (root). We have the following databases (see red boxes) and tables (see green boxes).
We want to copy student
a table from students
the database to person
the database using the following SQL query.
#MySQL Version 8.0.27
CREATE TABLE person.students SELECT * FROM students.student;
Observe the database and tables after copying the data (see the screenshot below). You can person
see two tables in the database, customers
and students
.
You may have noticed that we want to copy the table from the source database student
, so why is it named as in the target database students
? This is because we named it as in the SQL query students
.
Using MS SQL Server
Here, we have two databases named teachers
and in our MS SQL Server. The database has a table named and the database has a table named, which we want to replicate to the database.person
person
customers
teachers
teacher
person
We are replicating data in one server (MS SQL Server) with one user. You can see the current database (see red box) and table (see green box) in the screenshot below.
We will use the following command to copy teacher
a table named from the database to the database.Teachers
Person
#MSSQL Server
SELECT * INTO Person..teacher FROM Teachers..teacher;
You can see the copied tables in the screenshot given below. We now Person
have two tables in the database.
Copy data from MySQL server to MS SQL server
Have you ever thought about what would happen if you had to copy data from your database to another user's database? This means different users (and sometimes different machines) are involved.
Here we will see how to copy data from MySQL server to MS SQL server (note that there are two users on the same machine, one for MySQL and another for MS SQL Server).
Right-click your database in MS SQL Server and select Task -> Import Data
.
Click the Next button as shown in the following screenshot.
In the following screenshot, make sure the Data Source is .Net Framework Data Provider for MySQL
, write the MySQL database name, port number, and server as students
, , 3306
and respectively localhost
. Then, click Next.
It is important to note that you may have different database names and server IP addresses. We use two servers on the same machine; that is localhost
why we use .
用户名
Enter the sum of your MySQL server 密码
(see screenshot below) and click 下一步
.
Make sure Target is selected SQL Server Native Client 11.0
, confirm your server name and database name, and select Authentication. We are using Windows Authentication in this tutorial. Then click Next.
Click Next on the following screen.
Write the SQL query as per your requirement; we are using the following command student
to copy all the records from the table. Click Next.
Write down your target table name (the name that will appear in MS SQL Server) and click Next.
Click Next on the screen given below.
In the following screenshot, select 立即运行
(see green box), or you can save the package and run it later (see red box). Click Next.
Click Finish on the following screen.
Here, the following screen shows that the data has been copied. Click Close.
Check your MS SQL Server for the replicated tables, you can see the green boxes in the following screenshot.
in conclusion
Considering the above discussion, we conclude that it is important to replicate data from one database to another for various reasons including security issues, data availability, reliability, etc. We can also provide access rights to the data to different users across the globe.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
If ELSE in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
In this tutorial, we aim to explore how to use IF ELSE the statement in MySQL. One of the key roles of a data analyst is to gather insights from the data and produce meaningful results. It can be done with the help of several data filtering
DATETIME vs. TIMESTAMP in MySQL
Publish Date:2025/04/11 Views:117 Category:MySQL
-
DATETIME and TIMESTAMP are two different data types that can be used to store values that must contain both a date and a time portion. In this article, we will understand how it is stored in the database and the memory required for ea
Execute multiple joins in one query in MYSQL
Publish Date:2025/04/11 Views:94 Category:MySQL
-
Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.
Joining 3 tables in MySQL
Publish Date:2025/04/11 Views:187 Category:MySQL
-
In this tutorial, we will learn how to join three tables in MySQL. Businesses and organizations may have to visualize three tables simultaneously based on certain matching columns common to all three tables. This operation is allowed in MyS
Use of UPDATE JOIN in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
This tutorial will explain how to use the statement in MySQL database UPDATE JOIN . We generally use joins to iterate over the rows in a particular table which may or may not have similar rows in other tables. We can UPDATE use JOIN the cla
How to use the Row_Number() function in MySQL
Publish Date:2025/04/11 Views:142 Category:MySQL
-
In this tutorial, we will explain how to use the VALUES function in MySQL ROW_NUMBER() . This is a sorting method that assigns consecutive numbers within a partition starting from 1. It is important to note that no two rows within a partiti
Multiple primary keys in MySQL
Publish Date:2025/04/11 Views:66 Category:MySQL
-
In this tutorial, our goal is to explore the concept of multiple primary keys for a table in MySQL. Many times, businesses and organizations have to assign certain columns as primary keys. This primary key has multiple uses and reasons to b
Displaying foreign keys in MySQL
Publish Date:2025/04/11 Views:55 Category:MySQL
-
In this tutorial, we aim to explore how to display foreign keys for tables and columns in MySQL. The type of key that references a primary key, also known as the primary key of another table, is called a foreign key. Understanding the forei
Select first N rows in MySQL
Publish Date:2025/04/11 Views:85 Category:MySQL
-
Sometimes, you have to select first N rows of MySQL database according to your project requirements. n The value of varies according to the requirement; it can be TOP 1 row or TOP 30 rows. We will learn how to select top N rows using the cl