Sample Header Ad - 728x90

Database Design - Relationship between two or more junctions tables

2 votes
3 answers
680 views
I am a junior developer and I don't know how to implement this next relationship. > **Note: All entities described here are fictional but have equivalents in an actual production Database. Performance is irrelevant (OK with too many junctions tables) but would be nice to optimize the schema.** >**Additional Information: Using Entity Framework Code First to generate the tables on SQL Server.** **Context** ----------- I have an entity named People, another named HomeAddress and finally one named Job. They have a many-to-many relationship between each other such as People Job and Job HomeAddress. (Junction table have an underscore in their name). This will need to also be replicated with other tables, including one for PhoneNumber. enter image description here **Problem** ----------- How do I link People and Address given this situation. **The issue is some Jobs have an Address while others don't**. I need a relationship between People and Job and also ***specify*** an Address if available (all visual checks will be done on client side). - Example 1: Alice has a Job1 without an Address. Data is inserted into Database without specifying an Address. - Example 2: Bob has a Job2 with 2 Addresses. Bob chooses 1 Address from the list. Data is inserted into Database specifying 1 Address for Job2. - Example 3: Charlie has a Job3 with 4 Addresses. Charlie chooses all 4 of them. Data is inserted specifying all 4 Addresses for Job3. I don't know how to store those informations given this schema since Address should be dependant on Job and if Job is removed from People, so should Address too. **Tried Solutions** ------------------- - I tried to have a link between Job_People and HomeAddress, but since Job can have that information, this seems redundant. - Another solution I can think of is to also have an additional junction table between People and HomeAddress like so People HomeAddress. I just don't know if that is the right decision. > **Again, all entities names are irrelevant, but their relationship is what I am looking for.** Any help is appreciated.
Asked by Codingwiz (123 rep)
Jul 21, 2022, 09:18 PM
Last activity: Jul 28, 2022, 04:43 PM