I'm new to database design and have designed a database that seems fine for the application but looks like it has a major red flag to me.
I have a master table which is then joined to another table but the table that is joined is based on some data in another column.
My application will in turn run queries such as:
SELECT f.dataA, m.data1, m.data2
FROM Master_Table m
INNER JOIN Foo f ON f.id = m.TableKey
WHERE TableName = 'Foo'
and
SELECT fb.dataA, fb.dataB, m.data1, m.data2
FROM Master_Table m
INNER JOIN Foobar fb ON fb.id = m.TableKey
WHERE TableName = 'Foobar'
This works as desired but I don't like the fact that my Master_Table.TableKey
should probably be held to a foreign key constraint but this is impossible with this design (or at least I think so).
Should the TableKey
column be split out to being a FooKey
, BarKey
and FoobarKey
? This also then seems odd because I then guarantee that BarKey
and FoobarKey
will be null where TableName
= "Foo".
Or is there a completely different way I should be approaching this?
EDIT: Funnily enough this recent meta.dba post seems to touch on this idea about inheritance but I'm not too sure what the best approach still is.