数据库设计:根实体是链接到两个或多个相关实体的好方法吗?
我的应用程序(基于关系数据库)必须处理两种客户实体:(自然)人和组织。它们具有不同的属性,因此每个组都生活在自己的表中。
现在我有其他数据对象,例如地址、标签或备忘录。每条记录都属于一个人或一个组织。
我想知道如何表示这种联系。当链接到一个人或一个组织时,我不希望目标表中有另一个字段。
我正在考虑在个人和组织之上创建一种超级实体。我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已经多次回答过类似的问题了。我提供的两个最丰富的答案是 此处和此处。这两个答案在不同的上下文中都使用相似的结构;我认为第二个评论特别有帮助。
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.
我会使用“超级”表的方法,但要扭转参考。
person
和organizations
将引用party
表,而不是相反。如果 DBMS 不直接支持它(例如 PostgreSQL),那么这是在关系数据库中建模“继承”的常用设计模式。
为了方便起见,您可以创建加入团体/个人和团体/组织的视图,以便您可以轻松访问基本属性与专用类型一起。
I'd use the approach with the "super" table but turn the reference around.
person
andorganizations
will reference theparty
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)
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.
拥有像“Party”这样的元表是一种选择,尽管我会让解决方案更具可扩展性。怎么样?
在 PartyType 表中,您可以定义“人员”和“组织”类型。然后,您创建的每个聚会都会有一个与之关联的 PartyType。这样,您将来可以添加更多类型,并且您的表会更加密集,而不会出现太多空列。
Having a meta table like "Party" is one option, although I would make the solution a little more extensible. How about this
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.