sql – 重写字段的外键

在Postgresql中我需要重构一个表(Purchases);它有另一个表(Shop)的外键.相反,我想要两个以文本方式保持关系的字段.我不能丢失任何信息,表格已包含数据.
Purchases.shop_id: (long)          -- is the field I need to drop
Purchases.shop: (characters)       -- will hold the Shop's name
Purchases.shop_user: (characters)  -- will hold the Shop's user name.

Shop.id: (long,pk)      -- still referenced from Purchases
Shop.name: (characters)  -- Shop's name
Shop.user: (characters)  -- Shop's user name

两个字段是必需的,因为Shop在(名称,用户)上是唯一的(或者当然是id).

ALTER TABLE Purchases ADD COLUMN shop CHaraCTER varying(255);
ALTER TABLE Purchases ADD COLUMN shop_user CHaraCTER varying(255);

-- ???

ALTER TABLE Purchases DROP CONSTRAINT shop_id_fk;
ALTER TABLE Purchases DROP COLUMN shop_id;

所以开始和结束很容易,有人可以帮助中间部分吗?

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...