将 PostgreSQL EXCLUDE 范围约束与 UNIQUE 约束相结合
Combine a PostgreSQL EXCLUDE range constraint with a UNIQUE constraint
在 PostgreSQL 中,如何将范围列上的排除约束与其他标量列上的唯一约束结合起来。或者换句话说,如何确保范围重叠检查仅与对其他一些列的唯一检查结合使用?
例如,假设我有:
CREATE TABLE reservation (
restaurant_id int,
time_range tsrange
);
我想确保每个 restaurant_id
都没有重叠 time_range
。
我知道我可以像这样创建一个独占范围约束:
CREATE TABLE reservation (
restaurant_id int,
time_range tsrange EXCLUDE USING gist (time_range WITH &&)
);
但是如何确保 time_range
检查的范围在 restaurant_id
范围内?
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
在 PostgreSQL 中,如何将范围列上的排除约束与其他标量列上的唯一约束结合起来。或者换句话说,如何确保范围重叠检查仅与对其他一些列的唯一检查结合使用?
例如,假设我有:
CREATE TABLE reservation (
restaurant_id int,
time_range tsrange
);
我想确保每个 restaurant_id
都没有重叠 time_range
。
我知道我可以像这样创建一个独占范围约束:
CREATE TABLE reservation (
restaurant_id int,
time_range tsrange EXCLUDE USING gist (time_range WITH &&)
);
但是如何确保 time_range
检查的范围在 restaurant_id
范围内?
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