We are developing a Spring Boot application backed by a MySQL database and I have a question to which I couldn't find an answer, though there are several similar questions here on SO. The problem is to keep only one insert in the database if there are several parallel identical insert statements ran.
For example, if my Entity has the attributes (Id, parent_id and status)
, we don't want to allow more than one Entity with the same parent_id and status = 1
- even though they come in this form from the client, which spams our endpoint with the same request. I know that other db vendors allow for something called conditional unique key, i.e. that is a unique key like (parent_id, status = 1)
, but MySql does not. Is there any way to achieve this with an index, or even with Stored Procedures?
Similar questions (for reference):
- conditional unique constraint
- SQL can I have a "conditionally unique" constraint on a table?
- https://dba.stackexchange.com/questions/7443/function-based-index-as-a-conditional-unique-key
- Can I conditionally enforce a uniqueness constraint?
- https://dba.stackexchange.com/questions/43/how-to-create-a-conditional-index-in-mysql