[英]PostgreSQL Unique Constraint with Exclusions
I have a table:我有一张桌子:
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
)
Generally, this table is mapping actions on a task.通常,此表是对任务的映射操作。
TransportActionId
is an integer
defining the type of action. TransportActionId
是一个定义动作类型的integer
。 Some action types must be unique per task and some others not.某些操作类型对于每个任务必须是唯一的,而另一些则不是。
So I need a constraint of type:所以我需要一个类型的约束:
UNIQUE ("TaskId", "TransportActionId")
applying to all actions with TransportActionId != (2||3 || 4)
. UNIQUE ("TaskId", "TransportActionId")
适用于所有具有TransportActionId != (2||3 || 4)
。
So all actions except those with ActionId=2,3 or 4
.因此,除
ActionId=2,3 or 4
之外的所有操作。
Any ideas?有什么想法吗?
Use a partial unique index :使用部分唯一索引:
CREATE UNIQUE INDEX transint_partial_uni_idx
ON dbo."TransportInteraction" ("TaskId", "TransportActionId")
WHERE TransportActionId NOT IN (2,3,4);
Related:相关:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.