数据库设计 - 多个对象类型显示分层?

发布于 2024-12-05 14:35:16 字数 851 浏览 0 评论 0原文

几天来我一直在努力寻找解决方案,但我找不到任何“最佳实践”或关于如何实现我想要的目标的良好解释。

假设我有一个由下表组成的数据库(仅作为示例);

Customers (Fields: Id, CustomerName, Location)

Products (Fields: Id, ProductName, ProductCode)

Groups (Fields: Id, GroupName)

然后我需要将它们链接在一起以在树视图中显示。例如;

Customer1
    |
    |-Group1
    |    |-Product1
    |    |-Product2
    |
    |-Group2
         |-Product2
         |-Product3
         |-Group3
              |
              |-Product1
              |-Product4

正如我所说,这只是一个例子。真正的解决方案包括其他类型。

由于产品可能出现在多个地方,我需要创建一个“链接表” 显示层次结构数据。

所以我创建了另一个看起来像这样的表;

Id (int)
ParentId (int)
ObjectType (int)
GroupId (int)
ProductId (int)
CustomerId (int)

ObjectType 字段的原因是为了知道我需要在哪个数据库中检查要在树视图中显示的项目名称等。

我现在的问题是:还有其他方法来设计这个数据库吗? 我正在使用 LINQ 等在 C# 中进行开发。

I have been struggling and searching for a solution about this for a couple of days but i cannot find any "best practices" or good explanations of how to achive what i want.

Lets say that I have a database consisting of the following tables (just an example);

Customers (Fields: Id, CustomerName, Location)

Products (Fields: Id, ProductName, ProductCode)

Groups (Fields: Id, GroupName)

I then need to link these together to be shown in a Treeview. For example;

Customer1
    |
    |-Group1
    |    |-Product1
    |    |-Product2
    |
    |-Group2
         |-Product2
         |-Product3
         |-Group3
              |
              |-Product1
              |-Product4

As i said, this is just an example. The real solution consists of other types.

Since the products can occur in several places i need to create a "link table"
to display the hierarchial data.

So i created another table looking like this;

Id (int)
ParentId (int)
ObjectType (int)
GroupId (int)
ProductId (int)
CustomerId (int)

The reason for the ObjectType field is to know in what database i need to check for the items name etc. to display in the treeview.

My question now: Is there any other way to design this database?
I am developing in C# using LINQ etc.

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

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

发布评论

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

评论(1

一个人的旅程 2024-12-12 14:35:16

从您的示例来看,树的每个级别都应该是一个新的链接表。

如果组 1 为多个客户重复,您不会显示组 1 的内容是否也应该重复。?但我假设无论关联哪些客户,group1 的内容都是相同的。

如果你真的可以将任何东西链接到任何东西,那么 objectType 就是正确的选择......但是你会得到类似的东西:

parentId
ParentObjectType
childId
childObjectType

from your example, each level of the tree should be a new link table.

you do not show if group 1 is repeated for more than one customer, should the contents of group1 also be repeated.? but i assume group1 contents are the same no matter which customers are associated.

if you can truly link anything to anything, then the objectType is the way to go... but you would have something like:

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