I’m trying to implement a 3 table item tagging system
Table: Item
Columns: ItemID (primary, auto_increment), Title, Content
Table: Tag
Columns: TagID (primary, auto_increment), Title, TagCount
Table: ItemTag
Columns: ItemID, TagID
Each Title of a tag is unique.
I would like to implement an efficient way of inserting to the Tag table:
1. Check if Title exists
2. If it exists, update TagCount+1
3. Else if it doesn’t exist, insert
I looked into INSERT ON DUPLICATE KEY UPDATE, but I’m confused if it can be applied because my key is the TagID and not the Title.
I’m assuming you are trying to do the insert into the table “Tag”.
You need to modify the table and add a unique index on the title field :
Then, you can do the following
Hopefully that does the trick for you.