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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:30:33+00:00 2026-05-26T00:30:33+00:00

I need to add an auto increment id to an already existing table. I

  • 0

I need to add an auto increment id to an already existing table. I did:

ALTER TABLE table_name ADD column_name INT NOT NULL AUTO_INCREMENT FIRST ,
ADD PRIMARY KEY (column_name)

However, the auto numbering wasnt based on any particular column order. I want mysql to smartly put auto numbers based on the order of certain column. Is it possible?

Most of the answers out there tell you how to add auto increment fields, not how to control those numbers in already existing table.

  • 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-26T00:30:34+00:00Added an answer on May 26, 2026 at 12:30 am
    1. Add a new field – ALTER TABLE table_name ADD column_name INT FIRTS;
    2. Populate data in the new field.
    3. Make this field as part of primary key and add AUTO_INCREMENT option –

      ALTER TABLE table_name
      CHANGE COLUMN id id INT(11) NOT NULL AUTO_INCREMENT,
      ADD PRIMARY KEY (id);

    EDIT:

    Try this plan:

    1. Create another table, the same as table_name.
    2. Populate new table with sorted data from table_name table:

      INSERT INTO temp_table_name SELECT * FROM table_name ORDER BY name;

    3. Empty table_name table:

      TRUNCATE TABLE table_name;

    4. Alter table_name table, add new auto increment field and make it primary (your code).

    5. Copy data from temp. table to the table_name:

      INSERT INTO table_name SELECT NULL, t.* FROM temp_table_name t;

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

Sidebar

Related Questions

I need to ALTER my existing database to add a column. Consequently I also
I have table with following structure id int auto increment, primary key uid int
I need to add a blog to my existing rails application. I am looking
I'm changing my mysql db table from an id (auto) to a uid. ALTER
I've created a table: CREATE TABLE IF NOT EXISTS `markersStorage2` ( `id` int(11) NOT
I have a very specific application where I need an auto-increment variable with persistent
Possible Duplicate: Adding an identity to an existing column how to set auto increment
I need add a new user group for mediawiki. The new group has more
need to add a 2nd css stylesheet to a page. do i add a
I need to add the values of an associative array to another one. $a

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.