0

is it necessary to check for Unique Value before insert it in to a database? if the unique_col is predefine to be Unique Keys.

for example

 SELECT unique_col FROM table WHERE unique_col != unique_val 
 INSERT INTO table (unique_col) VALUE(:unique_value)

1 Answer 1

1

Is it necessary to check? That depends how you are handling the error.

In general, the database is going to do the check anyway, so an additional check on your part is redundant. If you do the check, another thread might insert the same value between your check and the insert, so you can still get an error (this is called a race condition).

So, don't do the check, but do check for the error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.