Reputation: 485
I just need some advice about using tags. I'm working on a website right now and the users can post videos, images, audio and information about themselves.
I never worked with tags before on a normal website (did work with them on wordpress, youtube etc..), so I need some advise to start designing my database.
What is the common way to work with tags when you have multiple categories? Do I have to add tags for each category? Example:
Table:
gallery
gallery_id
image
tags
Table
audio
audio_id
title
link
audio_tags
Etc...
Or do I make only one table named "tags" and all the tags in it? Like: Table Tags:
tag_id
tag
Thanks in advance.
Upvotes: 0
Views: 84
Reputation: 42380
It depends if you want tags to be shared among multiple objects. If you want that to be separate, then i would make separate tag tables for them. If not, I would have the same tag table, and use different tables for each relation the tags will have, so
for separate:
gallery
...
gallery_tags
tag_id
tag
tags_to_gallery
tag_id
gallery_id
..same for audio..
for not separate:
gallery
...
audio
...
tags
tag_id
tag
gallery_to_tag
gallery_id
tag_id
audio_to_tag
audio_id
tag_id
you could also do this with one ploymorphic table. with a table with something like this:
tag_relations
tag_id
object_id
object_table
Upvotes: 0
Reputation: 4749
It is up to you how you need to shape the application. If you want tags to be shared between galleries and audio, use a single table and two many-to-many reference tables (or one with polymorphism).
Upvotes: 1
Reputation: 15616
Technically the best format is the one you described first, but if you were going to implement the second you would need an extra field - that is the category field. But it would be better and easier to simple have tag fields in each table - better for readability and also use when you come to implement it in your site.
Hope that helps.
Upvotes: 0