如何更新具有约束的表中的值?
示例表: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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
密钥“ 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