473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Updating item numbers (or IDs)

After a while of deleting records in a MySQL db, there gets to be the
gaps in the id numbering system. i.e. 1, 2, 3, 6, 7, 12, and so on.
Is there a way to renumber the id system in a table for 1, 2, 3, 4, 5,
6, etc without manually going in and changing those numbers?
Apr 27 '06 #1
5 1634
cover wrote:
After a while of deleting records in a MySQL db, there gets to be the
gaps in the id numbering system. i.e. 1, 2, 3, 6, 7, 12, and so on.
Is there a way to renumber the id system in a table for 1, 2, 3, 4, 5,
6, etc without manually going in and changing those numbers?


I hope not.
Why do you want such a strange thing?

While designing a database, good designers make use of foreign keys to to
make sure the relations in the database will stay consistent.

Consider the following pseudocode:

create table tbluser(
userid autonumber Primary Key,
username text
);

create table writtenarticles(
writtenarticleid autonumber Primary Key,
writtenby numeric references tbluser(userid),
title text,
article text
);

The above situation makes sure that the value for writtenby in
tblwrittenarticles exists in column userid in table userid.

If you decide to change the numbers in tbluser, this relation will be broken
(and fires a exception/error in most cases.)

However, in some databases you have the option to cascade a change through
all related tables via their foreign keys.

Bottomline: Such an autonumber/serial field is ment to uniquely point to a
certain row, which is handy in a relational database.
It is not ment as a counter...

If you need such a counter, just do it programmatically, like looping over
your resultset (ordered by userid eg), and increase your own counter.

I don't want to lecture you, but I think what you asking for is conceptually
wrong. :-)

Good luck.

Regards,
Erwin Moller
Apr 27 '06 #2
On Wed, 26 Apr 2006 22:24:23 -0700, cover wrote:
After a while of deleting records in a MySQL db, there gets to be the gaps
in the id numbering system. i.e. 1, 2, 3, 6, 7, 12, and so on. Is there a
way to renumber the id system in a table for 1, 2, 3, 4, 5, 6, etc without
manually going in and changing those numbers?


While I generally agree with Erwin on this, it's probably best leave them
alone, I do appreciate there may be a reason for it so...

The only way to do it AFAIK in MySQL (pre version 5 at least) is to loop.
There's no "non-manual" way. In pseudo-code:

$handle1 = open_mysql_connection_and_db();
$handle2 = open_mysql_connection_and_db();

$result1 = send_mysql_query("SELECT ID FROM Table1", $handle1)
$ID=1;
while ($row = get_mysql_row($result1)) {
if ($row[ID] != $ID) {
send_mysql_query("UPDATE Table1 SET ID='$ID' WHERE ID='$row[ID]'",
$handle2);
}
$ID++;
}

It's not pleasant and you'll need to trigger it after deleting a record.

I also use pseudo-code above, partially because I use my own database
abstraction object and can't remember off-hand what the proper mysql
functions are (and am too busy to look them up at the minute) and
partially because most people also use some form of database abstraction
object (PDO etc) so you'd need to rewrite it to fit in with that anyway.

I also have classes generated for tables that have events for beforeDelete
and afterDelete so I'd use an afterDelete event to compact the ID numbers.

But, as I said at the start, be very aware of Erwin's comments they are
likely true so be sure you want to do this before doing it.

Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Apr 27 '06 #3
On Thu, 27 Apr 2006 08:53:33 GMT, Andy Jeffries
<ne**@andyjeffries.co.uk> wrote:

While I generally agree with Erwin on this, it's probably best leave them
alone, I do appreciate there may be a reason for it so...

Thanks guys - appreciate the informative replies.
May 5 '06 #4
Try to avoid situations where you need and autonumber id field to be sequencially perfect. However
you might be dealing with an odd situation.

I found that the best thing to do. Is to do a table backup. You don't have to back up the entire db
just the table in question.

first do a full backup of your table.
then do a data export of the same table. WHen exporting data I like to use the ~ as the separator
because the , may be contained in some data fields and scews up the field separation.
next open up that exported data in excel. Go to tools > import text file make sure to tell excell
the separator is ~
you will see all your data - hopefully the primary id autoincrement is the very first column. Do a
sort on that column adn then delete that column, save that file
now you delete or drop table from your db.
Then open up the saved exported table in a word processor and copy the sql that build the table
structure, copy and paste that code into the SQL section of phpadmin and go, this will recreate the
structure of the table.
Next go to the bottom of that page and you will see, insert text file, select that and you are
taken to a page/form to upload data. Make to set ~ as the separator and lower on the page select the
checkbox that says "local" or the upload sometimes won't work.
Then your data is reinserted and autonumberd starting from 0.

Note: if you have a huge amount of data in the data text file, you can only upload 2 megs at a time.
In situations like that, use you editor to split the file in half and upload each section.

This process is easy to do, sounds much worse that it really is, However there is potential for
disaster. Try practising on a home server first. get it down. If all else fails at least you have a
full and complete back up of the data and you can dump that table back and get back to where you
started.

On Wed, 26 Apr 2006 22:24:23 -0700, cover <co****************@yahoo.com> wrote:
After a while of deleting records in a MySQL db, there gets to be the
gaps in the id numbering system. i.e. 1, 2, 3, 6, 7, 12, and so on.
Is there a way to renumber the id system in a table for 1, 2, 3, 4, 5,
6, etc without manually going in and changing those numbers?


May 6 '06 #5
Gleep wrote:
Try to avoid situations where you need and autonumber id field to be sequencially perfect. However
you might be dealing with an odd situation.

I found that the best thing to do. Is to do a table backup. You don't have to back up the entire db
just the table in question.

first do a full backup of your table.
then do a data export of the same table. WHen exporting data I like to use the ~ as the separator
because the , may be contained in some data fields and scews up the field separation.
next open up that exported data in excel. Go to tools > import text file make sure to tell excell
the separator is ~
you will see all your data - hopefully the primary id autoincrement is the very first column. Do a
sort on that column adn then delete that column, save that file
now you delete or drop table from your db.
Then open up the saved exported table in a word processor and copy the sql that build the table
structure, copy and paste that code into the SQL section of phpadmin and go, this will recreate the
structure of the table.
Next go to the bottom of that page and you will see, insert text file, select that and you are
taken to a page/form to upload data. Make to set ~ as the separator and lower on the page select the
checkbox that says "local" or the upload sometimes won't work.
Then your data is reinserted and autonumberd starting from 0.

Note: if you have a huge amount of data in the data text file, you can only upload 2 megs at a time.
In situations like that, use you editor to split the file in half and upload each section.

This process is easy to do, sounds much worse that it really is, However there is potential for
disaster. Try practising on a home server first. get it down. If all else fails at least you have a
full and complete back up of the data and you can dump that table back and get back to where you
started.

On Wed, 26 Apr 2006 22:24:23 -0700, cover <co****************@yahoo.com> wrote:

After a while of deleting records in a MySQL db, there gets to be the
gaps in the id numbering system. i.e. 1, 2, 3, 6, 7, 12, and so on.
Is there a way to renumber the id system in a table for 1, 2, 3, 4, 5,
6, etc without manually going in and changing those numbers?



This procedure doesn't work if you have foreign keys. You need to change those,
also.

And yes, having holes in the sequence should not be a problem.

BTW - this should be in comp.databases.mysql - it's not a PHP question.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Pjotr Wedersteers | last post by:
I have a table with quiz questions. Each question has a unique ID, based on an auto_increment field. Now I've discovered that with deleting rows from the table the deleted ID's are not reissued....
19
by: Grant Edwards | last post by:
I've decided to learn wxPython, and I'm afraid I just don't grok the whole "id" thing where you have to pull unique integers out of your, er, the air and then use those to refer to objects: ...
6
by: Marlene | last post by:
Hi All I have the following scenario, where I have found all the duplicates in a table, based on an order number and a part number (item).I might have something like this: Order PODate Rec...
1
by: ColinWard | last post by:
I have the following loop which goes through and adds items to a listbox. the items have been selected by the user from a file open dialog. Right now all that shows up is the filename. I would like...
2
by: George | last post by:
The flow of my app is as follows 1) Pull up an order and change the quantity of a textbox item. (Ex: change the quantity from 1 to 2 2) Click on the Update button 3) When the page posts back you...
4
by: John Sutor | last post by:
I need some code that, on each keyup event, will take all of the numbers typed into the text box and format as they type to look like this 100 1,000 10,000 100,000 1,000,000 John S
14
by: Lars Netzel | last post by:
A little background: I use three Datagrids that are in a child parent relation. I Use Negative Autoincrement on the the DataTables and that's workning nice. My problem is when I Update these...
5
by: sparks | last post by:
We have a database with 1600+ records in it. They are linked across 7 tables by an ID type autonumber. Now we want(not I want LOL) to remove the first 1200 records no problem but they also...
13
by: deko | last post by:
http://www.liarsscourge.com/ewpi.html The list numbers should be formatted as <h3>, but other text within the <li> should be normal text. <ol> <li> <h3 id="d2">Download latest WordPress...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.