0

As per a prior question, I created a Unique Index on a name field. I've simplified my import so I don't need to merge fields any longer, just import from SourceTable into DestinationTable, the latter having Unique Index on Name.

I got an error immediately about a duplicate value existing when I tried to do the insert, so I guess that is good news, is there a way to specify an insert that will just skip the duplicate values and go to the next vs throwing out the "[Err] 1062 - Duplicate entry 'Actor' for key 2" and quitting?

Mark H
  • 259
  • 2
  • 10
  • possible duplicate of [MySQL - ignore insert error: duplicate entry](http://stackoverflow.com/questions/812437/mysql-ignore-insert-error-duplicate-entry) – M Khalid Junaid Mar 30 '14 at 20:07

1 Answers1

0

In MySQL you can use insert ignore to ignore all errors, including duplicate key.

To just ignore this one error, you can use on duplicate key update:

insert into t(cols)
    select values
    from wherever
    on duplicate key update col1 = values(col1);

The update part doesn't do anything important. It just does something so no error is reported.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Hi in fact that is the last thing I want to do, all my critical data (in other fields) is in the original table (thank you though). I did some digging and it appears my basic choice is the above (replace the old records with new) or Insert Ignore which keeps the old records and continues on. – Mark H Mar 30 '14 at 20:10
  • @MarkH . . . I have no idea what your comment means. Both of these `insert` statements will leave the original data intact, when a duplicate record is encountered. In the second case, it does depend on which column you choose for the update, but presumably you would choose one of the key columns that is the same in both the original data and the new data. – Gordon Linoff Mar 30 '14 at 20:46