I’m designing a database where any content can be tagged and I’m likely to want to be able to select all content with a specific tag.
I’m struggling with the following two options and would appreciate some advice. If there’s a better way please let me know.
Option A
Multiple ‘many to many’ join tables.
tag: id tag media: id title src creation media_tags: id media_id tag_id article: id title content creation article_tags: id article_id tag_id
Options B
A single ‘tag reference’ table, which uses a ‘table’ column to identify which table to join to.
tag: id tag tag_reference: id row_id tag_id table media: id title src creation article: id title content creation
From a maintenance point of view option B seems favorable but considering the SQL query to select all content and don’t think it’s possible without multiple queries.
When using Option B, you can’t set up foreign keys to the other tables. Thus I would go with Option A and one table for each m:n relation.
“From a maintenance point of view option B” – is a nightmare. What happens if you delete an article? All the rows with that
row_idwill persist intag_referencetable. You always need to update those entries manually.