表关系算法

发布于 2024-12-11 05:31:58 字数 496 浏览 0 评论 0原文

我遇到一种情况:

两张表示

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玉环 2024-12-18 05:31:58

如果每个 subCountry 可能只属于一个 country,那么您需要做的就是移动外键:

country
    -countryID (PrimaryKey)
    -countryName

subCountry
    -subCountryID (PrimaryKey)
    -relativeToCountry (Foreign Key -> country.countryID)
    -subCountryName

现在每个 subCountry 存储其通过选择“relativeToCountry”可以找到“parent”以及 country cx 的所有 subCountry 行。

如果关系更复杂(从历史上看,由于边界变化,我认为可能是这样),那么您再次需要不同的结构,例如

country
    -countryID (PrimaryKey)
    -countryName

subCountry
    -subCountryID (PrimaryKey)
    -subCountryName

subCountryStructure
    -relativeToCountry (Foreign Key -> country.countryID)
    -relativeToSubCountry (Foreign Key -> subCountry.subCountryID)

现在每个country可能有许多subCountry 记录,反之亦然。此外,关于每个特定关系的信息(例如,关系存在的时间段)可以存储在新表中。

If each subCountry may only belong to a single country, then all you should need to do is move the foreign key:

country
    -countryID (PrimaryKey)
    -countryName

subCountry
    -subCountryID (PrimaryKey)
    -relativeToCountry (Foreign Key -> country.countryID)
    -subCountryName

Now each subCountry stores its "parent" and all subCountry rows for a country 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

country
    -countryID (PrimaryKey)
    -countryName

subCountry
    -subCountryID (PrimaryKey)
    -subCountryName

subCountryStructure
    -relativeToCountry (Foreign Key -> country.countryID)
    -relativeToSubCountry (Foreign Key -> subCountry.subCountryID)

Now each country may have many subCountry 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文