views:

172

answers:

2

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?

+3  A: 

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

Gabriel Sosa
A: 

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

espais
Good, but task_tag_id is unnecessary because task_id+tag_id together can act as primary key.
Tomas