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

  • Home
  • SEARCH
  • 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 9058659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:46:47+00:00 2026-06-16T14:46:47+00:00

I have got an older database for which (at some really questionable and obscure

  • 0

I have got an older database for which (at some really questionable and obscure reason I do not like to put too much on topic here) I want to randomize or shuffle the primary keys.

I right now have auto-increment fields in the Mysql database tables.

I have not many relations, those that exist are not defined as foreign keys. The relationships do not need to be preserved.

All I’m looking for is to take the current values of the primary keys and replace it with a random value out of those like:

ID := new(ID)

Where the new function returns a value from the set of all OLD ids with a 1:1 match. E.g.

2 := 3
3 := 2

But not

2 := 3
3 := 3

Is there a way to change the data in the database with (ideally) a single SQL query per table?

Edit: I do not have really strict requirements. Consider to have exclusive access to the database if it helps, including changing constraints on the primary key back and forth, e.g. alter the table, do the operation, alter the table to previous schema. It is also possible to add another column for the new (or old) PK value.

  • 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-16T14:46:48+00:00Added an answer on June 16, 2026 at 2:46 pm

    Just a scetch of the procedure. Create two temporary tables

    CREATE TABLE temp_old
    ( ai INT NOT NULL AUTO_INCREMENT
    , id INT NOT NULL
    , PRIMARY KEY (ai)
    , INDEX old_idx (id, ai)
    ) ENGINE = InnoDB ;
    
    CREATE TABLE temp_new
    ( ai INT NOT NULL AUTO_INCREMENT
    , id INT NOT NULL
    , PRIMARY KEY (ai)
    , INDEX new_idx (id, ai)
    ) ENGINE = InnoDB ;
    

    Copy the id values in different orders to the two tables (randomly in the 2nd table):

    INSERT INTO temp_old
      (id)
    SELECT id
    FROM tableX 
    ORDER BY id ;
    
    INSERT INTO temp_new
      (id)
    SELECT id
    FROM tableX 
    ORDER BY RAND() ;
    

    Then we drop the primary key:

    ALTER TABLE tableX
      DROP PRIMARY KEY ; 
    

    to run the actual UPDATE statement:

    UPDATE tableX AS t
      JOIN temp_old AS o
        ON o.id = t.id
      JOIN temp_new AS n
        ON n.ai = o.ai
    SET t.id = n.id ;
    

    Then recreate the primary key and drop the temp tables:

    ALTER TABLE tableX
      ADD PRIMARY KEY (id) ; 
    
    DROP TABLE temp_old ;
    DROP TABLE temp_new ;
    

    Tested in SQL-Fiddle

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

Sidebar

Related Questions

For performance reasons, I've got a denormalized database where some tables contain data which
I have an (access) database table which contains data I would like to populate
I have got a form into which information is entered, and a drop down
I have got a table [newsletter] in the db, which saves the email address,
I have got a few tables which I am trying to join. I just
I've got a game which dumps state information to a database. Eg. Connected, Lost
I have a database table like this: id version_id field1 field2 1 1 texta
I have customer that uses older, custom built ERP application for which they don't
I have an SQLite3 database which, in order to optimize performance, uses computed columns
At my workplace we have a database with multiple schemas, and some table names

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.