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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:45:08+00:00 2026-06-04T19:45:08+00:00

I’m trying to build an script for a database migration and I have a

  • 0

I’m trying to build an script for a database migration and I have a doubt. I have a table which has lots of registers and an integer type key. This key is auto incremental and now beginning with the ‘1’ index.

The problem is that this index has to be occupied by a default value in the new database. So I want to loop database rows to get each index incremented in one, to leave the first place in blank and insert my value into it. I have tried with this statement:

UPDATE `tapp`, (
  SELECT @loop := id_App
  FROM
    tapp
  ) o
SET id_App = id_App + 1;

However, is trying to update each index starting from the beginning, so when it tries converting the first one to ‘2’ it finds out the second one is already taken and can’t make it.

It’s important to make the increase in one, because it’s a MyIsam database and I also have to update each foreign key one by one. I’m using MySQL.

Please give me a hand!

  • 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-04T19:45:10+00:00Added an answer on June 4, 2026 at 7:45 pm

    The easiest way is twofold: After inserting everything into the target table, you make a 2-pass update.

    First, you check what the highest number is in the table, then increment it by one and remember it as topnumber.

    Then, you update everything to incremental numbers, starting with topnumber. Finally, you update everything, starting with whatever your initial seed is and increment by one each record.

    number data
    1      "foo"
    3      "bar"
    10     "snafu"
    

    topnumber becomes 11

    After the first pass, the data looks like this:

    number data
    11     "foo"
    12     "bar"
    13     "snafu"
    

    After the second pass (and assuming your initial number is 7), the data looks like this:

    number data
    7      "foo"
    8      "bar"
    9      "snafu"
    

    UPDATE

    Alternatively, instead of updating the numbers to incremental values, you could add the remembered top number to every initial value at the first pass (and so the above sample table would look like this after the first update:

    number data
    11     "foo"
    13     "bar"
    23     "snafu"
    

    ), and at the second pass, you would decrement all the numbers by the previously stored top number and increment them by 1, which, for our example, would result in the following:

    number data
    2      "foo"
    4      "bar"
    11     "snafu"
    

    Using the names in your code snippet, the entire script might look something like this:

    /* remember the top ID */
    SET @max_id = (SELECT MAX(ID) FROM tapp);
    
    /* increment by the top ID */
    UPDATE tapp SET id_App = id_App + @max_id;
    
    /* decrement by the top ID and increment by 1 */
    UPDATE tapp SET id_App = id_App - @max_id + 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.