【问题标题】:unique index in postgreSQLpostgreSQL 中的唯一索引
【发布时间】:2018-08-13 10:27:59
【问题描述】:

我有一个带有唯一索引

的 PostgreSQL 表
CREATE SEQUENCE public.batterysensorhistory_id_seq NO MINVALUE NO 
MAXVALUE NO CYCLE;
CREATE TABLE batterysensorhistory
(
    id integer DEFAULT 
nextval('batterysensorhistory_id_seq'::regclass) PRIMARY KEY NOT NULL,
batteryid uuid NOT NULL,
sensorid integer NOT NULL,
isactivelink boolean DEFAULT false NOT NULL,
...
);

create unique index on batterysensorhistory (batteryid, sensorid, isactivelink)
where isactivelink = TRUE;

我想设置一个约束,只有具有相同电池 ID && 传感器 ID 的一行可以具有 isactivelink = true,但我的 唯一索引 不起作用。

另外,我在 sqlfiddle 中创建了这个示例:http://sqlfiddle.com/#!15/c3b37/1

知道我的代码有什么问题吗?

【问题讨论】:

    标签: sql postgresql indexing unique unique-index


    【解决方案1】:

    我想你想要:

    create unique index on batterysensorhistory (batteryid, sensorid)
        where isactivelink = TRUE;
    

    但你的版本应该可以工作。

    Here 是它失败的一个例子。

    【讨论】:

      猜你喜欢
      • 2018-12-02
      • 1970-01-01
      • 2020-10-01
      • 2015-04-24
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多