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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:37:22+00:00 2026-06-10T23:37:22+00:00

The accepted answer to sql swap primary key values fails with the error Can’t

  • 0

The accepted answer to sql swap primary key values fails with the error Can't reopen table: 't' – presumably this has something to do with opening the same table for writing twice, causing a lock.

Is there any shortcut, or do I have to get both, set one of them to NULL, set the second one to the first one, then set the first one to the previously fetched value of the second?

  • 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-10T23:37:24+00:00Added an answer on June 10, 2026 at 11:37 pm

    To swap id values of 1 and 2, I would use a SQL statement like this:

    EDIT : this does NOT work on an InnoDB table, only works on a MyISAM table, per my testing.

    UPDATE mytable a 
      JOIN mytable b ON a.id = 1 AND b.id = 2 
      JOIN mytable c ON c.id = a.id
       SET a.id = 0
         , b.id = 1
         , c.id = 2 
    

    For this statement to work, the id value of 0 must not exist in the table, any unused value would be suitable… but to get this to work in a single SQL statement, you need to (temporarily) use a third id value.


    This solution works for regular MyISAM tables, not temporary tables. I missed that this was being performed on a temporary table, I was confused by the error message you reported Can't reopen table:.

    To swap id values 1 and 2 in a temporary table, I’d run three separate statements, again, using a temporary placeholder value of 0:

    UPDATE mytable a SET a.id = 0 WHERE a.id = 1;
    UPDATE mytable b SET b.id = 1 WHERE b.id = 2;
    UPDATE mytable c SET c.id = 2 WHERE c.id = 0;
    

    Edit: Fixed errors

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

Sidebar

Related Questions

I am using the accepted answer from this question . The relevant code is
Based the accepted answer to this question I've setup a NetBeans/tomcat environment. In testing
I'm using the accepted answer to this question to help me build an HTML
I was reading over this question and wondered if the accepted answer might also
I've found this topic which I've implemented (see accepted answer): javascript equivalent of PHP's
i want to move a table to a new filegroup. Using the accepted answer
The accepted answer to this other question lead me to this sample, but compiling
Im using the accepted answer in this question . As I have the same
I have a question about the accepted answer for this question. Could you explain
I get this error when I execute this statement in PHP: $sql = mysql_real_escape_string(INSERT

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.