For My user
table in MySQL, before entering new userid & avoid duplicate userid, I check if similar Id exist. To me there are 3 approach :
Query for new userid (
SELECT ...
) and check0
row returned. If row exists then request for new userid, else (INSERT...
)Make
userid
columnUNIQUE
inuser
table and directlyINSERT...
if0
rows affected then request new useridMake
userid
columnUNIQUE
inuser
table and directlyINSERT...
ifmysqli_errno ($link)==1062
then request new userid
I presently use 1st method but it results in 2 query and intend to switch to second method. I found 3rd approach in a book which confused me !
My question is
- Is 2nd approach better for duplicate entry prevention ?
- Are 2nd & 3rd aprroach same or different ?
- Is there still any better approach to prevent duplicate entry with minimum DB Query ?