Re: Question: Correcting MySQL's ID colomn when removing an entry

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Sep 20, 2009 at 12:07:39PM +0430, Parham Doustdar wrote:

> Hello there,
> I'm guessing that when a row in a MySQL table is removed, the ID colomns of the rows which come after that row are not changed. For example:
> 1
> 2
> 3
> 4
> 
> Now, if I want to remove the third rows, the ID colomn would be something like:
> 1
> 2
> 4
> 
> I was wondering if there was a way to fix it through a query, so I wouldn't have to use a for statement in PHP to fix it?
> 

There's nothing to fix here. I assume you're talking about an ID column
which is serialized or "autoincrement". Such columns are not designed to
necessarily be "continuously" numbered. If you remove a row, there is
simply a gap in the sequence. Your application shouldn't care whether
the numbers are continuous or not, though it may care that they are "in
order".

To process such a table, you'd do your query like:

SELECT * FROM mytable ORDER BY id

And then process the results with a foreach or for loop.

I will say, though, that if your application cares about the ordering of
the records, I'd use a different field to implement that, such as
"date_added" or "last_name" or something similar. It's best to assume,
when dealing with autoincrement fields, that the numbers placed in them
are random. They are only designed to be unique within that table, to
allow for easy fetching of a single unique record within the table.
Autoincrement fields are not designed to be used to order the records in
a table.

Paul

-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux