MySQL Forums
Forum List  »  General

Re: Can some please help me with a duplicate key error
Posted by: Rick James
Date: May 20, 2013 10:19PM

> I do perform a lookup before the insert to make sure that the booking code is not already on table, and if it is not then I do the insert.

Consider doing
INSERT IGNORE ...
or
INSERT ... ON DUPLICATE KEY UPDATE ...

If you have multiple connections doing the task, you could get errors --
Connection 1: SELECT (and see it is missing)
Connection 2: SELECT (and see it is missing)
Connection 1: INSERT (successful)
Connection 2: INSERT -- Duplicate Key.

The alternatives provide 'atomic' operations, so that can't happen.

Also, InnoDB, plus
BEGIN
SELECT
INSERT
COMMIT
would be "atomic".

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.