如何在MySQL标签中存储标签,一个字段总共或一个为每个标签提交? [英] How to store tags in MySQL tags, one field in total or one filed for each tag?

查看:673
本文介绍了如何在MySQL标签中存储标签,一个字段总共或一个为每个标签提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个靠近stackoverflow.com的产品。需要一张海报来输入他的问题或任务的标签。如何将这些标签存储在数据库中,一个字段(列)或一个标签的一个字段(列)?

解决方案

我认为多对多关系将有助于您p>

  -------- --- -------------- ------------ 
- 标签 - < -------> - products_tags - < -------> - 产品 -
-------- ----------------- ------------



编辑:



多对多的方法是更规范化但是我认为最难实现,因为在这种情况下,为了获得给定的产品的所有标签。
优点:


  1. 完全正常化

  2. DRY :由于
    如果您需要更改标签名称,您
    可以做到,您将看到
    无处不在

  3. 等。

另一种方法是保存所有标签在一个字段中分开的东西(让我们说逗号)。在这里,您可以获得标签的速度。你只需要用分隔符拆分标签就可以了。保存标签更容易。
但是我不喜欢这种方法,因为如果你需要更新一个模板,你需要按文章,拆分,更新,然后保存。


I am developing a product which is close to stackoverflow.com. A poster is required to enter tags for his problem or task. How to store these tags in the database, one field(column) in total or one field(column) for one tag?

解决方案

I think the Many to Many relationship will help you

something like

--------             -----------------           ------------
- tags -   <-------> - products_tags - <-------> - products - 
--------             -----------------           ------------

edit:

the Many to Many approach is the more normalized one, but I think the hardest to implement, since is based in joins to get all tags for a given "product" in this case. advantages:

  1. totally normalized
  2. DRY: since if you need to change a tag name you can do it and you will see the change everywhere
  3. etc.

the other approach is to save all tags in one field separated by something (let say comma). Here you have speed in terms of getting the tags. you just need split the tags by that separator and thats it. Saving tags is more easy too. but I dont like this approach because if you need to update a template you need to go article by article, split, update, and then save..

这篇关于如何在MySQL标签中存储标签,一个字段总共或一个为每个标签提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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