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 7542005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:02:55+00:00 2026-05-30T08:02:55+00:00

I have a MySQL tag-mapping table with structure from the following post – Recommended

  • 0

I have a MySQL tag-mapping table with structure from the following post – Recommended SQL database design for tags or tagging

I have two columns – TagID and ItemID. I want to make an update on some item (change it’s tags). For example I had item with ID=1 with tagID’s 1,2,3. But on update user want’s only tags with ID’s 3,4,5. Obviously I have to remove the “1” and “2”, keep the “3” and add “4” and “5”.

What’s the simplest way/most elegant to do it (use INSERT INGORE’S, ON DUPLICATE UPDATE or some other “features” of MySQL)?

Delete all and insert all “new” tags back or go through them all and delete/insert separately? It will be min 1, max 5 tags. Or it really doesn’t matter for such a small number of tags/operations?

  • 1 1 Answer
  • 0 Views
  • 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-30T08:02:57+00:00Added an answer on May 30, 2026 at 8:02 am

    If you definitely have a maximum of five tags, including them in the table as tag1, tag2, tag3, tag4, tag5 will definitely be “simplest”. For extra simplicity, you could just update all 5 values, so tags 3,4,5 are saved to tag1,tag2,tag3, with tag4 and tag5 saved as NULL. Obviously it’s not scalable – you definitely wouldn’t want to do that with a large number (or potentially unlimited) tags.

    The next simplest would be to delete all and reinsert, but it’s not particularly elegant.

    The most elegant way I can think of is to run two queries. The first will remove everything that is not your new set of tags. I have extended your example to show adding two replacement tags: (3,4,5,6,7).
    The next will insert all of your new tags, but will ignore duplicates, based on a UNIQUE combined index of TagID and ItemID.

    #This creates the table and UNIQUE index, so you can understand how the next queries work
    CREATE TABLE `taglinks` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `ItemID` int(11) NOT NULL,
      `TagID` int(11) NOT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `Linkindex` (`ItemID`,`TagID`)
    );
    
    #This inserts example data
    INSERT INTO `taglinks` (`ItemID`, `TagID`) VALUES (1,1),(1,2),(1,3),(1,4),(1,5);
    
    #This removes tags 1 and 2
    DELETE FROM `taglinks` WHERE `ItemID` = 1 AND `TagID` NOT IN (3,4,5,6,7);
    
    #This adds two new tags (6 and 7). Tags 3, 4 and 5 are unaffected
    INSERT IGNORE INTO `taglinks` (`ItemID`, `TagID`) VALUES (1,6),(1,7),(1,3),(1,4),(1,5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have the following in mySQL: SELECT t.tag, COUNT( * ) AS `count` FROM
I have two queries to get related tags from a mysql database, one works,
I have an array of tags taken from a MySQL database. Obviously, these tags
Rails and MySQL: I have a table with several boolean columns representing tags. I
I have a MySQL table to hold tags (i.e. like those used here on
I have the following query in MySQL SELECT *,(select lat from node where id=node_id)as
In MySQL table, I have a field called Tag, which may include multiple comma-delimited
I have mysql database and I want a software which can draw the database
I have mysql table that has a column that stores xml as a string.

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.