简体   繁体   中英

Specify a field with at most two values equal on mysql

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM