Re: Merge Two Databases into one

From: Jerry Stuckle <jstucklex_at_attglobal.net>
Date: Tue, 28 Oct 2014 16:53:43 -0400
Message-ID: <m2ovof$1c3$2_at_dont-email.me>


On 10/28/2014 12:08 PM, ramganesh7k_at_gmail.com wrote:
> Hi all,
>
> I want to append my data's from old_db in to the new_db
> In new_db the table exists and has data already and I am trying to insert old_db data's(rows)
> also maintaining foreign key.
>
> This is my table
>
> -old_db tables
>
> #address table
> id address
> 1 old_street1
> 2 old_street2
>
> #name table
> id Name fk_address_id
> 1 foo1 1
> 2 foo2 2
> 3 foo3 2
>
> -new_db tables:
> #address table
> id address
> 1 new_street1
> 2 new_street2
>
> #name table
> id Name fk_address_id
> 1 bar1 2
> 2 bar2 1
> 3 bar3 2
>
>
> trying to insert old_db in to the new_db
> like this
>
> #address table
> id address
> 1 new_street1
> 2 new_street2
> 3 old_street1
> 4 old_street2
>
> #name table
> id Name fk_address_id
> 1 bar1 2
> 2 bar2 1
> 3 bar3 2
> 4 foo1 3
> 5 foo2 4
> 6 foo3 4
>
> I am trying this
> Dump the old_db using --no-create-info and then load it to
> the new_db from the command line.
>
> I am getting this error:

>>>> mysql -u -p new_db < old_db.sql

> "ERROR 1062 (23000) at line 51: Duplicate entry '1' for key 'PRIMARY'"
> because I have a column(id) with unique entries
>
> I am new to MySQL
>
> what do you think would be the best solution for my problem?
>
> MySQL Version -5.5
>
>
> Thanks,
> Ram Ganesh K
>

If the primary key already exists in the current database, you won't be able to create a second row with the same key. You have to change the key in either the new data or the old data.

Personally, I would create a small script that changed the keys in the data as it is being imported. Of course, you'll have to change it in the foreign key tables, also.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex_at_attglobal.net
==================
Received on Tue Oct 28 2014 - 21:53:43 CET

Original text of this message