推荐的SQL数据库设计用于标签或标记 [英] Recommended SQL database design for tags or tagging

查看:354
本文介绍了推荐的SQL数据库设计用于标签或标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说过几种实现标记的方法;使用TagID和ItemID之间的映射表(对我来说是有意义的,但是它是否扩展?),向ItemID添加固定数量的可能的TagID列(似乎是一个坏主意),将标签保存在逗号分隔的文本列中疯狂,但可以工作)。我甚至听说有人推荐一个稀疏矩阵,但是标签名称如何正常增长?



我错过了标签的最佳做法?

解决方案

三个表(一个用于存储所有项目,一个用于所有标签,一个用于两个关系),使用外键正确索引设置在正确的数据库上运行,应该能够正常工作并扩展。

 表:项
列:ItemID,标题,内容

表: b $ b列:TagID,标题

表:ItemTag
列:ItemID,TagID


I've heard of a few ways to implement tagging; using a mapping table between TagID and ItemID (makes sense to me, but does it scale?), adding a fixed number of possible TagID columns to ItemID (seems like a bad idea), Keeping tags in a text column that's comma separated (sounds crazy but could work). I've even heard someone recommend a sparse matrix, but then how do the tag names grow gracefully?

Am I missing a best practice for tags?

解决方案

Three tables (one for storing all items, one for all tags, and one for the relation between the two), properly indexed, with foreign keys set running on a proper database, should work well and scale properly.

Table: Item
Columns: ItemID, Title, Content

Table: Tag
Columns: TagID, Title

Table: ItemTag
Columns: ItemID, TagID

这篇关于推荐的SQL数据库设计用于标签或标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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