How to model tags in the database?

被刻印的时光 ゝ 提交于 2019-11-30 03:37:24

问题


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?


回答1:


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_ID (pk)

OBJECT_TAG_XREF

  • OBJECT_ID (pk, fk to OBJECT)
  • TAG_ID (pk, fk to TAG)

TAG

  • TAG_ID (pk)



回答2:


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.




回答3:


Definitely normalize. A table for tags, a table for your existing objects, and a table of links between them.



来源:https://stackoverflow.com/questions/1329887/how-to-model-tags-in-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!