如何更新具有约束的表中的值?

发布于 2025-02-13 16:16:49 字数 519 浏览 0 评论 0原文

示例表:ID_REL

id | other_id
-----------
1  | 123
-----------
2  | 456
-----------
3  | 123

列ID和其他_ID有一个约束。该表是一个关系表。我想将所有“ 123”值更新为表中已经存在的“ 456”。我尝试过这样简单的事情:

UPDATE id_rel
SET other_id = 456
WHERE other_id = 123;

当我尝试以上时,我会收到以下错误的消息:

ERROR: duplicate key value violates unique constraint "id_rel" Detail: Key (id, other_id)=(1, 456) already exists.

如何更改这些值而无需必须删除约束并基本上重建表?

Example Table: id_rel

id | other_id
-----------
1  | 123
-----------
2  | 456
-----------
3  | 123

There is a constraint on columns id, and other_id. The table is a relation table. I'd like to update all '123' values to '456' which already exist in the table. I've tried something as simple as:

UPDATE id_rel
SET other_id = 456
WHERE other_id = 123;

When I try the above I get a message like the following error:

ERROR: duplicate key value violates unique constraint "id_rel" Detail: Key (id, other_id)=(1, 456) already exists.

How can I change these values without having to remove the restraints and basically rebuild the table?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最笨的告白 2025-02-20 16:16:49

密钥“ 456”是独特的约束,并且此约束已为另一个记录而已。

您必须合并两个记录或删除占据约束值的记录

The key "456" as an unique constraint and this constraint allready exist for a another record.

You have to merge the two record or delete the one who occupy the constraint value

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文