数据库设计:根实体是链接到两个或多个相关实体的好方法吗?

发布于 2024-11-03 07:20:59 字数 506 浏览 0 评论 0原文

我的应用程序(基于关系数据库)必须处理两种客户实体:(自然)人和组织。它们具有不同的属性,因此每个组都生活在自己的表中。

现在我有其他数据对象,例如地址、标签或备忘录。每条记录都属于一个人或一个组织。

我想知道如何表示这种联系。当链接到一个人或一个组织时,我不希望目标表中有另一个字段。

我正在考虑在个人和组织之上创建一种超级实体。我在 Highrise 或 Capsule CRM 等 CRM 应用程序中看到过这种情况 - 他们称之为“派对”。每个人和每个组织在政党表中都有相应的条目。其他表格现在链接到政党表格,而不是直接链接到个人或组织。

参与方:

  id | person_id | org_id
   1 |         1 |
   2 |           |      1

在上面的示例中,参与方记录 #1 链接到个人 #1,参与方记录 #2 链接到组织 #1。

这真的是一个可行的方法还是我只是有点盲目寻求更简单的解决方案?

My application - based on a relational database - has to deal with two kinds of customer entities, (natural) persons and organizations. They have different attributes and therefore each group lives in its own table.

Now I have other data objects like addresses or tags or memos. Each record belongs to exactly either a person or an organization.

I wonder how to represent that connection. When linking to a person or an organization I don't want to have another field with the target table.

I was thinking about creating a kind of super-entity on top of persons and organizations. I've seen this in CRM apps like Highrise or Capsule CRM - they have called it "parties". Every person and every organization has a corresponding entry in the party table. Other tables now link to the party table instead of directly to persons or organizations.

Party:

  id | person_id | org_id
   1 |         1 |
   2 |           |      1

In the above example the party record #1 links to person #1 and party #2 links to organization #1.

Is this really a feasible way or am I just a bit to blind for a simpler solution?

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

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

发布评论

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

评论(3

月隐月明月朦胧 2024-11-10 07:20:59

我已经多次回答过类似的问题了。我提供的两个最丰富的答案是 此处此处。这两个答案在不同的上下文中都使用相似的结构;我认为第二个评论特别有帮助。

I've answered similar questions several times on SO. The two most informative answers from me are here and here. Both those answers use similar structures in different contexts; the comments for the second one are particularly helpful, I think.

欲拥i 2024-11-10 07:20:59

我会使用“超级”表的方法,但要扭转参考。 personorganizations 将引用 party 表,而不是相反。

如果 DBMS 不直接支持它(例如 PostgreSQL),那么这是在关系数据库中建模“继承”的常用设计模式。

 party (id, customer_number, [other common attributes] )
 person (person_id, party_id, ... )
 organizations (org_id, party_id, ... )
 address (address_id, party_id, ...) 

为了方便起见,您可以创建加入团体/个人和团体/组织的视图,以便您可以轻松访问基本属性与专用类型一起。

I'd use the approach with the "super" table but turn the reference around. person and organizations will reference the party table, not the other way round.

That is the usual design pattern to model "inheritance" in a relational database if the DBMS doesn't support it directly (like e.g. PostgreSQL)

 party (id, customer_number, [other common attributes] )
 person (person_id, party_id, ... )
 organizations (org_id, party_id, ... )
 address (address_id, party_id, ...) 

For convenience you can create views that join party/person and party/organizations so that you can easily access the base attributes together with the specialized type.

简单气质女生网名 2024-11-10 07:20:59

拥有像“Party”这样的元表是一种选择,尽管我会让解决方案更具可扩展性。怎么样?

PartyType
- id
- name

Party
- id
- partyTypeId
- typeSpecificId

在 PartyType 表中,您可以定义“人员”和“组织”类型。然后,您创建的每个聚会都会有一个与之关联的 PartyType。这样,您将来可以添加更多类型,并且您的表会更加密集,而不会出现太多空列。

Having a meta table like "Party" is one option, although I would make the solution a little more extensible. How about this

PartyType
- id
- name

Party
- id
- partyTypeId
- typeSpecificId

In the PartyType table you would define a "Person" and an "Organization" type. Then each party you create would have a PartyType associated with it. This way you could add more types in the future and your tables are denser, without as many null columns.

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