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 check0row returned. If row exists then request for new userid, else (INSERT...)Make
useridcolumnUNIQUEinusertable and directlyINSERT...if0rows affected then request new useridMake
useridcolumnUNIQUEinusertable and directlyINSERT...ifmysqli_errno ($link)==1062then 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 ?