Skip to content
Advertisement

Postgresql – create table with disjoint subclasses

I’m unsure how one can create a table on postgresql with disjoint subclasses. I have represented (a very simplified version of) my problem below in an ER diagram, showing the two subclasses and the attributes of each subclass.

enter image description here

For the columns common to all rows (id, common1, common2), it’s clearly very simple (as shown in the code below).

However I’m not sure of the best way of considering the subclass issue. Does anyone know where to go from here?

Advertisement

Answer

Postgres table inheritance would work like so:

Consider if we have a row in each table.

They are all rows of music.

If you only want to query rows in music specify only music.

If you want to use symphony columns, you have to query symphony.

Try it


A more traditional structure would use join tables like so:

Try it

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement