表关系算法
我遇到一种情况:
两张表示
country
-countryID (PrimaryKey)
-countryName
-relativeToSubCountry (Foreign Key -> subCountry.subCountryID)
subCountry
-subCountryID (PrimaryKey)
-subCountryName
例:
德国与多个次国家(历史时期或独立国家)相关。
Germany -> Bavaria -> Saxonia -> ... -> Bismark -> Hitler -> BRD -> DDR
问题是我如何实现此表关系?因为 country.relativeToSubCountry
不能有超过 1 个 id。
先感谢您。
I've got a situation:
Two tables
country
-countryID (PrimaryKey)
-countryName
-relativeToSubCountry (Foreign Key -> subCountry.subCountryID)
subCountry
-subCountryID (PrimaryKey)
-subCountryName
Example:
Germany is relative to more than one subcountry (period in history or independent state)
Germany -> Bavaria -> Saxonia -> ... -> Bismark -> Hitler -> BRD -> DDR
The question is how can I implement this table relations? Because country.relativeToSubCountry
cannot to have more than 1 id.
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果每个
subCountry
可能只属于一个country
,那么您需要做的就是移动外键:现在每个
subCountry
存储其通过选择“relativeToCountry”可以找到“parent”以及country
cx 的所有subCountry
行。如果关系更复杂(从历史上看,由于边界变化,我认为可能是这样),那么您再次需要不同的结构,例如
现在每个
country
可能有许多subCountry
记录,反之亦然。此外,关于每个特定关系的信息(例如,关系存在的时间段)可以存储在新表中。If each
subCountry
may only belong to a singlecountry
, then all you should need to do is move the foreign key:Now each
subCountry
stores its "parent" and allsubCountry
rows for acountry
cxan be found by selecting for 'relativeToCountry`.If the relationship is more complex (and historically I think it probably is, due to border changes) then you need a different structure again, something like
Now each
country
may have manysubCountry
records and vice versa. Further, information about each specific relationship (time period over which the relationship existed, for example) cxan be stored in the new table.