Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store tags in a database?

I have a database that contains two tables:

  • entries
  • tags

The entries table contains posts that each have one or more tags. The problem is, each post can have any number of tags. In other words, I can't have a 'tag1', 'tag2', etc. column and do a LEFT JOIN.

How should I set up entries so that each post can have any number of tags?

like image 779
Nathan Osman Avatar asked Aug 22 '10 01:08

Nathan Osman


People also ask

How do I store post tags in a database?

If space is going to be an issue, have a 3rd table Tags(Tag_Id, Title) to store the text for the tag and then change your Tags table to be (Tag_Id, Item_Id). Those two values should provide a unique composite primary key as well. Show activity on this post.

How do I store multiple tags in a database?

The classic way to store it is via an embedded array of tags: { PictureUrl: ..., tags: [ "party", "man" ... ] } As long as number of tags is below a few houndreds its fine. You can index tags field with multi key index and simply search terms on tags.

How do I manage tags in mysql?

you can store JSON array in mysql>=5.7 for tags like below : ["tag1","tag2",...] if you use an extra table for store tags , you need to join and search for adding a new tag , its bad way when we have too many tags in database !

What is SQL tagging?

The SQL tagging feature in Siebel Business Applications is a diagnostic tool that allows administrators to trace long-running or slow-performing queries back to the user or action that triggered it.


1 Answers

You need a mapping table.

Create a table called entries_tags that contains two columns: entry_id and tag_id, with a multi-key on both entries.

This is called a many-to-many relationship.

like image 138
Borealid Avatar answered Nov 11 '22 18:11

Borealid