自引用表上的复合键

发布于 2024-08-03 04:43:35 字数 794 浏览 4 评论 0原文

我们有一个用于下面定义的站点表的复合主键。从功能上来说,这完全符合我们的预期。每个站点都应该有一个同一区的父站点。以这种方式定义表可以专门实现这一点。

    CREATE TABLE [dbo].[site](
        [site_number] [nvarchar](50) NOT NULL,
        [district_id] [bigint] NOT NULL,
        [partner_site_number] [nvarchar](50) NULL,
     CONSTRAINT [PK_site] PRIMARY KEY CLUSTERED 
    (
        [site_number] ASC,
        [district_id] ASC
    )

    ALTER TABLE [dbo].[site]  WITH CHECK ADD  CONSTRAINT [FK_site_site] FOREIGN KEY([partner_site_number], [district_id])

我的具体问题是关于复合 PK 上定义的自引用 FK。我听过一些关于这个特定设计的意见,它们往往是相互矛盾的。有些人特别喜欢它,因为它在复合键的一般理解范围内发挥了应有的作用。其他人坚持认为这在理论上是不正确的,并且 FK 中还应该包含一个 [partner_district_id] 字段,而不是 [district_id]。此设计需要验证以强制 [district_id] = [partner_district_id],这可以通过检查约束或应用程序级逻辑来完成。

对这些解决方案或任何其他解决方案的进一步意见将不胜感激。

We have a composite primary key for the site table defined below. Functionally, this does exactly as we would like it to. Each site should have a parent site of the same district. Defining the table in this way allows specifically for that.

    CREATE TABLE [dbo].[site](
        [site_number] [nvarchar](50) NOT NULL,
        [district_id] [bigint] NOT NULL,
        [partner_site_number] [nvarchar](50) NULL,
     CONSTRAINT [PK_site] PRIMARY KEY CLUSTERED 
    (
        [site_number] ASC,
        [district_id] ASC
    )

    ALTER TABLE [dbo].[site]  WITH CHECK ADD  CONSTRAINT [FK_site_site] FOREIGN KEY([partner_site_number], [district_id])

My specific question is regarding the self-referencing FK defined on a composite PK. I've heard a few opinions on this particular design and they tend to be conflicting. Some like it particularly because it functions as it should within a general understanding of composite keys. Others insist that it is theoretically incorrect and that there should also be a [partner_district_id] field that is included in the FK instead of [district_id]. This design would require validation to enforce that the [district_id] = [partner_district_id], which could be done either with a check constraint or application level logic.

Further opinions on these solutions or any others would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

蓝眼睛不忧郁 2024-08-10 04:43:35

我建议将 SiteId 单独作为主键。 DistrictId 可能应该是外键?

编辑 - 在这种情况下,我建议将额外的 PartnerDistrictId 添加到外键中;您永远不知道,您以后可能想将一个站点与不同地区的另一个站点合作。但就我个人而言,我赞成在这里使用代理键。在大多数情况下;)

I'd suggest SiteId on it's own be the primary key. DistrictId should probably be a foreign key?

EDIT - in that case, i'd suggest adding the additional PartnerDistrictId to the foreign key; you never know, you may later want to partner one site with another in a different district. But personally, I'd be in favour of a surrogate key here. And in most cases ;)

挽清梦 2024-08-10 04:43:35

命名评论... Site_Id 本身不是唯一的吗?因为名称 Site_id 意味着 iut 是。如果它仅与 District_Id 组合时唯一,那么它可能命名错误...如果它是 site_Sequence、District_site_No 或其他更清晰的内容,可能会更清晰。

如果我理解您的域模型,那么一个地区中的所有站点都“派生”自同一根父站点,并且不同地区的站点之间不能有重叠...如果是这样,则可以通过使 DistrictID 可为空来实现相同的功能,并且只为根站点填充它。那么Site_Id可以是单个字段PK,而ParentSiteId可以是单个字段FK。所有“子”站点都“属于”其根父站点记录中指定的地区。

naming comment... Is the Site_Id by itself not unique? Cause the name Site_id implies that iut is. If it is only unique in combination with District_Id, then it is perhaps misnamed... it might be clearer if it were site_Sequence, or District_site_No, or something else clearer.

If I understand your domain model, then all sites in a district 'derive' from the same root parent site, and there can be no overlap between sitres in different districts... if so then the same functionality could be achioeved by making DistrictID nullable, and only populate it for the root sites. Then the Site_Id could be a single field PK, and ParentSiteId could be a single Field FK. All 'child' sites would 'belong' to the district designated in their root parent Site record.

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