I’m working on a project where the database has a few tables that contain a type_id field that stores id’s from multiple tables
for instance:
id | table_type | table id
==============================
1 ADDRESS 1
2 ADDRESS 2
3 CITY 1
4 CITY 2
4 ADDRESS 3
5 COUNTRY 1
the table_id field holds either an id from the Addresses table, or the Cities table, or the Countries table
I’m just wondering if this is good design. or should i avoid this whenever possible?
This table is used to grab all locations that a user has entered.
The answer is:
It Depends.
If the example table you gave was named
Locationand you’re using it to achieve type inheritance, whereAddress,City, andCountryare specific types ofLocation, then this design can work. In this case, your primary key will be in theLocationtable, and each of the other tables will have a foreign key toLocation. If that’s not how you’re using it, then this is not a properly normalized database design.