My application will need to reference addresses. Street info will be stored with my main objects but the rest needs to be stored seperately to reduce redundancy. How should I store/retrieve ZIPs, cities and states? Here are some of my ideas.
single table solution (cant do relationships)
[locations]
locationID
locationParent (FK for locationID – 0 for state entries)
locationName (city, state)
locationZIP
two tables (with relationships, FK constraints, ref integrity)
[state]
stateID
stateName
[city]
cityID
stateID (FK for state.stateID)
cityName
zipCode
three tables
[state]
stateID
stateName
[city]
cityID
stateID (FK for state.stateID)
cityName
[zip]
zipID
cityID (FK for city.cityID)
zipName
Then I read into ZIP codes amd how they are assigned. They aren’t specifically related to cities. Some cities have more than one ZIP (ok will still work) but some ZIPs are in more than one city (oh snap) and some other ZIPs (very few) are in more than one state! Also some ZIPs are not even in the same state as the address they belong to at all. Seems ZIPs are made for carrier route identification and some remote places are best served by post offices in neighboring cities or states.
Does anybody know of a good (not perfect) solution that takes this into consideration to minimize discrepencies as the database grows?
I don’t know if you’re internationalizing your app, but the general construct is this, with a one-to-many relationship with the following item:
Country
Region (state/province)
City
That’s usually sufficient to be able to filter your data in a meaningful way. Trust me on this: you don’t want to get into the technicalities of geographic land division.
For an address, store the data above plus street address, postal code (international version of zip code), etc. down to the resolution you need. I say resolution because you could split the address field into things like apartment number, street number, street name, street direction, etc. — but that data may be dependent on the location, so I would avoid doing that if you are going to internationalize your app. Just a street address field is sufficient 99.99% of the time.