存储标签或类别(如 Stack Overflow)的最佳方式

发布于 2024-12-14 04:29:06 字数 304 浏览 2 评论 0原文

我有一个问题网络应用程序,允许用户就特定事物提出问题。我正在为每个问题开发一个标签样式标签,就像堆栈溢出一样。当我将每个帖子与类别或标签相关联时,我想知道存储它的最佳方式是什么。我认为这是一种理想的方法:

- 在 sql 数据库中创建一个列来存储序列化的 json 对象,或者只是一个逗号分隔的行,其中包含问题所属类别的 ID。

我觉得使用这种方法很难根据特定标签搜索问题,因为它必须解析每个问题的类别列,这似乎并不理想。有什么好方法来构建此功能以很好地适应搜索,同时又保持模式相当简单(请记住,我的主要目标是使标签尽可能类似于 stackoverflow)?

I have a question web application which allows users to ask questions on specific things. I am developing a tag style labeling for each question just like stack overflow. When I associate each post with categories or tags, I was wondering what is the best way to store this. Here is what I would guess would be an ideal way to do this:

-Create a column in the sql database to store either a serialized json object or simply just a comma separated line with the IDs of the categories that the question pertains to.

I feel with this approach it will be hard to search questions according to a specific tag because it will have to parse the categories column for each question which seems like it would not be ideal. What is a good way to architect this feature to fit searching well, but also keep the schema fairly simple (keep in mind, my main goal is to make the tags as similar to stackoverflow as possible)?

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

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

发布评论

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

评论(4

晨与橙与城 2024-12-21 04:29:06

好吧,为了简单起见,为什么不遵循使用三个表的规范化方法:

Questiond(QId, name)

Tag (tagId, name)

Tag-per-topic (QId,tagId)

非常简单......

Well, to keep it simply why not just follow the normalized approach with three tables:

Questiond(QId, name)

Tag (tagId, name)

Tag-per-topic (QId,tagId)

very simple....

土豪我们做朋友吧 2024-12-21 04:29:06

最好的方法是使用外键。

拥有一张表来存储问题/项目及其唯一 ID。
(item_ID, info1, info2, info3 ...)

有另一个表来存储所有可能的标签及其唯一 ID。
(tag_ID, tag_name)

最后一张表将两者链接在一起。
(商品 ID、标签 ID)

The best way to do it is with foreign keys.

Have one table that stores the question/item with its unique ID.
(item_ID, info1, info2, info3 ...)

Have another table that stores all the possible tags and their unique ID.
(tag_ID, tag_name)

Have one last table that links the two together.
(item_ID,tag_ID)

素染倾城色 2024-12-21 04:29:06

只是一个简单的多对多关系?

例如:

table: [questions]
[TableId][name]
'1' - 'general'

table: [Tags]
[TagId][name]
'99' - 'c#'

table: [QuestionsTagged]
[TableId][TagId]
'1' - '99'

然后使用外键约束将它们链接在一起

just a simple many-to-many relation?

For example:

table: [questions]
[TableId][name]
'1' - 'general'

table: [Tags]
[TagId][name]
'99' - 'c#'

table: [QuestionsTagged]
[TableId][TagId]
'1' - '99'

Then link them together with foreign key constraints

逆光飞翔i 2024-12-21 04:29:06

每个帖子都会在某个表中包含一个 ID,其中包含您计划包含的内容。我们称之为“帖子”。

每个帖子标签将位于另一个带有 post_id 和标签(以及您可能喜欢的任何审核列)“post_tags”的表中。

作为管理员,您可以(可选)将记录添加到“标签”查找表中,以查找足够流行的标签,以便您想要很好地定义它们,例如 https://stackoverflow.com/tags/asp.net/info

Each post would have an ID in some table with whatever you plan to include. Let's call it 'posts'.

Each post tag would be in another table with post_id and the tag (and any audit columns you might like) 'post_tags'.

You, as an administrator can then (optionally) add records to a 'tags' lookup table for tags that are popular enough for you to want to define them well, a la https://stackoverflow.com/tags/asp.net/info.

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