我有一张桌子:
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是一个定义操作类型的整数.某些操作类型对于每个任务必须是唯一的,而有些则不是.
所以我需要一个类型的约束:
UNIQUE(“TaskId”,“TransportActionId”)适用于所有使用TransportActionId的操作!=(2 || 3 || 4).
因此除ActionId = 2,3或4之外的所有动作.
有任何想法吗?
解决方法
使用
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