I have a very basic question regarding the design of a database. I thought I knew the answer, but after seeing an online tutorial on a triple dropdown menu and the design of the demo database, I’m not so sure anymore.
Ok here’s the setup, there are three tables involved.
First table: tblcountry
Fields: country_id
country
Second table: tblstate
Fields: state_id
country_id
state
Third table: tblcity
Fields: cidy_id
state_id
city
Now my question is: In table three, is it necessary to include the countryid as well? By using SQL you can query which country the city is in….or is it good design to have a reference to the country in the city table??
I hope someone can help 🙂
Normalization starts with data. In what follows, take the word state loosely. I’m talking about data analysis and normalization, not about modeling political hierarchies.
It means “City [city_name] is in state [state_name] of country [country_name].” (What a table means is called its predicate.)
This table effectively identifies the “full name” of a city as {country_name, state_name, city_name}. At the conceptual level, it’s not very different from using {last_name, middle_name, first_name} to identify a person. The difference is that, although two people often do share the same full name, two cities don’t.
It’s clear there’s only one candidate key: {country_name, state_name, city_name}. This table is in 5NF. Substituting ID numbers for text can’t change that.
Now, what’s the first thing you think after you’ve normalized a table to 5NF, and that table turns out to be “all key”? Is it, “Ok, now I need to split the key into more tables?”
I hope not.
To increase data integrity, you can project sensible subsets of the columns, and set foreign key references. Projection, used in this way, has absolutely no effect on the normal form of the original table. If it were in 2NF before this kind of projection, it will be in 2NF after this kind of projection. These tables have slightly different predicates from the original. (Duh.)
To select countries for populating a drop-down list, you can just
The user having chosen a country, selecting states is simple.
And choosing city names is similar.