A useful MySQL trick I just learned tonight is using the “INSERT…ON DUPLICATE KEY UPDATE” query syntax along with the LAST_INSERT_ID() function in order to populate the mysql_insert_id() function or the mysqli->insert_id attribute when the record is a duplicate.
At first I had tried using the REPLACE syntax, thinking that would be easier. The problem was that the auto-increment primary key field values kept changing whenever I REPLACEd a duplicate unique value, which was surprising — to me. So then I read the manual and found out that…
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.
That explained why my keys were changing, so I figured I’d have to use INSERT…ON DUPLICATE KEY UPDATE. Well, that didn’t seem to work, as I only got an insert_id value if it was a new entry. Amazingly enough, returning to the manual once again answered my question:
To make LAST_INSERT_ID() meaningful for updates, insert rows as follows:
INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;
So, now I can do what I wanted, which was to read some data in from a CSV file, insert the records while not getting an error if there is a duplicate unique field, and get the insert_id value to use in subsequent queries.
