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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:41:06+00:00 2026-05-17T17:41:06+00:00

I am dealing with a MySQL table here that is keyed in a somewhat

  • 0

I am dealing with a MySQL table here that is keyed in a somewhat unfortunate way. Instead of using an auto increment table as a key, it uses a column of decimals to preserve order (presumably so its not too difficult to insert new rows while preserving a primary key and order).

Before I go through and redo this table to something more sane, I need to figure out how to rekey it without breaking everything.

What I would like to do is something that takes a list of doubles (the current keys) and outputs a list of integers (which can be cast down to doubles for rekeying).

For example, input {1.00, 2.00, 2.50, 2.60, 3.00} would give output {1, 2, 3, 4, 5).

Since this is a database, I also need to be able to update the rows nicely:

UPDATE table SET `key`='3.00' WHERE `key`='2.50';

Can anyone think of a speedy algorithm to do this? My current thought is to read all of the doubles into a vector, take the size of the vector, and output a new vector with values from 1 => doubleVector.size. This seems pretty slow, since you wouldn’t want to read every value into the vector if, for instance, only the last n/100 elements needed to be modified.

I think there is probably something I can do in place, since only values after the first non-integer double need to be modified, but I can’t for the life of me figure anything out that would let me update in place as well. For instance, setting 2.60 to 3.00 the first time you see 2.50 in the original key list would result in an error, since the key value 3.00 is already used for the table.


Edit: I guess what this really abstracts to is this:

I need a way to convert an ordered map keyed with doubles into an ordered map keyed with integers, where at no point does there ever exist two values for one key (which is a violation of a map anyway).

  • 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-17T17:41:06+00:00Added an answer on May 17, 2026 at 5:41 pm

    I’m assuming you’ll be able to take the database down at some point to make this conversion.

    Note: I am NOT a MySQL user. My DB of choice is PostgreSQL, so there MAY BE SYNTAX ERRORS here between how MySQL does it and Pg does it. But this should give you a good idea.

    First, make a keymap table that maps old keys to new:

    create table keymap (
        oldkey decimal,
        newkey integer autoincrement
    )
    

    Make sure you index keymap because we’re going to be lookups aplenty on it.

    create unique index keymap_oldkey on keymap(oldkey);
    

    Then fill it with old keys and let MySQL create the new ones:

    insert into keymap
        select distinct `key` from fribbles order by `key`
    

    Now you’ll have keymap with all the old keys, and because you haven’t specified a new key, you’ll have the autoincrement on the newkey column will populate, and your table will look like.

    oldkey    newkey
    ----------------
    1.5       1
    1.6       2
    1.93      3
    3.1       4
    

    Now, add a newkey column to your tables that need it

    alter table fribbles add column newkey integer
    

    Don’t make it autoincrement, because otherwise it will get populated at alter time, and we don’t need that.

    Now, finally, update the fribbles table:

    update fribbles f
        set newkey = ( select newkey from keymap m where m.oldkey = f.`key` )
    

    Finally, now that you have newkey populated, you can drop the old one.

    alter table fribbles drop column `key`;
    alter table fribbles alter column newkey rename to `key`;
    

    I hope that gives you a decent plan of attack.

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

Sidebar

Related Questions

I'm dealing with a MySQL table that defines the JobName column as UNIQUE. If
I have the following script to create a table in MySQL version 5.1 which
Right now I'm dealing with an issue regarding an intense acts_as_tree MySQL query via
I am developing a script in my localhst using PHP and mysql and I
I have a table of data, and I allow people to add meta data
This is almost similar question to this one: - Dealing with timezones in PHP
Right now we are dealing with a bit of a conundrum in my corporate
I'm trying to figure out why one of our migration scripts is taking forever
I know this is not a straight up question, so if you need me
I posted a similar question to this not too long ago in regards to

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.