我有一张桌子:
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
以外的所有操作。
有什么想法吗?
答案 0 :(得分:7)
使用partial unique index。像:
CREATE UNIQUE INDEX transint_partial_uni_idx
ON dbo."TransportInteraction" ("TaskId", "TransportActionId")
WHERE TransportActionId NOT IN (2,3,4);
具有更多细节的密切相关答案:
Create unique constraint with null columns
Unique combination in a table