How to ignore duplicated entries problem in Primary key field in MySQL ?

While I was working on a HUGE database processing, it was about 3,4 M. rows., I faced a problem which is “Add row with ID which already inserted in before in Primary key field”, for example:

1 item description
1 item description_2

They are not identical but the primary keys are. Since primary keys have to be unique this throws an error.
Here is how we get around this error:

$sql = "INSERT IGNORE INTO ".$table." (".$column.") VALUES (".$value.")";

This will drop the second line and continue on with the script.

$sql = "INSERT INTO ".$table." (".$column.") VALUES (".$value.")"  ON DUPLICATE KEY UPDATE ".$column[0]."=".$column[0]+1"";

This will update the previous row. Most of the times you do not want to change a primary key if the data is dumped to you. So I cannot stress to you enough to emphasize to your data feed distribution to give you nice, clean dumps!!!

Leave a comment


WP-SpamFree by Pole Position Marketing