Reputation: 6862
I am building a site where there will be a bunch of companies and their business listing, so they will have their Address, City, State, Zip Code, etc.
I found this website here: http://jesseprice.com/mysql-city-state-zip-latitude-longitude-database/ which has a database of all city, state, zip, county, lat, long.
I imported the database above, so my question is how would I connect this to my companies table? And when a company signs up, I guess I could make a drop down for the state, and then it will drop down to the cities found in that state pulling from the database so they choose from the those, and then the zip code. Would that work? So they wouldn't have to enter it in themselves?
I'm just a little confused on how to do this properly and all make it work together.
Thank you.
Upvotes: 0
Views: 10254
Reputation: 81
You need have following table design (companies/cities) -
CREATE TABLE `companies` (
`companyID` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`companyName` varchar(150) NOT NULL DEFAULT '',
`address` varchar(300) NOT NULL DEFAULT '',
`cityID` smallint(5) NOT NULL,
`countryID` varchar(3) NOT NULL,
PRIMARY KEY (`companyID`),
KEY `cityID` (`cityID`),
KEY `countryID` (`countryID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `countries` (
`countryID` varchar(3) NOT NULL DEFAULT '',
`countryName` varchar(52) NOT NULL DEFAULT '',
`localName` varchar(45) NOT NULL,
`webCode` varchar(2) NOT NULL,
`region` varchar(26) NOT NULL,
`continent` enum('Asia','Europe','North America','Africa',
'Oceania','Antarctica','South America') NOT NULL,
PRIMARY KEY (`countryID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `cities` (
`cityID` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`cityName` varchar(50) NOT NULL,
`stateID` smallint(5) unsigned NOT NULL DEFAULT '0',
`countryID` varchar(3) NOT NULL DEFAULT '',
PRIMARY KEY (`cityID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
You can download full list of world cities / countries in mysql format from here -
http://myip.ms/info/cities_sql_database/World_Cities_SQL_Mysql_Database.html
.
Upvotes: 1
Reputation: 3537
Option 1:
Add ALL fields to your address table, referencing the selected table on additions, placing city, state, zip, county, lat and long into your table.
Advantages:
Option 2:
Add a zip field only, and for all lookups, pull the data from the downloaded table.
Advantages:
I would actually recommend adding the fields into your managed table, and keep the downloaded table as a read-only, replaceable reference table.
Upvotes: 1