数据库:将一种类型的记录与多个其他表相关联

发布于 2024-10-19 09:38:39 字数 990 浏览 0 评论 0原文

将一种类型的对象与多种类型的其他对象之一相关联的最佳方法是什么?

例如,一个简化的场景:

tblNews (_newsId_, _title_) 
1, Hello world
2, Lorem ipsum

tblPeople (_personId_, _personName_) 
1, John Smith
2, Joe Bloggs

tblPlaces (_placeId_, _placeName_)
1, Townville
2, Smallplace

定义 tblNews 中的项目与人员和地点之间关系的最佳方法是什么(例如新闻项目 newsId=1(Hello world)可能与人员 personId=2(Joe Bloggs)相关,并且地点 PlaceId=1(汤维尔))?我考虑过使用两个表,如下所示:

tblObjectTypes (_typeName_, _typeTable_, _typePK_)
person, tblPeople, personId
place, tblPlaces, placeId

tblNewsRelationships (_relationshipId_, _newsId_, _objectType_, _objectKey_)
1, 1, person, 2
2, 1, place, 1

这里 tblNewsRelationship 定义了关系中的新闻项、它涉及的对象类型以及该对象的主键。

tblObjectTypes 包含新闻项可能涉及的每种类型的对象、该类型的对象存储在哪个表中以及应使用哪个字段来查找它们。代码将转到 tblObjectTypes,查找关系中定义的 objectType,并使用给定的 typeTable 和 typePK 来查找给定其 objectKey 的正确记录。

然而,这似乎非常复杂,因为这似乎应该是一个常见问题。我搜索了又搜索,但是如果不知道描述这种关系的术语,就不可能找到任何有用的东西,所以我不知道是否有“正确”的方法来创建这种关系。

抱歉问这么长的问题。

What is the best way to relate one type of object to one of multiple types of other objects?

For example, a simplified scenario:

tblNews (_newsId_, _title_) 
1, Hello world
2, Lorem ipsum

tblPeople (_personId_, _personName_) 
1, John Smith
2, Joe Bloggs

tblPlaces (_placeId_, _placeName_)
1, Townville
2, Smallplace

What would be the best method for defining relationships between the items in tblNews and both people and places (e.g. news item newsId=1 (Hello world) could be related to person personId=2 (Joe Bloggs) and place PlaceId=1 (Townville))? I have considered using two tables as in:

tblObjectTypes (_typeName_, _typeTable_, _typePK_)
person, tblPeople, personId
place, tblPlaces, placeId

tblNewsRelationships (_relationshipId_, _newsId_, _objectType_, _objectKey_)
1, 1, person, 2
2, 1, place, 1

Here tblNewsRelationship defines which news item is in the relationship, which type of object it relates to and the primary key of that object.

tblObjectTypes contains each type of object that the news item could relate to, which table that kind of object is stored in and which field should be used to look them up. The code would go to tblObjectTypes, look up the objectType defined in the relationship and use the typeTable and typePK given to find the correct record given its objectKey.

However, this seems awfully convoluted for what seems like it should be a common problem. I've searched and searched, but without knowing the terminology describing this type of relationship it's impossible to find anything useful so I have no idea if there's a 'correct' way of creating this sort of relationship.

Sorry for asking such a long question.

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

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

发布评论

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

评论(1

行雁书 2024-10-26 09:38:39

一条新闻可以与一个人和一个地方相关,对吗?因此新闻表应该有一个名为 person_id 的列和一个名为 place_id 的列。每个新闻行都会有相应的人或地点的相应 ID 号。

“外键”是您想要的术语。

A news item can be related to a person and a place, right? So the news table should have a column called person_id and a column called place_id. Each news row will have the appropriate id number of the corresponding person or place.

"Foreign key" is the term you want.

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