I have a tag table like this:
TAG ID | TAG_NAME | IMAGE_ID
ex:
1 | FUNNY | 10 <br />
2 | DOG | 10
3 | SUNNY | 10
4 | JULY | 10
and I also keep a reference to all the tags associated to an image in the image table
IMAGE_ID | IMAGE_NAME | IMAGE_TAGS ( varchar(255) utf8_general_ci )
ex:
10 | dog.jpg | FUNNY,DOG,SUNNY,JULY
The reasoning for the tag column in the images table, is so that if an image is retrieved, all the associated tags can be retrieved as well without having to dip into the tags table.. but for some reason I’m starting to think it would just be best to get all the associated tags from the tag table to reduce redundancy and to be certain it’s associated with that tag (it could have been removed or deleted but the data doesn’t match) –
but then again I also feel it might be benefical to keep the tags column.. I can’t figure out if I should keep the column or drop it.. am I over thinking this?
Yes, the
image_tagscolumn should be removed.When you store comma separated data, you will loose all the indexing and integrity goodness that comes with your database engine. This is why it’s considered a sql anti-pattern.
Your tags table should probably not have the
image_idcolumn either. Consider using this schema:The tags for each image are kept in a m:n table
imagetags. Foreign key constraints will help to further strengthen the data integrity.