Reputation: 2275
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)
Upvotes: 0
Views: 29
Reputation: 1269633
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.
Upvotes: 1