如何对数据库中的标签进行建模?
我有一个现有的网络应用程序,想要添加标签功能,以便用户可以标记现有对象。问题是我应该为每个对象添加一个标签列吗?或者我应该将其规范化并使用标签表,其中每个对象都有一个标签集合?我倾向于后者,因为它感觉更干净、更容易报告并且更容易创建标签云。但既然我知道这个问题已经被解决了 1000 次,我想问一下我是否遗漏了什么?
I have an existing webapp and want to add a tag feature so that users can tag existing objects. The question is should I add a tag column to each object? or should I normalize it and use a tag table where each object will have a collection of tags? I am leaning towards the latter because it feels cleaner, easier to report on and easier to create a tag cloud. But since I know this has been solved 1000 times I wanted to ask and see if I am missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否预见到用户需要将多个标签与一个对象相关联?
如果不,请将 TAG_ID fk 添加到 OBJECT 表中。否则,您总共需要三个表才能正确建模多对多关系:
OBJECT
OBJECT_TAG_XREF
TAG
Do you foresee users needing to associate more than one tag with an object?
If not, add the TAG_ID fk to the OBJECT table. Otherwise, you'd need three tables in total to correctly model a many-to-many relationship:
OBJECT
OBJECT_TAG_XREF
TAG
是的,你应该将其标准化。 “标签列”要么只支持每条记录一个标签,要么具有糟糕的搜索性能。
Yes, you should normalize it. The 'tag column' is either going to support only one tag per record, or is going to have hideous search performance.
绝对正常化。标签表、现有对象表以及它们之间的链接表。
Definitely normalize. A table for tags, a table for your existing objects, and a table of links between them.