Question

Customer Table - CustomerId, Street, City, State, Zipcode

ZipCode Table - ZipCodeId, ZipCode, CityId, StateId 

However, what I am confused about it is - should I put CityId, StateId and ZipCodeId or should I put CityName, StateName and ZipCode in Customer Table? And should these be set up as referencing foreign key in city, state and zipcode tables? Should I get rid of City Table altogether and just repeat city's name in ZipCode table and Customer table?

Was it helpful?

Solution

Nominally, the customer table should only contain the street address and zip code ID (not the city or state). Note that data entry for such a normalized scheme is not necessarily straight-forward; people will expect to enter city, state, zip code (or maybe just zip code) and the onus will be on the application to map that correctly and disambiguate when necessary.

I too live in a zip code used by two cities; they even happen to be in different counties, which leads to questions about which county I live in on occasion. One of the cities has multiple other zip codes; the other only has (part of) the one zip code. There's no problem: you would have two separate ZipCodeID entries for the same ZipCode, one for each city. Note that this means that there would not be a unique index on the ZipCode column in the ZipCode table.

Where would you store the +4 of the Zip+4 scheme? Good question! That belongs with the street address.

OTHER TIPS

If it is possible to have more than one city with same zip code, then a better solution that you have a City table, where you have zipcode column (just varchar or int column, no foreign key). In City table you keep foreign key from State table. At the end you should keep CityId in Customer table.

Reason for this: City and State tables are small tables, and joining them you don't make performance problem, just make index on them and everything will be fine.

Your design looks ok to me - keep full address in customer table. Just make sure that you permit to enter multiple entries into ZipCode table having the same zipcode but different cities (i.e. do not make ZipCode.ZipCode unique key).

See here:

Zip codes relate only to the mail system and have absolutely nothing to do with political boundaries. Some cities will be covered by more than one zip code; some zip codes will cover more than one city.

It is very frustrating when you call insurance company and after telling them zipcode they would give you really bad quote - simply because you seem to live in different city across the road, and that city has exactly the same zip code, but bad crime situation.

You can also incorporate USPS address correction into your application to standardize and keep this information normalized. See: https://ribbs.usps.gov/index.cfm?page=aec

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top