如何设计一个用于将多个标签与ID关联的关系数据库? [英] How to design a relational database for associating multiple tags with id?

查看:262
本文介绍了如何设计一个用于将多个标签与ID关联的关系数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Q/A网站项目,该网站中的一个问题可以与多个标签链接.

I'm developing a project of Q/A website, and a questions in this website can be linked with multiple tags.

例如:How can I implement quicksort in C++ ?

此问题的标签可以为C, C++, Algorithm

我的问题是如何将这些标签存储在MySQL表中?

My question is how can I store these tags in MySQL table ?

我的方法:

上面的问题有id = 123

-------------------------
|question_id | tags     |
-------------------------
| 123        |  C       |
-------------------------
| 123        |  C++     |
-------------------------
| 123        |Algorithm |
-------------------------
| 124        | Java     |
-------------------------

但是,如果我以这种方式创建一个表,那么对于很多问题,该表将变得非常大.

But if I create a table in this sense, then for lot of questions, the table will become very large.

有没有更好,更有效的方法来存储这种数据?

Is there any better and efficient way to store this kind of data ?

推荐答案

有一个额外的表,该表具有3列:被标记的商品ID,标签,订购信息.

Have an extra table with exactly 3 columns: id of item being tagged, tag, ordering info.

添加或删除项目时,请添加或删除此表中的所有相关行.

When you add or delete an item, add or delete all the relevant rows from this table.

详细信息在这里: http://mysql.rjweb.org/doc.php/列表
高效,快速,简便且高度可扩展.

The details are here: http://mysql.rjweb.org/doc.php/lists
Efficient, fast, easy, and highly scalable.

这篇关于如何设计一个用于将多个标签与ID关联的关系数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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