I’m attempting to build a database table that holds zip code data (that is then later referenced in my application).
I am grabbing the .csv file from http://www.populardata.com/zipcode_database.html which has the following data:
- zip
- latitude
- longitude
- city
- state
- county
- zip_class
What I’ve attempted so far is:
CREATE TABLE `finder_zip_codes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`zip` mediumint(128) DEFAULT '0',
`latitude` varchar(128) DEFAULT NULL,
`longitude` varchar(128) DEFAULT NULL,
`city` varchar(128) DEFAULT NULL,
`state` varchar(128) DEFAULT NULL,
`county` varchar(128) DEFAULT NULL,
`zip_class` varchar(128) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
But since I’m newer to MySQL, best-practices, etc. have snuck up on me. For example, the zip table seems to strip the beginning of the zip codes. I looked into ZEROFILL but I don’t think that’ what I want here, I basically want to use zip as an int field, but have it not auto-correct the data inside of it (or truncate it).
Any help would be greatly appreciated – I’ve been trying a few doc sites and this is what I’ve come up with so far – so any additional tips would be awesome. Thanks!
You probably don’t want to store a ZIP code as an integer field, because it’s not really an integer. It looks like one, but it wouldn’t make sense to perform integer operations on it (e.g. You would never do
12034 + 32523– it would be nonsensical).It would be better to store this as a
char(5)field, because really that’s what it is – a 5 character string, that happens to be constrained to only contain digits.EDIT
LatitudeandLongitudecan be stored as a numeric datatype (probably a decimal or double) as they actually are numbers 🙂