How do I guarantee row uniqueness in MySQL without the use of a UNIQUE constraint?
Posted
by MalcomTucker
on Stack Overflow
See other posts from Stack Overflow
or by MalcomTucker
Published on 2010-03-16T14:01:23Z
Indexed on
2010/03/16
14:06 UTC
Read the original article
Hit count: 170
mysql
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..
© Stack Overflow or respective owner