Steven
Steven

Reputation: 25314

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

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?

Upvotes: 2

Views: 6218

Answers (2)

Gabriel Sosa
Gabriel Sosa

Reputation: 7956

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..

Upvotes: 8

erik
erik

Reputation: 3880

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

Upvotes: 2

Related Questions