层次结构中的可选关系

发布于 2024-10-17 08:21:21 字数 235 浏览 0 评论 0原文

我有一个层次结构区域 ->区域->其中区域是可选的扇区,即特定扇区可能属于区域,但用户可能不希望在区域级别分解。此外,扇区与区域不同,它们具有不同的属性,并且区域实际上是扇区的集合,

扇区还具有在层次结构中进一步相关的其他实体。

我想知道在数据库中对此进行建模的最佳方法是什么?我之前在类似的场景中做过此操作,并使用虚拟区域实体来允许存储扇区。

有人有更好的想法吗?

谢谢, 标记

I have a hierarchy Area -> Zone -> Sector where Zone is optional i.e. a particular Sector may belong to an Area but the user may not want to break down at the Zone level. Additionally Sectors are not the same as Zones, they have different properties and Zones are effectively a collection of Sectors

Sectors have other entities related further down the hierarchy as well.

What I'd like to know is what's the best approach to modelling this in a database? I've done this before in a similar scenario and used a dummy Zone entity to then allow storage of the Sectors.

Anybody got any better ideas?

Thanks,
Mark

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

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

发布评论

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

评论(2

魂牵梦绕锁你心扉 2024-10-24 08:21:21

如果区域和区域在您的应用程序中实际上足够相似,您可能需要考虑对两者使用相同的表(并使用标志列来区分给定行代表的类型)。在这种情况下,任何区域/区域都可以有一个父区域/区域,并且每个扇区都必须有一个父区域/区域。

例如:

Table Container:
    Field container_id
    Field container_type
    Field parent_container_id (NULL)
Table Sector:
    Field parent_container_id

If Areas and Zones are effectively similar enough in your application, you might want to consider using the same table for both (and use a flag column to distinguish which type a given row represents). In this case, any Area/Zone could have a parent Area/Zone, and every Sector must have a parent Area/Zone.

For instance:

Table Container:
    Field container_id
    Field container_type
    Field parent_container_id (NULL)
Table Sector:
    Field parent_container_id
屌丝范 2024-10-24 08:21:21

你不能将其建模为 IS-A 关系吗?区域始终是一个区域,扇区始终是一个区域。

区域和扇区之间存在可选关系。

create  table dbo.area(
   area_id   numeric(8,0)  identity,
   parent_id   numeric(8,0)  not null, --whatever the parent table of area table is
   flag1   bit  not null,
   prop1 varchar(100) not null, -- many more props...
   constraint pk_0 primary key clustered ( area_id )
)

create  table dbo.zone(
   area_id   numeric(8,0)  not null,
   prop_z1   varchar(50)  null,

    constraint pk_1 primary key clustered ( area_id )
)

alter table dbo.zone
    add constraint fk_1 foreign key ( area_id )
    references dbo.area ( area_id )

-- same for sector

create  table dbo.sector(
   area_id   numeric(8,0)  not null,
   prop_s2   varchar(50)  null,

    constraint pk_2 primary key clustered ( area_id )
)

alter table dbo.sector
    add constraint fk_s1 foreign key ( area_id )
    references dbo.area ( area_id )

这是一个奇怪的设计,但嘿,你已经要求任何替代方案。

您可以在扇区表中添加一个或多个 zone_id 列,如果您想让自己变得非常困难,请向这些列添加外键约束。

can't you model it as an IS-A relationship? A zone is always an area and a sector is always an area.

There is an optional relationship between area and sector.

create  table dbo.area(
   area_id   numeric(8,0)  identity,
   parent_id   numeric(8,0)  not null, --whatever the parent table of area table is
   flag1   bit  not null,
   prop1 varchar(100) not null, -- many more props...
   constraint pk_0 primary key clustered ( area_id )
)

create  table dbo.zone(
   area_id   numeric(8,0)  not null,
   prop_z1   varchar(50)  null,

    constraint pk_1 primary key clustered ( area_id )
)

alter table dbo.zone
    add constraint fk_1 foreign key ( area_id )
    references dbo.area ( area_id )

-- same for sector

create  table dbo.sector(
   area_id   numeric(8,0)  not null,
   prop_s2   varchar(50)  null,

    constraint pk_2 primary key clustered ( area_id )
)

alter table dbo.sector
    add constraint fk_s1 foreign key ( area_id )
    references dbo.area ( area_id )

Its an odd design but hey you've asked for any alternative.

You can add one or more zone_id columns in the sector table, and if you want to make it really hard for yourself, add foreign key constraints to those.

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