You are invited to Log in or Register a free Frihost Account!

A problem about numbering in database

Just take wordpress for example,
In wordpress blog,
every comments are given a ID number,
it has something like this in the html source,
<li id="comment-18">

which is used to easily locate the comment.
Let's say there are 18 comments in total, if you add a comment, this one will be numbered "comment-19", that's understandable.
but the problem is,
even if the administrator deleted all the 18 comments, and then you add a new comment, this one would still be numbered "comment-19", there is no comment at all before adding the new one!
Why is that? why wouldn't it be "comment-1" instead?
So, there must be something left about the information of the number of the comments!
I'm not clear about how this numbering works...
I think this is because mySql have ID's. I don't know about wordpress but I have similar situation when I need different id's for one image gallery and I am using the same id for div like ID in mysql. Of course, html is build automatically with php script. When I delete some rows in mysql table the ID's are not changed and any div with image have same image and same id like before deleting.

sonam is right. Often each row in a database table have a unique ID that never change. The ID can be used to lookup the data stored at a certain row. In your case the table would be the list of all the comments and a row would be a certain comment.

To keep all the IDs in sequence after removing a row you would have to decrement the ID of all the rows with higher ID by one. Other tables might store IDs to refer to certain rows in the table so they would have to be updated as well. This would make it much harder to keep the data consistent and it would also slow things down quite a lot.

You probably don't want to change the ID from the user's point of view either because you can have links to certain comments by appending #comment-X, where X is the ID of the comment, to the URL. If the ID was changed it would make links refer to the wrong comment.
You are confusing the database primary key, or for in a broader sense, the register identifier (Comment ID) for a simple numeric index.

the ID will be used , in any database, as a unique identifier of that register, the comment with the ID 10, must keep the id, even if the ID 9 comment is deleted, because the id is used to reference this register in the whole database, in the comment example , lets say the comment had a response, the response must store the comment ID so we may know which comment the response is related to. We could regenerate the keys in simpler databases, but it would siply be a waste of resources.

You may achieve a sequential index by not using the ID but counting the iteration on the comment selection loop and concatenating the counter to the html id instead of the ID of the register from the database.... generally its not worth it unless the order of the registers matters for some UI reason.
because they use seq, which is auto increment integer as a surrogate key for the table. easy, confirm unique and as added bonus, most database will have "last added index" function which will return the seq.
Related topics
post nuke problem
database problem
Database Problem
I can't upload my Database SQL script ! Helllppppp!!!!
Critical Database Error (Or something else)
mysql problem
Importing an existing database...
Whats the problem?
Mysql database problem
Is There A Good Database-less CMS?
ASP Problem
[URGENT]Simple authentification does not work!!!
Problem with my blog/database
Problem with creating a database -- help :(
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

© 2005-2011 Frihost, forums powered by phpBB.