开发者

How to store tags in MySQL tags, one field in total or one filed for each tag?

开发者 https://www.devze.com 2022-12-12 17:28 出处:网络
I am developing a product which is close to stackoverflow.com. A poster is required to enter tags for his problem or task开发者_JAVA技巧. How to store these tags in the database, one field(column) in

I am developing a product which is close to stackoverflow.com. A poster is required to enter tags for his problem or task开发者_JAVA技巧. 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..


I would do something like this...

tblAvailableTags
  - tag_id
  - tag_name

tblTasks
  - task_id
  - task_name
  - ...etc

tblTaskTags
  - task_tag_id
  - task_id
  - tag_id

So tblTaskTags would be your link between the two...so you could do something to the effect of

SELECT * FROM tblTaskTags WHERE task_id = selected task id

0

精彩评论

暂无评论...
验证码 换一张
取 消