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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:38:06+00:00 2026-06-11T10:38:06+00:00

I have a PHP page which interacts with this small MySQL database table with

  • 0

I have a PHP page which interacts with this small MySQL database table with a simple structure. ID, Name, Address are the fields. ID is the primary key.

+------+------------+-------------+
|  ID  |   Name     |   Country   |
|______|____________|_____________|
| E001 |  Stephen   |   America   |
| E002 |  John      |   Britain   |
| E003 |  Kate      |   Canada    |
| E004 |  Carlos    |   Spain     |
| E005 |  James     |   Australia |
|______|____________|_____________|

If I remove the record at E003, it looks like this.

+------+------------+-------------+
|  ID  |   Name     |   Country   |
|______|____________|_____________|
| E001 |  Stephen   |   America   |
| E002 |  John      |   Britain   |
| E004 |  Carlos    |   Spain     |
| E005 |  James     |   Australia |
|______|____________|_____________|

A gap appears between E002 and E004.

Is there a way to fill in that gap? Like this

+------+------------+-------------+
|  ID  |   Name     |   Country   |
|______|____________|_____________|
| E001 |  Stephen   |   America   |
| E002 |  John      |   Britain   |
| E003 |  Carlos    |   Spain     |
| E004 |  James     |   Australia |
|______|____________|_____________|

Earlier record which had E004, fall into the place of E003. Former E005 gets the number E004 and so on. Like the records take a step back.

Is there a way to do this programmatically?

  • 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-11T10:38:07+00:00Added an answer on June 11, 2026 at 10:38 am

    Is there a reason you need to renumber them? The key values shouldn’t really matter. If they do (i.e., they are used for outside purposes), consider having the external key be a regular column in the table and let the primary key be an auto-increment integer field. Even if you do this, you still probably shouldn’t be renumbering keys. That would mean that all external dependencies would need to be updated as well. There’s just not much value in doing it.

    But, if you have to…

    If the key is auto-increment (which it should be), you can do this:

    ALTER TABLE MyTable DROP ID;
    ALTER TABLE MyTable ADD COLUMN ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT FIRST;
    

    If it’s not, then you can do several things, but the easiest might be to use variables to act as a counter. To make this easier, please don’t use a string primary key, but instead use a plain numeric primary key.

    SET @id = 0;
    
    UPDATE MyTable
    SET ID = (@id := @id + 1)
    ORDER BY ID;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a a php page which updates a mySql database it works fine
I have a php page which dynamically produces this; ... <select name='sales_id'> <option> <option
I have a simple php page which outputs a table (which has been created
I have web page in PHP which displays all records in a table. I
I have a php page which contains a form. Sometimes this page is submitted
I have an asp.net web page which interacts with a SQL Server database, grabs
I have a PHP page which generates an HTML table. At the bottom of
I have a php page which shows profiles of users from a database. I
I have a PHP page which breaks down in IE 6 and 7, hence
I have the following in a .htaccess file redirect 301 /page.php http://domain.com/page Which works

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.