【问题标题】:Combine a PostgreSQL EXCLUDE range constraint with a UNIQUE constraint将 PostgreSQL EXCLUDE 范围约束与 UNIQUE 约束相结合
【发布时间】:2015-11-01 15:46:45
【问题描述】:

在 PostgreSQL 中,如何将范围列上的排除约束与其他标量列上的唯一约束结合起来。或者换一种说法,如何确保范围重叠检查仅与对其他一些列的唯一检查结合使用?

例如,假设我有:

CREATE TABLE reservation (
    restaurant_id int,
    time_range tsrange
);

我想确保对于每个restaurant_id,没有重叠的time_ranges。

我知道我可以像这样创建一个独占范围约束:

CREATE TABLE reservation (
    restaurant_id int,
    time_range tsrange EXCLUDE USING gist (time_range WITH &&)
);

但是我如何确保time_range 检查的范围是restaurant_id

【问题讨论】:

标签: postgresql range constraints


【解决方案1】:
CREATE TABLE reservation 
(
    restaurant_id int,
    time_range tsrange,
    EXCLUDE USING gist (restaurant_id with =, time_range WITH &&)
);

请注意,您需要安装扩展btree_gist,因为 GIST 索引默认没有相等运算符:

http://www.postgresql.org/docs/current/static/btree-gist.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 2014-06-18
    • 2021-12-09
    • 2017-08-05
    相关资源
    最近更新 更多