Search code examples
database-designentity-relationship

Designing many to many relationship between three entities


I'm having trouble modelling this situation. I'm designing a location-based coupon system. A user can define zones and offers and then associate each offer to multiple zones. Also each zone can have more than one offer. So zone and offer have a many to many relationship. The user entity owns both entities. A zone and an offer can only be associated if the same user owns both of them. A diagram can be very helpful. Thanks.


Solution

  • Assuming a user can own own zones and offers independently from whether zones and offers are actually connected, you can do something like this:

    enter image description here

    This is a classic problem of diamond-shaped dependency where the diamond bottom must reference the same diamond top for both diamond sides, which is ensured by using identifying relationships so the top PK is propagated down both sides and then merged at the bottom (note FK1 and FK2 in front of ZONE_OFFER.USER_ID).

    You can always add more (surrogate?) keys to this model if there are external reasons to do so, but this basic structure must exist in some shape or form to ensure only zones and offers of the same user can be connected.