Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 530911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:14:46+00:00 2026-05-13T09:14:46+00:00

I have the following tables; CREATE TABLE IF NOT EXISTS `tags` ( `tag_id` int(11)

  • 0

I have the following tables;

CREATE TABLE IF NOT EXISTS `tags` (
  `tag_id` int(11) NOT NULL auto_increment,
  `tag_text` varchar(255) NOT NULL,
  PRIMARY KEY  (`tag_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;


CREATE TABLE IF NOT EXISTS `users` (
  `user_id` int(11) NOT NULL auto_increment,
  `user_display_name` varchar(128) default NULL,
  PRIMARY KEY  (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

CREATE TABLE IF NOT EXISTS `user_post_tag` (
  `upt_id` int(11) NOT NULL auto_increment,
  `upt_user_id` int(11) NOT NULL,
  `upt_post_id` int(11) NOT NULL,
  `upt_tag_id` int(11) NOT NULL,
  PRIMARY KEY  (`upt_id`),
  KEY `upt_user_id` (`upt_user_id`),
  KEY `upt_post_id` (`upt_post_id`),
  KEY `upt_tag_id` (`upt_tag_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

CREATE TABLE IF NOT EXISTS `view_post` (
`post_id` int(11)
,`post_url` varchar(255)
,`post_text` text
,`post_title` varchar(255)
,`post_date` datetime
,`user_id` int(11)
,`user_display_name` varchar(128)
);

The idea is that I would like to use the most effective way to save tags, for a post and users. Simply once I add a post I pass few tags along that post and user. Later I would like to be able to count tabs for each user and post. Something very similar to Stack Overflow.

I suppose that the ‘tag_text’ should be unique? Is if effective that I run a function each time I submit a new post to go through the ‘tags’ table to check if a tag already exists, and if yes, return its ‘tag_id’ so I can insert it into ‘user_post_tag’ table.

Is this maybe a bad approach to tackle this kind of issue.

All suggestions are welcome.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:14:46+00:00Added an answer on May 13, 2026 at 9:14 am

    Yes, what you are doing is the best way to do it. You created an n to m relationship, as a post can have multiple tags and the same tag can be on multiple posts. You do not want to store the tag name for each of the posts, so you store the id.

    But, you should -NOT- have this redudancy of storing multiple times the same tag_id for the same user. It will hit hard your server if the users have multiple tags and you have to execute SELECT count(...) for each of these tags. Do you understand what I’m talking about here? Because right now, how would get how many times the user A has the tag B? You’d have to do SELECT count(*) FROM user_post_tag INNER JOIN tags ON (...) WHERE user_id=A and tag_id=B.

    My suggestion is to split user_post_tag into two tables:

    1. user_tags, to count how many times the user has this tag, primary key would be user_id and tag_id and you’d have a count field, which you would just update with count=count+1 everytime this user makes a new post with the tag. This way, you can simply do SELECT tag_text, count FROM user_tags INNER JOIN tags ON (...) WHERE user_id=A to select all tags (with number of times used) of a given user. You’re using a fully indexed query. You’re not asking MySQL to go over the table, look for a bunch of rows and count them, you’re telling to MySQL, go this row at this table and at the other table, join them and give it to me, fast!
    2. post_tags, to store the tags a certain post have, primary key would be post_id and tag_id, no additional fields needed.

    I suppose that the ‘tag_text’ should
    be unique? Is if effective that I run
    a function each time I submit a new
    post to go through the ‘tags’ table to
    check if a tag already exists, and if
    yes, return its ‘tag_id’ so I can
    insert it into ‘user_post_tag’ table.

    Yes, it should be unique. It’s way better to check if a tag exists before inserting and inserting if it doesn’t than having redundancy and having to do SELECT … count(*) to know how much times the tag has been used. It will be much mess less frequent post creation than post selection, so if you have to pick between being query intensive on insertion and selection, certainly pick insertion.

    By the way, if you’d like to have a count of how many posts have the same tag, like in stack overflow, you’d need another table, with primary key tag_id, and then, like on user_tags, you increment the count field everytime a post gets a certain tag.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following tables CREATE TABLE `files` ( `fileid` int(11) NOT NULL AUTO_INCREMENT,
I have the following tables: CREATE TABLE `accommodations` ( `id` int(11) unsigned NOT NULL
I have the following tables CREATE TABLE IF NOT EXISTS `user` ( `id` int(11)
I have the following tables: CREATE TABLE IF NOT EXISTS `Person_Categories` ( `PrsCatID` int(11)
I have the following table: CREATE TABLE IF NOT EXISTS `customer_list` ( `id` INT
I have this Tags table CREATE TABLE IF NOT EXISTS `Tags` ( `id_tag` int(10)
I have 2 tables with the following structures: CREATE TABLE IF NOT EXISTS `campaigns`
I have the following table: CREATE TABLE IF NOT EXISTS `notes` ( `id` int(10)
I have the following scheme: CREATE TABLE IF NOT EXISTS `connections` ( `username1` varchar(15)
I have 2 tables as the following - CREATE TABLE IF NOT EXISTS `nl_members`

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.