简体   繁体   English

如何将MySQL数据库A的表中的行添加到数据库B中的现有表中

[英]How do I add rows from a table of a MySQL database A to an existing table in database B

I'm using MySQL. 我正在使用MySQL。 I have a "customers" table in database A (located on server). 我在数据库A(位于服务器上)中有一个“客户”表。

I want to add them to the customer table in database B (located on localhost) without deleting anything. 我想将它们添加到数据库B(位于localhost中)的客户表中,而不删除任何内容。

I'm very new to SQL commands or MySQL in general so try to be the most explanatory as you can. 一般而言,我对SQL命令或MySQL还是很陌生,因此请尝试尽可能多地进行说明。 If you need more information about something I will edit the post and add it. 如果您需要有关某事的更多信息,我将对其进行编辑并添加。

(I have MySQL workbench) (我有MySQL工作台)

Thanks. 谢谢。

Just use this on localhost: 只需在localhost上使用它:

mysqldump -u YOUR_DATABASE_USER -p YOUR_DATABASE_PASS -h YOUR_SERVER_IP databaseA customers > customer.sql
mysql -u YOUR_DATABASE_USER -p YOUR_DATABASE_PASS databaseB < customer.sql

PD: If want some explanation just tell me PD:如果想要一些解释,请告诉我

On server (DB A) : 在服务器上(数据库A)

# Sets our database as default, so we wont have to write `database`.`table` in the consecutive queries.
# Replace *database* with your database name. 
USE *database*;

# Makes a copy of the customers table - named customers_export.
# This copy contains all the fields and rows from the original table (without indexes),
# and if you want, you can add a WHERE clause to filter out some data
CREATE TABLE `customers_export` SELECT * FROM `customers`;

Since you are using mysql_workbench, Do a Data Export (in MANAGEMENT section) by choosing the relevant database and only the customers_export table. 由于您使用的是mysql_workbench,因此通过选择相关数据库和 customers_export表来进行数据导出(在“管理”部分中)。

On localhost (DB B) : 在localhost(DB B)上

Assuming the database name is the same (otherwise you will need to change the database name in the dump file), do a Data Import/Restore by selecting the dump file which we exported in the previous step. 假设数据库名称相同(否则,您将需要在转储文件中更改数据库名称),则通过选择上一步中导出的转储文件来执行数据导入/还原。 This will create the customer_export table. 这将创建customer_export表。

# Set as default
USE *database*;

# If the data you want to import contains *NO* collisions (same primary or unique values in both tables), the structure and table name is the same
INSERT INTO `customers` SELECT * FROM `customers_export`;

And we are done. 我们完成了。
If it does have collisions, or you want to play change the column names, some values and etc - you will need to either modify the select statement or update the customers_export table to suit your needs. 如果确实有冲突,或者您想更改列名,某些值等,则需要修改select语句或更新customers_export表以适合您的需求。

Also, back up the customers table on the second server - in case something goes wrong with the insert. 另外,请备份第二台服务器上的customers表,以防插入出现问题。

Finally - drop the customers_export table on both servers. 最后,在两台服务器上都放置customer_export表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM