I have the ID field setup on my table as auto_increment, primary key, and unique. However, when I add new items to the database, the ID is not always the next available number. Like when I delete some records (say 4 through 6) instead of starting at 5, it starts at 7. How can I prevent this?
So should I just shut autonumber off and find out the next number through code? I just don't want huge gaps in my ID numbers, 1, 2, 8, 14, 15, 16, 22. You know?
when you insert a new record, the id will be 6 if you get the ID of the last record and add 1 still, the same as if you just let auto_increment take care of it.
If you only ever delete records at the end of the table, then your method would work so yes you should turn auto_increment off, but it won't make any difference if you delete records in the middle of the table.
just go in and re-number the id number. if there is only a 11 then it should be pretty fast.
if you turn off autoincrement and get the number from the last id yourself, how is that going to make the gaps smaller if you are still deleting form the middle? sorry just curious.
Originally posted by scoutt just go in and re-number the id number. if there is only a 11 then it should be pretty fast.
I expect that database to grow in time.
Originally posted by scoutt if you turn off autoincrement and get the number from the last id yourself, how is that going to make the gaps smaller if you are still deleting form the middle? sorry just curious.
Because currently, the next autoindex is like 23, while my code to get the last id will give me 10. Add one, the next number is 11. See what I'm saying? The autoindex is already way up because of how many I've deleted.
I don't think it works that way. you will get conflicts on that number already being used. if you get total rows, say 11, then you add one to it, mysql is going to say that that id is already in use (duplicate). you will start having problems when you DB gets bigger. say you have 500 rows and you delete # 250 so now you have 499, but you have an id of 500. if you get the total rows then add one it will give it an id of 500. see it will give you an error since there is already an id. even if you used last_ID or whatever it is, it will give you the last one, order or not.
if I am way off base here let me know, you might have to explain to me like I'm a 3rd grader,
No, it's not filling the holes (if you could think of a way to do that, that'd be awesome), but it is preventing a good portion of them. Like I said, the next autoindex is 23, my code will give 11. So it's like autoincrement, but smarter. It will use the numbers twice if the number no longer exists (only at the end of the number set).
The next AutoIndex is 23. Meaning that if I let autoincrement decide the ID, it would put in 23. But since the ID of the last record in the table is 10, my code gets that, increments it, and gives the new record the ID 11. In this process, AutoIndex will increment to 24. Even though my code overrides it, it still increases.
I wish I could show you. It'd all make sense fast.
This is in phpMyAdmin. The next autoindex is at 18, but I only have 4 rows (the id of the last row happens to be 4 to, so this one doesn't have holes yet).
If I were to let autoincrement chose the ID, I would have 1, 2, 3, 4, 18...but with my code, I get 1, 2, 3, 4, 5.
no I see what you are saying. its funny because my test machine at home that has apache and php and mysql on it doesn't do that. if I have a last id of 10 and I delete it, the table will still insert #10, but on my site if I did that it would not increment the same.
so, if your table has an ID of 10, how is autoindex going to return 23? if the last id is 10 it should autoindex 11 not 23. if that is the last row in the table. but if you have anything higher than that higher up in the table it will return 23 or whatever.
1
3
23
5
even though the last id is 5 the next autoindex is 24. make sure you script doesn't do that as it will return 23 instead of 5
what version do you have? mine doesn't have that one and I have 2.2.4
if that is the case then mysql is remembering the last id that was in there but had been deleted. my site doesn't do that. if I deleted #4then the next one is #4 not 5. very strange
that won't matter. it is mysql that is messin gup. I was jsut curious as to where you got the screen shot from since it is not on my version phpMyAdmin.
I thougth I did. you can't insert data in a row that has been deleted or is not there. the way you are going, that is up to you. personally I would leave it at auto-increment. but to each his own I guess.
I have another site that has something like what you are doing. somebody can insert articles and it goes in the database. that person can have it set to 1 week up to 3 months. after that time it gets deleted on it's own. it checks everytime the page is loaded and if the dates match it gets deleted. now the first one can get deleted in a week and the last one will take a month. my point is that I don't get holes like that, and it is set to auto-increment.
if your database is going to get real big, then why consider deleteing them? how can it get big if you delete them? if it gets out of hand you can dump it and re number it then load it back. that will take all of 10 minutes. and the site will never know the difference.
that is my opinion and not to be considered your only option.
Originally posted by The Hobo This is in phpMyAdmin. The next autoindex is at 18, but I only have 4 rows (the id of the last row happens to be 4 to, so this one doesn't have holes yet).
If I were to let autoincrement chose the ID, I would have 1, 2, 3, 4, 18...but with my code, I get 1, 2, 3, 4, 5.
I just installed 2.2.6 of phpMyAdmin and that picture you showed is not in 2.2.6. I clicked on properties of a table and it is no where to be found.
you can't insert data in a row that has been deleted or is not there
Why not? The numbers aren't being used anymore. Seems to work perfectly fine to me. Just developing a method to find that missing number is what I can't do (without some kind of big loop)
my point is that I don't get holes like that, and it is set to auto-increment.
The point is, that mine does.
if your database is going to get real big, then why consider deleteing them? how can it get big if you delete them?
Because my site contains snippets and tutorials that users can submit. If a user submits some vulgar thing or something malicous, I need to delete it. Also if things become outdated, etc.
INSERT INTO tablename (id,other,stuff) VALUES (4,'something','something')
Now why don't you, as I already asked, explain to me why that isn't possible? Or do we need to go back and forth for a few more posts before you get around to telling me?
I'm not saying you're wrong. I'm asking you to tell me why it's not possible. Do you understand?
sure. how are you going to tell that there is no #4? you will have to hard code it and then even if you get it to go it will go to the end of all rows, not after 3. I will run a test on a table that is setup like this and see if it does. I'll get back to you in a few, or monday
Originally posted by scoutt sure. how are you going to tell that there is no #4?
That's what I was asking you. If there was a way to do it without looping through and finding the first hole. Maybe I could create a table that would store the next available ID for all the other tables. When an item is deleted, it will specify that as the next number. When one is added in it's place, it'll get the ID after the next. Sorry if this is too confusing.
I know it'll still put it at the end, but then I wont have links like:
I see what you are saying and sorry if I thought different. right off hand I don't know of anyway to do what you want. not saying there might be way, I just don't know.
how do you plan on getting the correct row if you don't specify an id in the url? specifing and id is better then the articles name or something.
I meant that I don't want spaces between my numbers, 1, 2, 4, 8, 12, but 1, 2, 3, 5 instead. I'll give what I said a try:
If an item is deleted from the table, the deleted ID number is saved in another table so it can be used again. Once it is used, it will get the last id + 1, like I said above. That way I can always know what the ID should be.
If more than one item is deleted, I'll add it as well, so I'll have:
3,14,12
and once they are all used up, just go to the last id + 1.