如何建模数据库中的标签? [英] How to model tags in the database?
问题描述
我有一个现有的webapp,并且想要添加一个标签功能,以便用户可以标记现有对象。问题是我应该为每个对象添加一个标签列吗?或者我应该规范化它,并使用一个标签表,其中每个对象将有一个标签的集合?我倾向于后者,因为它感觉更干净,更容易报告,更容易创建标签云。但因为我知道这已经解决了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?
推荐答案
用户需要将多个标签与对象关联?
Do you foresee users needing to associate more than one tag with an object?
如果不,请将TAG_ID fk添加到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_ID(pk)
OBJECT_TAG_XREF
OBJECT_TAG_XREF
- OBJECT_ID(pk,fk到OBJECT)
- TAG_ID(pk,fk到TAG)
TAG
- TAG_ID(pk)
这篇关于如何建模数据库中的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!