Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Rebuild index id #

How to rebuild the id of primary keys

5:00 am on Oct 10, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 7, 2007
posts: 46
votes: 0

Hey guys and gals, just wondering if there is a simple way to rebuild the primary index ID numbers of my tables?

I have a content table, each has a primary auto incrementing key called ID. Over time, these values has fragmented. For example, it will go to 187, then skip to 240 and continue, and so on.

What I want to do is where it skips, overwrite 240 and start back at 188 and continue upward correctly. Is there an easy way to do this without manually correcting this?

By the way, the database is MySQL and I am using PHP.

8:47 pm on Dec 7, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Dec 9, 2003
votes: 0

If you have other tables which use those values, this sort of operation is VERY risky. You'll have to write code that updates all the different joined rows with the new value for every single row.

If you aren't using those values anywhere else and just want to reorder them, it should be as simple as removing that column from the table and then re-adding a new auto incrementing column to the table.


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members