aahhaa
aahhaa

Reputation: 2275

php mySQL check for unique value needed on Unique col?

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

Answers (1)

Gordon Linoff
Gordon Linoff

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

Related Questions