gpt4 book ai didi

sql - 如何更改postgresql中的外键值?

转载 作者:行者123 更新时间:2023-11-29 11:24:27 25 4
gpt4 key购买 nike

假设我有两个表:CustomerCity。有许多 Customer 住在同一个 City。这些城市有一个作为主键的 uid。客户通过 Customer.city_uid 拥有对其各自城市的外键引用。

由于外部原因,我必须将两个 City.uid 相互交换。但客户应该留在他们的城市。因此,也有必要交换 Customer.city_uid。所以我想我首先交换 City.uid,然后通过 UPDATE 语句相应地更改 Customer.city_uid。不幸的是,我不能这样做,因为这些 uids 是从 Customer 表中引用的,而 PostgreSQL 阻止我这样做。

是否有一种简单的方法可以将两个 City.uid 以及 Customer.city_uid 相互交换?

最佳答案

一个解决方案可能是:

BEGIN;
1. Drop foreign key
2. Make update
3. Create foreign key
COMMIT;

或者:

BEGIN;
1. Insert "new" correct information
2. Remove outdated information
COMMIT;

关于sql - 如何更改postgresql中的外键值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12892826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com