shadkamel's avatar

msyql data integeration

hello, i have a system on two separate pc that doesn't have any connection to each other, they are working fine, but i want to merge the both database into one database, does any on have an idea how to do it ?

0 likes
7 replies
LaryAI's avatar
Level 58

One solution could be to export the data from both databases into CSV files, then merge the CSV files and import them into a new database. Here are the steps:

  1. Export the data from the first database into a CSV file. You can do this using the following command in the MySQL command line:
SELECT * INTO OUTFILE '/path/to/file.csv' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' FROM table_name;

Replace /path/to/file.csv with the path where you want to save the CSV file, and table_name with the name of the table you want to export.

  1. Repeat step 1 for the second database, saving the CSV file to a different location.

  2. Merge the two CSV files into one using a tool like Excel or Google Sheets.

  3. Create a new database in MySQL where you want to import the merged data.

  4. Import the merged CSV file into the new database using the following command in the MySQL command line:

LOAD DATA INFILE '/path/to/merged_file.csv' INTO TABLE table_name FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n';

Replace /path/to/merged_file.csv with the path to the merged CSV file, and table_name with the name of the table you want to import the data into.

Note: This solution assumes that the two databases have the same schema (i.e. same table names and columns). If they don't, you'll need to modify the export and import commands accordingly.

m7vm7v's avatar

You can try https://sql-hub.com/ (never used it tho but seems a good try)

If you have two databases with the same schema then you may want to use MySql database with the —no-create-info option. Then you can use it to load it into the target database.

If you have a database with a different schema then you can simply use mysqldump and then go ahead with a surgical merge.

Beware of primary key or auto-generated ids or indexes.

shadkamel's avatar

@m7vm7v my problem is with the primary keys, when i merge the tables it will fails because of duplicate primary keys

Tray2's avatar

@shadkamel There is only one way to do it and preserve the integrity of the database, and that is doing it one table at the time and then update all the foreign keys if needed, then going on the next table.

1 like
shadkamel's avatar
shadkamel
OP
Best Answer
Level 50

i think the solution is be using UUID for all tables in the system that can avoiding duplication in the system, if any one have another idea please tell us :).

Please or to participate in this conversation.