加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

具有排除的PostgreSQL唯一约束

发布时间:2020-12-13 15:57:45 所属栏目:百科 来源:网络整理
导读:我有一张桌子: 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 witho
我有一张桌子:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读