全世界大概有
260 country/regions
5000 states
many many cities
设计因您的需要而异。
1 - 用于小型存储:
country(id,country)
state(id,state,country_id)
city(id,city,state_id)
2 - 用于快速查询:
city(id,city,state,country)
3 - 出于中道的目的:
country(code,country)
state(code,country) -- you might merge country and state into one table based on code
city(state_code,city)
您可能有兴趣查看 iso 代码:
https://en.wikipedia.org/wiki/ISO_3166-1 例如美国
https://en.wikipedia.org/wiki/ISO_3166-2 例如美国-纽约
因此 iso 州代码包含 iso 国家代码。
根据您提供的更多信息进行更新:
如果您正在为美国设计房地产网站。
1 - 您不需要国家/地区表,很可能所有属性都在美国境内
2 - 美国境内的州少于 60 个,因此您可以使用枚举来保存州。几乎所有人都会理解 NY = New York,因此您不需要状态表。
3 - 所以你需要一张城市表。因为您将使用 city_id 记录超过 10,000,000 条财产记录。
usa_cities(
id int PK
state enum('NY', 'LA', ...)
city varchar
...
)
properties(
id int PK
city_id int,
....
)
AS 属性表通常很大,您可能会跳过状态表,并进行反规范化设计以加快对较少连接的查询:
properties (
id int PK,
state enum('NY', 'LA',...)
city varchar
...
)
你也可以使用 enum 作为城市,我不知道美国有多少个城市,但我一开始并不鼓励这样做。