标签: exclusion-constraint

PostgreSQL EXCLUDE USING 错误:数据类型整数没有默认操作符类

在 PostgreSQL 9.2.3 我试图创建这个简化的表:

CREATE TABLE test (
    user_id INTEGER,
    startend TSTZRANGE,
    EXCLUDE USING gist (user_id WITH =, startend WITH &&)
);
Run Code Online (Sandbox Code Playgroud)

但我收到此错误:

ERROR:  data type integer has no default operator class for access method "gist"
HINT:  You must specify an operator class for the index or define
       a default operator class for the data type.
Run Code Online (Sandbox Code Playgroud)

PostgreSQL的文档使用这个例子不为我工作:

CREATE TABLE room_reservation (
    room text,
    during tsrange,
    EXCLUDE USING gist (room WITH =, during WITH &&)
);
Run Code Online (Sandbox Code Playgroud)

同样的错误信息。

而这个对我来说也不起作用: …

postgresql constraint installation exclusion-constraint gist-index

47
推荐指数
2
解决办法
3万
查看次数

日期范围的唯一性约束

考虑一个prices包含这些列的表:

id         integer primary key
product_id integer -- foreign key
start_date date not null
end_date   date not null
quantity   integer
price      numeric
Run Code Online (Sandbox Code Playgroud)

我希望数据库强制执行这样的规则,即产品在日期范围内(通过where <date> BETWEEN start_date AND end_date)的特定数量只能有一个价格。

这种基于范围的约束可行吗?

postgresql database-design exclusion-constraint postgresql-9.4

15
推荐指数
1
解决办法
8074
查看次数

创建 PostgreSQL 约束以防止唯一组合行

想象一下你有一个简单的表:

name | is_active
----------------
A    | 0
A    | 0
B    | 0
C    | 1
...  | ...
Run Code Online (Sandbox Code Playgroud)

我需要创建一个特殊的唯一约束,该约束在以下情况下失败:相同is_active值不能共存不同的name值。

允许条件示例:

注意:简单的多列唯一索引不允许这样的组合。

A    | 0
A    | 0
B    | 0
Run Code Online (Sandbox Code Playgroud)

允许条件示例:

A    | 0
B    | 1
Run Code Online (Sandbox Code Playgroud)

失败条件示例:

A    | 0
A    | 1
-- should be prevented, because `A 0` exists
-- same name, but different `is_active`
Run Code Online (Sandbox Code Playgroud)

理想情况下,我需要唯一约束或唯一部分索引。触发器对我来说更成问题。

双重A,0允许,但(A,0) (A,1)不是。

postgresql exclusion-constraint

11
推荐指数
1
解决办法
2756
查看次数

限制在边界上的不重叠矩形

我正在尝试对电路板上零件的放置进行建模。没有任何有意义的限制,我的基本架构如下所示:

create table part (
    part_id bigserial primary key,
    name text not null,
    width double precision not null,
    height double precision not null
);
create table board (
    board_id bigserial primary key,
    width double precision not null,
    height double precision not null
);
create table board_part (
    board_id bigint not null references board,
    part_id bigint not null references part,
    position point not null
);
Run Code Online (Sandbox Code Playgroud)

SQL Fiddle可视化

对于bb2any board_parts,我想强制执行以下约束:

  1. b位于黑板上:

    box(b.position, …
    Run Code Online (Sandbox Code Playgroud)

postgresql database-design referential-integrity spatial exclusion-constraint

5
推荐指数
1
解决办法
529
查看次数

每个项目最多强制执行一个 null 的唯一约束

我正在使用 PostgreSQL 9.3。假设我们正在制造某种类型的某些物品,在任何时候我们最多只能生产某种类型的一件物品。下表记录了我们制造物品的历史记录,其中可能有一行manufactured_untilnull- 这些是当前生产的物品。

create table item
(
  id int,
  type_id int,
  manufactured_from timestamp,
  manufactured_until timestamp
)
Run Code Online (Sandbox Code Playgroud)

样本数据:

1  | 101  | 1.1.2000    | 31.12.2012
2  | 102  | 1.4.2003    | 1.1.2010
3  | 101  | 1.1.2013    | 
4  | 102  | 2.1.2010    | 4.5.2014
5  | 102  | 5.5.2014    | 
Run Code Online (Sandbox Code Playgroud)

以下逻辑应该成立:对于每种物品类型,此时最多应该生产一个物品 ( manufactured IS NULL)。在示例中,我应该无法添加记录(6, 101, 27.8.2014, NULL)

我想写一个UNIQUE约束来保护它。是否可以?对于奖励积分,是否有一种相当复杂的方法来保护一种项目类型的间隔不重叠?

postgresql database-design exclusion-constraint unique-constraint

5
推荐指数
1
解决办法
2158
查看次数

基于其他行中的值对行值的约束

是否可以有一个包含四列的表:

CREATE TABLE accrual (
    add_date date NOT NULL,
    user_id integer NOT NULL,
    rate integer NOT NULL,
    amount numeric(7,3)
); 
Run Code Online (Sandbox Code Playgroud)

然后对其进行限制,以便如果我已经有一个条目,例如

('2016-04-01', 3, 120, 25.6)
Run Code Online (Sandbox Code Playgroud)

尝试插入具有相同日期和 user_id 但不同速率的另一个条目会失败吗?IE

('2016-04-01', 3, 140, 15)
Run Code Online (Sandbox Code Playgroud)

会失败,但是

('2016-04-02', 3, 140, 15)
Run Code Online (Sandbox Code Playgroud)

或者

('2016-04-01', 4, 140, 15)
Run Code Online (Sandbox Code Playgroud)

或者

('2016-04-01', 3, 120, 15)
Run Code Online (Sandbox Code Playgroud)

会好吗?

需要明确的是,日期和 user_id 基本上就像一个索引,除了重复索引完全没问题,只要费率相同。金额不受限制。

我正在使用 postrgesql 9.5 数据库,并且使用 PHP。我的猜测是,最简单的方法是编写一些 PHP 代码来执行检查。我能找到的最接近的问题是这个:

触发或检查约束根据其他值的存在插入值

但它是关于 Oracle 数据库的,并且略有不同。

感谢您的任何建议!

postgresql exclusion-constraint check-constraints

5
推荐指数
1
解决办法
2600
查看次数

限制两个特定列值同时存在

我有一个 PostgreSQL 示例表,其中最多允许有一行不是“c”类型的行。

我将不胜感激任何帮助创建一个强制执行这一点的约束。

CREATE TABLE example
(
  example_id    serial PRIMARY KEY,
  example_state CHAR(1) NOT NULL
);

ALTER TABLE example ADD CONSTRAINT 
  example_constraint
CHECK (example_state = 'a' OR example_state = 'b' OR example_state = 'c');
Run Code Online (Sandbox Code Playgroud)

postgresql database-design constraint exclusion-constraint postgresql-9.3

3
推荐指数
1
解决办法
3147
查看次数

对日期范围设置唯一性约束

我有一个表reservation的列roomno(INTEGER)startdate(DATE)enddate(DATE)与主键(roomno, startdate)。

我如何在表上设置约束,以便不允许预订重叠?

我试图在SQLFIDDLE postgreSQL9.3 中实现这个

例如:

101  2016-01-01   2016-01-05
101  2016-01-03   2016-01-06 [This row should not be possible to insert]
Run Code Online (Sandbox Code Playgroud)

startdate并且enddate是数据类型date

postgresql database-design exclusion-constraint postgresql-9.3 range-types

2
推荐指数
1
解决办法
1074
查看次数