[英]PostgreSQL Unique Constraint with Exclusions
我有一张桌子:
CREATE TABLE dbo."TransportInteraction"
(
"Id" bigint NOT NULL DEFAULT nextval('dbo."TransportInteraction_Id_seq"'::regclass),
"Message" character varying(256),
"TransportActionId" integer NOT NULL,
"TimeCreated" timestamp without time zone NOT NULL,
"TimeInserted" timestamp without time zone NOT NULL,
"TaskId" bigint
)
通常,此表是对任务的映射操作。 TransportActionId
是一个定义动作类型的integer
。 某些操作类型对于每个任务必须是唯一的,而另一些则不是。
所以我需要一个类型的约束:
UNIQUE ("TaskId", "TransportActionId")
适用于所有具有TransportActionId != (2||3 || 4)
。
因此,除ActionId=2,3 or 4
之外的所有操作。
有什么想法吗?
使用部分唯一索引:
CREATE UNIQUE INDEX transint_partial_uni_idx
ON dbo."TransportInteraction" ("TaskId", "TransportActionId")
WHERE TransportActionId NOT IN (2,3,4);
相关:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.