I'm trying to make basic database structure with a list of NaturalPersons, LegalEntities and their SocialNetworkAccounts, simplified structure of this tables looks like this:
NaturalPersons table:
– ID [number] AI PK
– Name [text]
– Surname [text]
LegalEntities table:
– ID [number] AI PK
– CompanyName [text] unique
SocialNetworkAccounts table:
– ID [number] AI PK
– SocialNetworkLink [text]
– SubjectID [number] FK
The problem is that I can only link SubjectID to either NaturalPersons.ID or LegalEntities.ID and I can only think of these workarounds:
I. I can make two tables for SocialNetworkAccounts, like
SocialNetworkAccounts-NP table:
– ID [number] AI PK
– SocialNetworkLink [text]
– PersonID [number] FK
SocialNetworkAccounts-LE table:
– ID [number] AI PK
– SocialNetworkLink [text]
– CompanyID [number] FK
But then I dislike having many similar tables in my database as it complicates queries and further development.
II. I can make Foreign Keys in First two tables instead:
NaturalPersons table:
– ID [number] AI PK
– Name [text]
– Surname [text]
– SocialNetworkLinkID [number] FK
LegalEntities table:
– ID [number] AI PK
– CompanyName [text] unique
– SocialNetworkLinkID [number] FK
SocialNetworkAccounts table:
– ID [number] AI PK
– SocialNetworkLink [text]
But then I won't be able to add multiple entries (social links) per person / company.
III. I was thinking about another option, where I fill either PersonID or CompanyID:
NaturalPersons table:
– ID [number] AI PK
– Name [text]
– Surname [text]
LegalEntities table:
– ID [number] AI PK
– CompanyName [text] unique
SocialNetworkAccounts table:
– ID [number] AI PK
– SocialNetworkLink [text]
– PersonID [number] FK
– CompanyID [number] FK
But SQL requires data in FK columns to be present. And I need something like a "composite foreign key", where it is "either PersonID or CompanyID" is present.
So I'm puzzled about best practices for such situations. I'm also not sure how the feature (if exists) might be called.