gpt4 book ai didi

postgresql - 当给定列与 PostgreSQL 中的谓词匹配时,考虑行是唯一的

转载 作者:行者123 更新时间:2023-11-29 13:17:36 28 4
gpt4 key购买 nike

在唯一性约束的上下文中,如果给定列具有特定值(例如“false”),是否有一种方法可以将行视为唯一。

举个例子,

这是允许的:

id  | user| location| active
----+-----+----------+----------
1 | 1 | West LA | f
2 | 1 | West LA | f
3 | 1 | West LA | t
4 | 2 | West LA | t

但这不会:

id  | user| location| active
----+-----+----------+----------
1 | 1 | West LA | f
2 | 1 | West LA | t
3 | 1 | West LA | t
4 | 2 | West LA | t

为了实现这一点,我想知道是否可以做一个类似这样的唯一性约束:
唯一(用户,位置)
但只应用它 WHERE active = true

最佳答案

您就快完成了 - 创建一个部分(又名“过滤”)唯一索引:

create unique index on the_table (day, location) 
where active;

关于postgresql - 当给定列与 PostgreSQL 中的谓词匹配时,考虑行是唯一的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46723343/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com