SQL attributes depending on type

attributes, sql

Solution

One typical choice is a `1:1` extension table:

create table client (id int primary key);
create table person (id int foreign key references client(id), ...columns...);
create table organization (id int foreign key references client(id), ...columns...);

However, my preferred choice is to include all columns in the `client` table. You can have a column for type that is either `person` or `organization`. Columns that are not relevant for the row's type can be null. Your queries will be much simpler that way.

Problem

Let's say I have an entity CLIENT, which can be either PERSON or ORGANIZATION. Depending on which type it is, I have to choose attributes (address, name for organization, date_of_birth,first_name,last_name for person). I have created all three entities, but how can I make the attributes type-dependent? Seen Database design: objects with different attributes, didn't help...

Original source

Related problems