简体   繁体   English

指定一个在mysql上最多等于两个值的字段

[英]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? 如何在mysql上指定我想要一个最多可以取两个相等值的字段? 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. AFAIK没有其他机制可以防止插入第三行。

No, you can define a check constraint in MySQL but it is ignored and in other words meaningless. 不,您可以在MySQL中定义一个检查约束,但是它会被忽略,也就是说没有意义。

To overcome this you can define insert/update triggers and throw an error if the value is not one of the desired ones. 为了解决这个问题,您可以定义插入/更新触发器,如果​​该值不是所需的值之一,则引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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