简体   繁体   中英

How to store tags for a blog article in SQL database?

I currently am working on developing a blogging website. For this application we're using MySQL as the database. For this application,I created a blog table which contains the following properties:

  • id
  • blog_title
  • content
  • user_id
  • updated_at
  • upvotes

I want to add tags to this table. What is the recommended way of adding tags to this table so that in the application I can search for articles/blogs based on tags ?

It is common to use many-to-many relationship for tags. In your case it can be a couple of tables:

tags

  • id
  • name

tag_blog

  • tag_id (foreign key to tags)
  • article_id (foreign key to articles)

You can set combination of (tag_id, article_id) as primary key. In this case it will be guaranteed that tag can be mentioned only once for the given article.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM