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 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.
Dig Deeper on Oracle and SQL
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.