I understand that there exists INSERT IGNORE
and INSERT ... ON DUPLICATE KEY UPDATE
. However, when there is a duplicate key, I'd like to do a INSERT
to a temporary table to keep a record of the unique key that has been violated, so that I can output it to the user.
Is there any way I can do a ON DUPLICATE INSERT
? If it helps, I'm trying to do a bulk insert.
With
ON DUPLICATE KEY UPDATE
, you cannot insert into another table - nor is there an alternative function available.Two custom/alternative ways you can accomplish this:
Using a
stored procedure
as outlined in the accepted answer to this question: MySQL ON DUPLICATE KEY insert into an audit or log tableCreating a
trigger
that logged everyINSERT
for your table into another table and querying the table full of "logs" for any duplicates.Something similar to this should work:
To get a list of the duplicates in the table, you could us: