简体   繁体   中英

Database design best practices - linking/junction table with multiple references

I have membership and person tables linked together with membership_person linking table in many-to-many relationship (columns: person_id and membership_id)

Each person can have one or more address, phone and email. These are stored in separate tables eg address, phone and email tables.

The new scenario I am facing now is where a person that has multiple memberships but he/she prefers to use different address, phone number and email for each membership.

Here is an example:

M1 => P1 
   => address-1 
   => phone-1 
   => Email-1

M2 => P1 
   => address-2 
   => phone-2 
   => Email-2

M3 => P1 
   => address-3 
   => phone-3 
   => Email-3

What is the database best practice to deal with the above? should I create a separate linking tables eg membership_address, membership_phone and membership_email? or just add address_id, phone_id, email_id to the existing membership_person table?

Thanks :)

This is called association data - data about the association between two entities - and you store it on the association table.

Something like (using mysql syntax):

create table membership_person (
    person_id int not null references person,
    membership_id int not null references membership,
    address_id int not null references address,
    phone_id int not null references phone,
    email_id int not null references email,
    primary key (person_id, membership_id)
)

The extra 3 columns define the contact entity for a person for each membership .

Extending, below is a suggestion.

Table : Membership_Person

Compound Primary Key (membership_id, person_id, table_id, priority)

Fields :

  • membership_id - references the membership table
  • person_id - references the person table
  • table_id - indicates either the phone, address, or email table as the referenced table for this row
  • table_entry_id - primary key for the referenced table
  • priority - the preferred entry for this person + membership + (phone, address, or email)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM