Database design: OR relation

后端 未结 3 1619
野性不改
野性不改 2020-12-11 23:13

I have a database with a \"users\" table containing data about my users. Each user is to be linked to a company or a college. I wish to have two separate tables \"college\"

相关标签:
3条回答
  • 2020-12-11 23:30

    I would create relationtables. UserCollege and UserCompany. This way you are even able to have users that are linked to both if needed in the future. If not you simply just create a relationrecord on one of the both

    0 讨论(0)
  • 2020-12-11 23:31

    You could use an 'institution' or 'organisation' lookup table, with a structure something like

    InstitutionId[PK], InstitutionType, LookupKey
    

    where LookupKey is the PK to either Company or College.

    Or,

    InstitutionId[PK], CompanyId[FK], CollegeId[FK]
    

    In both cases you link from user to institution, then onto Company and/or College.

    I personally prefer the second option, because it allows you to easily validate the FK relationship and also allows (if applicable) for a user to be a member of a company and/or a college.

    0 讨论(0)
  • 2020-12-11 23:45

    You can use subtype/super-type relationship. Keep all common fields in the organization table. College and company tables contain only fields specific to those entities.

    0 讨论(0)
提交回复
热议问题