如何在数据库中建模标签? [英] How to model tags in the database?

查看:23
本文介绍了如何在数据库中建模标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的 web 应用程序并想添加一个标记功能,以便用户可以标记现有的对象.问题是我应该为每个对象添加一个标签列吗?还是应该对其进行规范化并使用标签表,其中每个对象都有一组标签?我倾向于后者,因为它感觉更干净,更容易报告并且更容易创建标签云.但是因为我知道这已经解决了 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_ID (pk)

OBJECT_TAG_XREF

OBJECT_TAG_XREF

  • OBJECT_ID(pk,fk 到 OBJECT)
  • TAG_ID(pk、fk 到 TAG)

标签

  • TAG_ID (pk)

这篇关于如何在数据库中建模标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆