How to store tags in MySQL tags, one field as a whole or one for each tag?

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

+2
source share
2 answers

I think Many to Many will help you.

sort of

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

change

The Many to Many approach is more normalized, but I think that it is the most difficult to implement, since in this case it is based on associations to get all the tags for this "product". Benefits:

  • fully normalized
  • DRY : because if you need to change the tag name, you can do it and you will see change everywhere
  • and etc.

another approach is to save all tags in one field, separated by something (for example, a comma). Here you have the speed in terms of receiving tags. you just need to split the tags into this separator, and that is it. Saving tags is easier. but I don’t like this approach, because if you need to update the template, you need to go through the articles, split, update and then save.

+8
source

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 will be your connection between the two ... so you can do something with the effect

SELECT * FROM tblTaskTags WHERE task_id = selected task identifier

+2
source

Source: https://habr.com/ru/post/1482215/


All Articles