Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

merging two mysql DB's but getting duplicate key error

Status
Not open for further replies.

paublo

ISP
Sep 14, 2006
127
US
Hi, 1st off thanks for looking at this issue.

I have two databases that have the same structure and I want to be able to merge the databases together however im getting a duplicate key error.
I'm trying, insert into squirrelmail_merged.address select * from squirrelmail.address; but im getting the following error, ERROR 1062 (23000): Duplicate entry 'test-test' for key 1.

I tried various "on duplicate key" syntax but i cant seem to figure it out. If someone can let me know what i can do to merge the 2 DB's and have the unique keys for the db im importing changed that would be great.

TIA, P

below is the table structure, both DBs are identical.

Code:
mysql> show tables;
+-------------------------------+
| Tables_in_squirrelmail_merged |
+-------------------------------+
| address                       |
| userprefs                     |
+-------------------------------+
2 rows in set (0.00 sec)

mysql> describe address;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| owner     | varchar(128) |      | PRI |         |       |
| nickname  | varchar(16)  |      | PRI |         |       |
| firstname | varchar(128) |      | MUL |         |       |
| lastname  | varchar(128) |      |     |         |       |
| email     | varchar(128) |      |     |         |       |
| label     | varchar(255) | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

mysql> describe userprefs;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| user    | varchar(128) |      | PRI |         |       |
| prefkey | varchar(64)  |      | PRI |         |       |
| prefval | blob         |      |     |         |       |
+---------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
 
Chris

I read that but im still not sure what syntax i need to use to, im looking for someone to give me the syntax i need based on the tables.

thanks, P
 
That all depends on what you are using to insert the data from.

CSV, Text File, SQL command file or????

And it's not the tables that will decide how INSERT IGNORE or UPDATE ON DUPLICATE need to be written but the DATA that is being merged.

For a start we don't know whether the whole row is identical so can't tell you if it should be updated to a new value or be simply ignored.



Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
i don't think that is straightforward as you may break foreign key dependencies

assuming owner == user then are these numeric keys? (i see they are varchars) If they are numeric then just add an arbitrarily large number to them. then merge.

if not, then i don't think that an automated merge will be easy. it would be easier/more secure from a data integrity perspective, to write some code to iterate the tables and merge them through inserts.
 
I just went with the ignore syntax which if it finds a duplicate it will not insert it. The dups are email address mostly.
 
yes - but that may break foreign key dependencies between the tables. so good luck!
 
as above. i'd programmatically analyse each record importing each table join into an object; decide based on business rules what do in the event of a conflict, and then act on that via writing a new record, merging against existing records or dropping the new object.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top