我有一张这样的表:
CREATE TABLE IF NOT EXISTS tt
(key1 integer
,key2 integer
,data varchar(8)
,CONSTRAINT cst_tt_key1_key2 UNIQUE (key1, key2)
);
CREATE INDEX idx_tt_data_trigram
ON data
USING gist(data gist_trgm_ops);
我希望在data
的相同值下具有key1
的唯一值。
换句话说,可以使用以下记录。
1, 1, "Postgres"
2, 1, "Postgres"
但是,不允许以下记录。
1, 1, "Postgres"
1, 2, "Postgres"
知道如何使用CHECK
或CONSTRAINT
吗?
答案 0 :(得分:0)
只需创建一个唯一索引。
create unique index on tt(key1, data)