Checking for duplicates before doing an INSERT

Checking for duplicates before doing an INSERT

Before making an INSERT, how could I check with an SQL statement if the value exists already? I have to do it with MySQL, which does not support sub-selects :(((
I

    Requires Free Membership to View

    By submitting your registration information to SearchOracle.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchOracle.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

can do it with SELECT/analyse-result-in-Perl/INSERT, but it is slow.

The process -- do a SELECT, analyse whether the row exists, and if not, then do the INSERT -- works well, but it isn't efficient, as you suggest. The strategy I prefer is to go ahead and do the insert, then analyse whether it was successful.

The reason for this is simple, and is based on the assumption that most of your inserts should be successful. Let's say that out of 100 attempted inserts, 5 will be duplicates. In the select-analyse-insert strategy, you will be executing 195 calls to the database -- 100 selects and 95 inserts. In the insert-analyse strategy, you will be executing only 100 calls to the database, of which 95 will have been successful.

Of course, this strategy requires that the column was declared as UNIQUE in the database, and also that you are able to intercept the "duplicate insert rejected" error message or status code from the database, and handle it gracefully. In MySQL, you can use the mysql_info() function to determine whether the insert was successful.

For More Information

  • What do you think about this answer? E-mail the edtiors at editor@searchDatabase.com with your feedback.
  • The Best SQL Web Links: tips, tutorials, scripts, and more.
  • Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
  • Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
  • Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.

This was first published in April 2002

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.