I am looking for a best practice for multiple tables in a vertical hierarchy having small shared data. Let’s say three tables:
Country {
id
}
State {
id,
country_id,
FK_to_country_id,
}
City {
id,
state_id,
FK_to_state_id,
}
in above state belongs to country while city belongs to state and turns out to belong to country as well. The schema looks clean but when you want to look up which country a city belongs to, you have to use a JOIN with three tables. If there is another tier called County which belongs to city, the situation gets worse.
City {
id,
country_id,
state_id,
FK_to_country_id,
FK_to_state_id,
}
adding another column ‘country_id’ in City frees us from cumbersome JOIN but the database schema gets a little duplicate.
What’s practice in real world?
That’s what
JOINs are made for; don’t deprive them of their duty.In real-world practice, it is absolutely normal to be joining multiple tables like that.
In your example, yes it would be redundant to have country ID’s in the city table.
But it’s not always the case. Consider an example where a city can exist without needing to be in a state (a lot of 3rd-world countries have no “state” designation), then you have to have that foreign-key country ID, because the
Statetable is optional.