tags:

views:

34

answers:

2

how do i organize country data (countries, states and cities etc) in mysql?

cause every country has 3 tables: countries, states and cities.

should i have each country in separate set of tables or should i have them all in these 3 tables? if i have all of them in same tables, im afraid that the amount of rows will be huge cause i tend to have a lot of countries!

what is best practice for this?

+1  A: 
countryTable(countryId(PK),countryName)

stateTable(stateId(PK),stateName,belongCountryId(FK))

cityTable(cityId(PK),cityTable,belongStateId(FK))

you can use "Cascade query" or Create "View" to get information from these 3 tables.

oyishi
so you mean all countries in these 3 tables with the columns you have in the parentheses? wont it get slower with queries if you got a lot of rows it has to search through? eg to get one city it has to parse through rows of ALL countries instead of only one?
never_had_a_name
you can put all countries, states and cities on the earth into your database by using these 3 tables.
oyishi
if you "got a lot of rows it has to search ",you can Create "View" to increase the query efficiency
oyishi
ok thanks. then i know its best practice=)
never_had_a_name
+2  A: 

Here's one way:

country:
country_id (INT) | country_name (VARCHAR)

state:
state_id (INT) | country_id (INT) | state_name (VARCHAR)

city:
city_id (INT) | country_id (INT) | state_id (INT) | city_name (VARCHAR)
Tom