通过检查添加可延迟的唯一约束

发布于 2025-01-16 07:15:32 字数 507 浏览 1 评论 0原文

我想添加一个带有检查的可延迟唯一约束,例如

ALTER TABLE locations 
   ADD CONSTRAINT short_name 
    UNIQUE (hallway, row, module, level, position, zone_id) 
    CHECK (active) DEFERRABLE

但这是不可能的。

上下文:我有一个表,其中给定的字段组合必须是唯一的,仅当字段活动设置为 true 时(可能的检查?)。通常,我们会收到必须作为原子事务执行的更新列表,这就是约束必须可延迟的原因。

这个问题的答案显示为选项一唯一索引,但它不可推迟。

有没有可能的解决方案或解决方法?

I would like to add a deferrable unique constraint with a check, such as

ALTER TABLE locations 
   ADD CONSTRAINT short_name 
    UNIQUE (hallway, row, module, level, position, zone_id) 
    CHECK (active) DEFERRABLE

But it is not possible.

Context: I have a table in which a given combination of fields must be unique, just when the field active is set on true (Possible CHECK?). Normally we receive a list of updates that must be performed as an atomic transaction, that is why the constraint must be deferrable.

The answer to this question shows as an option one UNIQUE INDEX, but it is not deferrable.

Is there any possible solution or workaround?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文