Kev*_*Lee 8 mysql database normalization
我需要帮助设计我的国家,城市,州表.我将从我的表中提供样本数据,以便您可以更好地解决我的问题.
这是我的国家表:
Country
______
code name
US United States
SG Singapore
GB United Kingdom
Run Code Online (Sandbox Code Playgroud)
这是我的城市表:
City
_____
id country city state
1 US Birmingham Alabama
2 US Auburn Alabama
.
.
29 GB Cambridge NULL
30 GB Devon NULL
Run Code Online (Sandbox Code Playgroud)
我的问题是,唯一拥有州领域的国家是美国.所有其他城市都有空值.
我的临时解决方案是为美国创建一个特殊的城市表,然后所有其他国家都有另一个没有州字段的城市表.
我认为这只会使问题复杂化,因为我有两个城市表.
我该如何改进这个设计?
为什么不去关系?
Country ( CountryID, CountryCode, CountryName )
Region ( RegionID, RegionCode, RegionName, CountryID )
City ( CityID, CityCode, CityName, RegionID )
Run Code Online (Sandbox Code Playgroud)
'Region'这个名称比State更通用,这意味着它可能在任何地方都更有意义.
为什么不是标准的 3 路链接表集?
table country (
id int primary key,
name varchar(255)
);
table state (
id int primary key,
name varchar(255),
country_id int foreign key country (id)
);
table city (
id int primary key,
name varchar(255)
state_id int foreign key state (id)
);
Run Code Online (Sandbox Code Playgroud)
这将适用于大多数情况,除了像劳埃德明斯特、萨斯喀彻温省这样跨越两个省界的少数情况。