Reputation: 503
Im working on a database and I have a problem with one of my relations, I have a Person entity and an Address entity and there is a OneToMany relation between Person and Address. I want to normalize the Address entity so if you have multiple Persons with the same Address, there is only one address in the database. Here is a visualization:
Person 1: { id: 1, name: "foo", address_id: 1 }
Person 2: { id: 2, name: "bar", address_id: 1 }
Address 1: { id: 1, street: "barstreet", housenumber: 123 }
If I want to change the address for Person 1 I dont want to change Address 1 because person 2 is still linked to it so I want this to happen:
Person 1: { id: 1, name: "foo", address_id: 2 }
Person 2: { id: 2, name: "bar", address_id: 1 }
Address 1: { id: 1, street: "barstreet", housenumber: 123 }
Address 2: { id: 1, street: "bazstreet", housenumber: 5 }
What is the best way of solving this problem, thanks in advance.
Upvotes: 1
Views: 693
Reputation: 129
The structure of you database looks fine. I suppose that you have cases with many people which are living at the same address :)
When you need to update an address of a person, I would do the next flow:
Upvotes: 2