I got a table of users looking like this:
| id | username | active |
There may be multiple entries with the same username and active=0
, but only one unique entry with the same username which has got active=1
set.
What's the best way to ensure that no duplicates can be created by two simultaneous requests?
(Session1:SELECT;0 rows, Session2:Select;0 rows, Session1:Insert, Session2:Insert
=> duplicate entry)
I found out that I could set active=NULL
instead of active=0
and set username&active as unique, which would achieve what I am looking for - but this seems to be a rather hacky solution and doesn't reliably work in all DB engines.
I've already seen recommendations to move active=0
entries to a second table, but this seems very messy too and would create overhead in all the select queries.