What would be the most effective way to insert tags into a table

holly

New Member
I have the following tables;\[code\]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));\[/code\]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.
 
Back
Top