-1

I have a Hotel table and a City table. If I'd like to add a new column city in Hotel table which is a foreign reference of City, is there a way to automatically update the city reference of existing records according to some logic such as hotel address (which already exists in the hotel table)

ALTER TABLE hotel
  ADD city INTEGER REFERENCES city (city_id); 
  # for all existing hotels, I should update hotel.city = hotel.address.city
  • UPDATE hotel SET city = ? WHERE city = NULL – Evan Carroll Jan 8 at 17:32
  • @EvanCarroll How do I call column of another table here? if it's a conditional boolean of one-to-many relationship? – LunaticJape Jan 8 at 18:19
  • 2
    Please add your table schema and a detailed explanation on how this column should be updated. Have a look at how to set up a Minimal, complete and verifiable example – McNets Jan 8 at 19:47

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.