简体   繁体   English

如何强制另一列的每个名称(在枚举中)在列中只有一个“真”值?

[英]How to enforce that there is only one `true` value in a column per names (in an enum) of another column?

I have the following structure with an enum { 'ready', 'set', 'go'} on name :我有一个枚举结构如下{ 'ready', 'set', 'go'}name

|----------------------|---------------------------|------------------------|
|          Id          |          enabled          |          name          |
|----------------------|---------------------------|------------------------|
|          1           |          true             |        'ready'         |
|----------------------|---------------------------|------------------------|
|          2           |          false            |        'ready'         |
|----------------------|---------------------------|------------------------|
|          3           |          false            |        'ready'         |
|----------------------|---------------------------|------------------------|
|          4           |          false            |        'set'           |
|----------------------|---------------------------|------------------------|
|          5           |          true             |        'set'           |
|----------------------|---------------------------|------------------------|
|          6           |          true             |        'go'            |
|----------------------|---------------------------|------------------------|
|          7           |          false            |        'go'            |
|----------------------|---------------------------|------------------------|

How can I put a constraint on it so that there will only ever be 3 true 's (one on ready , one on set , and one on go )?我如何对其施加约束,以便只有 3 个true (一个在ready ,一个 on set ,一个 on go )?

You can use filtered unique indexes (or as Postgres calls them "partial indexes" ):您可以使用过滤的唯一索引(或者 Postgres 称它们为“部分索引” ):

create unique index unq_t_name
    on t(name)
    where enabled;

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

相关问题 SQLSERVER - 有没有办法强制每行只有一位列为真,其他所有列为假? - SQLSERVER - is there a way to enforce that only one bit column is true and all others are false, per row? 用于在位列中为每个表强制执行单个TRUE值的索引 - Index to enforce a single TRUE value per table in bit column 如何在SQL中每列值仅选择一行? - How to select only one row per column value in SQL? 如何仅将列的一个值设置为true并设置为false - How to set only one value of column as true and rest false 如果值为真,则获取列名 - Get column names if value is true 如何按照特定ID将一列值减去另一列值 - How to substract one column value to another column value as per Particular ID Postgres:强制“belongs_to”关联组在任何给定时间只有一个值为“true”的列 - Postgres: Enforce group of `belongs_to` associations to have exactly one column at any given time with a value of `true` 仅将每个值的行保留在一个列中(在基于时间的情况下)在另一列中满足特定条件之前 - Only keep rows per each value in one column BEFORE (time-based) a certain condition is fulfilled in another column 如何强制表的一列中的值不属于同一表的另一列 - How to enforce values in one column of the table are not part of another column of the same table 查找在另一列中为真的一列的范围 - Finding the range of one column that is true in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM