Postgresql Postgres唯一日期列

Postgresql Postgres唯一日期列,postgresql,date,unique,Postgresql,Date,Unique,我想创建具有唯一日期列的表。可能吗 我有以下表格: CREATE TABLE senders ( id bigint NOT NULL, number bigint NOT NULL, inserted_at date NOT NULL ); 我想确保每个日期都有唯一的值。例如,我可以插入(1,1,'2017-01-01'),(1,1,'2017-01-02'),但我不能再添加一次(1,1,'2017-01-01')。我尝试使用唯

我想创建具有唯一日期列的表。可能吗

我有以下表格:

CREATE TABLE senders (
id              bigint  NOT NULL,
number          bigint  NOT NULL,
inserted_at     date    NOT NULL
); 
我想确保每个日期都有唯一的值。例如,我可以插入
(1,1,'2017-01-01'),(1,1,'2017-01-02')
,但我不能再添加一次
(1,1,'2017-01-01')
。我尝试使用唯一约束创建表或唯一索引,但总是出现SQL唯一异常

CREATE UNIQUE INDEX senders_unique ON senders (id, number, inserted_at);

CREATE TABLE senders (
id              bigint  NOT NULL,
number          bigint  NOT NULL,
inserted_at     date    NOT NULL,
UNIQUE(id, number, inserted_at)
);

有可能吗。感谢所有答案。

唯一索引是选项。如果您在创建它时出错,那么您可能已经有了重复项。您得到的错误到底是什么?哪种说法?好的。上次插入我在冲突中使用的值时,什么都不做。没有它,我发现我在数字列上有额外的唯一索引。现在我工作。对不起,我的错