INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

merging two mysql DB's but getting duplicate key error

merging two mysql DB's but getting duplicate key error

merging two mysql DB's but getting duplicate key error

(OP)
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) 

RE: merging two mysql DB's but getting duplicate key error

(OP)
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

RE: merging two mysql DB's but getting duplicate key error

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

RE: merging two mysql DB's but getting duplicate key error

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.

RE: merging two mysql DB's but getting duplicate key error

(OP)
I just went with the ignore syntax which if it finds a duplicate it will not insert it. The dups are email address mostly.

RE: merging two mysql DB's but getting duplicate key error

yes - but that may break foreign key dependencies between the tables. so good luck!

RE: merging two mysql DB's but getting duplicate key error

(OP)
Jpadie, what do you recommend I do?

RE: merging two mysql DB's but getting duplicate key error

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close