How do I guarantee row uniqueness in MySQL without the use of a UNIQUE constraint?
- by MalcomTucker
Hi
I have some fairly simple requirements but I'm not sure how I implement them:
I have multiple concurrent threads running the same query
The query supplies a 'string' value - if it exists in the table, the query should return the id of the matching row, if not the query should insert the 'string' value and return the last inserted id
The 'string' column is (and must be) a text column (it's bigger than varchar 255) so I cannot set it as unique - uniqueness must be enforced through the access mechanism
The query needs to be in stored procedure format (which doesnt support table locks in MySQL)
How can I guarantee that 'string' is unique? How can I prevent other threads writing to the table after another thread has read it and found no matching 'string' item?
Thanks for any advice..