|
|
|
|
|
by pilif
4077 days ago
|
|
In many databases, your suggested "where not exists" sub query might not actually protect you but just make the possible window to hit the race much smaller. What happens is that your database would evaluate the subquery, the rest of the where, commit another transaction and then finally run the insert part of your query. There are no guarantees in the SQL standard that queries with subqueries should be atomic. The only truly safe way to protect yourself is to fix the schema in a way that you can make use of unique indexes. Those are guaranteed to be unique no matter what. |
|
Or put the whole thing in a transaction, right?