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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:25:29+00:00 2026-06-15T18:25:29+00:00

Right now I have a table with an auto increment id number, a name,

  • 0

Right now I have a table with an auto increment id number, a name, and a definition
I am updating the table with values from another table using

INSERT INTO words(id, word, definition) SELECT id, word, definition FROM temp 
ON DUPLICATE KEY UPDATE words.word = temp.word, words.definition=temp.definition;

The table temp has the same exact column layout as the table I am using, and it works fine except when I delete an entry from the middle of temp and then update my main table.
For example,

temp
id  word definition
1   bob   is a cat
2   sam   is a dog
3   doug  is a monk
4   croe  is a bird

table main is set to that
Then I remove row 2 from temp

temp
    id  word definition
    1   bob   is a cat
    2   doug  is a monk
    3   croe  is a bird

and then update main table

main
id  word definition
1   bob   is a cat
2   sam   is a dog
3   croe  is a bird
4   croe  is a bird

because temp only has 3 rows now, but main HAD 4, the 4th row isn’t deleted. I tried to fix this by making word a unique column, and now I get the error that there is a duplicate entry for key ‘word’.
So how do I fix this?

  • 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-06-15T18:25:30+00:00Added an answer on June 15, 2026 at 6:25 pm

    From the MySQL manual,

    In general, you should try to avoid using an ON DUPLICATE KEY UPDATE
    clause on tables with multiple unique indexes.

    so, what you could do is make word your primary key, and get rid of the id column. That should do the trick 🙂

    Your query then becomes

    INSERT INTO words(word, definition) SELECT word, definition FROM temp 
    ON DUPLICATE KEY UPDATE words.word = temp.word, words.definition=temp.definition;
    

    If you need arbitrary sorting, I would create an additional table

    CREATE TABLE word_order (VARCHAR(30) PRIMARY KEY, sort_order INT)
    

    and then when you run your queries

    SELECT words.word,
           words.definition
    FROM words
    JOIN word_order
    ON words.word = word_order.word
    ORDER BY word_order.sort_order
    

    Your INSERTS become a little more complicated though, you’d have to run multiple inserts. See sql – insert into multiple tables in one query.

    Also, this CodeProject article has some more ideas on the topic.

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

Sidebar

Related Questions

I am writing a MVC3 project. Right now I have a table which has
I am writing a MVC3 project. Right now I have a table which has
I have some code that shows results in a table Right now it will
Good day! I right now have a function the drags an element from a
Right now, I have a table whose primary key is an auto_increment field. However,
Right now I have 3 tables: CREATE TABLE `tbl_payment_info` ( `payment_info_id` int(11) NOT NULL,
I am using PHPMyAdmin right now and I am creating a new table with
I have a table with 18,310,298 records right now. And next query SELECT COUNT(obj_id)
Right now I am working with a mysql database table. I have created a
Right now I have 3 tables: User, Roles, and User_Roles for the many-to-many association.

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.