具有排除的PostgreSQL唯一约束

我有一张桌子:

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

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...