微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;

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

原文地址:https://www.jb51.cc/mssql/79966.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐