How can I specify on mysql that I want a field which can take at most two values equal? Is it possible with the check instruction?
Asked
Active
Viewed 60 times
2 Answers
0
It's easy to build this behavior into a stored procedure. Just query the table for how many values it already holds of the given criteria and reject the request if it's too many.
AFAIK there is no other mechanism to prevent a third row from being inserted.

Andreas Wederbrand
- 38,065
- 11
- 68
- 78
0
No, you can define a check constraint in MySQL but it is ignored and in other words meaningless.
To overcome this you can define insert/update triggers and throw an error if the value is not one of the desired ones.

juergen d
- 201,996
- 37
- 293
- 362
-
I tried using check also for other things, but when I try to insert a value that i shouldn't insert, it doesn't raise any kind of exception. why? check is always ignored in mysql?so is useless? – Seba92 Jun 09 '14 at 10:21
-
Yes it is useless. Maybe it will be used in a future version of MySQL, but currently not. it just hasn't been implemented. – juergen d Jun 09 '14 at 10:22