I have a relational database, in which I want to have a table 'Entity'. This entity has a type: it's either a Person, a Company or a NonProfit. Depending on this type, it should have different properties (columns):
Person:
- id
- name
- age
- statuses: ['child', 'adult', 'senior']
Company:
- id
- name
- address
- contact_form
- industry
- statuses: ['startup', 'scaleup', 'corporate']
NonProfit:
- id
- name
- address
- bank_account
- statuses: ['environmental', 'social']
They all have a name, and in the front-end they should appear in a similar fashion in a list:
Entities:
Name Type Status
----------------------------------
Mary Person Adult
Mcenroe Company Startup
Joe Person Child
BetterWorld NonProfit Social
If you then click on details, depending on the type, the entity is displayed in a different fashion.
I'm about to create 4 different tables: Entity, Person, Company and NonProfit, and then refer to either one of the latter three in the Entity table.
Entity:
- id
- person_id #optional
- company_id #optional
- nonprofit_id #optional
Then somehow enforce that the entity has only one of the three foreign keys. But it feels clunky, so I think there is a better way. I'm not too experiences with relational databases though.
Whats the best way to enable this kind of data in a relational database?