In PostgreSQL I need to refactor a table (Purchases
); it has a foreign key to another table (Shop
). Instead I want two fields that keep the relation in a textual way. I must NOT lose any information, the tables already contain data.
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
Two fields are necessary because a Shop is unique on (name,user)
(or by id
of course).
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;
So the start and the ending is easy, can somebody help with middle-part? :)
I know that foreign keys were made for this but I have to do it this way.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…