Search code examples
mysqlnormalization

What is the best way to set this data in the database?


I have a table of cites and zip codes. As it happens one city can have multiple zip codes so how should i set the db that i may be able to pull the correct zips when i enter a city name or get the correct city when i enter the zip.

Should this be one table with all the data, or three where one has cities, one has zips and third has the proper associations?


Solution

  • I would use only one table with zip codes as primary key and an index on the cities (to speed up searching). The three-table solution may me useful if you have a many-to-many relationship (one zip code can have multiple cities), but otherwise I think it's only unnecessary complexity.