Check For Duplicate Records VS try/catch Unique Key Constraint

Posted by Jed on Stack Overflow See other posts from Stack Overflow or by Jed
Published on 2010-03-27T18:56:09Z Indexed on 2010/03/27 19:03 UTC
Read the original article Hit count: 385

Filed under:
|
|
|

I have a database table that has a Unique Key constraint defined to avoid duplicate records from occurring.

I'm curious if it is bad practice to NOT manually check for duplicate records prior to running an INSERT statement on the table.

In other words, should I run a SELECT statement using a WHERE clause that checks for duplicate values of the record that I am about to INSERT. If a record is found, then do not run the INSERT statement, otherwise go ahead and run the INSERT....

OR

Just run the INSERT statement and try/catch the exception that may be thrown due to a Unique Key violation.

I'm weighing the two perspectives and can't decide which is best- 1. Don't waste a SELECT call to check for duplicates when I can just trap for an exception VS 2. Don't be lazy by implementing ugly try/catch logic VS 3. ???Your thoughts here??? :)

© Stack Overflow or respective owner

Related posts about sql

Related posts about select