|
-
October 10th, 2009, 11:42 AM
#1
auto increment problem.
Hello. I have a SQL code like this:
Code:
CREATE TABLE IF NOT EXISTS `test`(
`id` int(10) NOT NULL AUTO_INCREMENT,
`test2` varchar(15) NOT NULL,
`test3` varchar(15) NOT NULL,
PRIMARY KEY(id)
)
Each time I add a new data it will get id 1 and if I add more it will become id 2. That's correct. But whenever I delete one of them, it doesn't down one down, so if I have added 5 and deleted number 3 and added a new one, that one will become number 6. But there is no id 3, so how can I make it move all the higher numbers one down if a lower number is deleted. Since a list like this is pretty dumb:
1
2
4
5
6
- It should be like this:
1
2
3
4
5
- realchamp.
-
October 10th, 2009, 02:28 PM
#2
Re: auto increment problem.
What database server are you using? it becomes little difficult to help unless we know which database you are using. Assuming that it is SQL Server, you should be looking at DBCC CHECKIDENT
-
October 10th, 2009, 03:33 PM
#3
Re: auto increment problem.
 Originally Posted by Shuja Ali
What database server are you using? it becomes little difficult to help unless we know which database you are using. Assuming that it is SQL Server, you should be looking at DBCC CHECKIDENT
I am using MySQL.
-
October 11th, 2009, 03:12 AM
#4
Re: auto increment problem.
I would like to know why you want to have/need this behavior? Because it isn't the usual need for such an "id" column that it removes "holes" like this.
So my guess is that there's an easier way to handle the actual problem then recreating the id column after each delete.
I don't know how to do it in MySQL, but usually in my experience when there is a need for creative solutions like this, that the actual problem is something else
-
October 12th, 2009, 04:43 AM
#5
Re: auto increment problem.
 Originally Posted by Alsvha
I would like to know why you want to have/need this behavior?
Me too.
realchamp, have you consider the case when this primary key is the foreign key in other tables? (perhaps not now but in the future)
Maintaining the primary keys with no "holes" in this table means that you will have to change the referencing tables as well (not the removed key, obviously because to be able to remove it it should not be referenced by other tables).
Also think of the impact on the performance of the database as well, having to change lots of primary keys (= indexes); with or without tables referecing this key the performance will be deteriorated.
-
October 12th, 2009, 08:30 AM
#6
Re: auto increment problem.
 Originally Posted by jcaccia
Me too.
realchamp, have you consider the case when this primary key is the foreign key in other tables? (perhaps not now but in the future)
Maintaining the primary keys with no "holes" in this table means that you will have to change the referencing tables as well (not the removed key, obviously because to be able to remove it it should not be referenced by other tables).
Also think of the impact on the performance of the database as well, having to change lots of primary keys (= indexes); with or without tables referecing this key the performance will be deteriorated.
I've found another solution, else thanks.
-
October 29th, 2009, 10:47 PM
#7
Re: auto increment problem.
Please let us know what solution you found.
-
December 9th, 2009, 11:08 AM
#8
Re: auto increment problem.
I'm not really understanding what you are asking, but if you are trying to resuse auto incroment values, the answer is simply, you can't.
Even if you delete a record you cannot recover that auto incroment value and use it again in another record. The PK only incroments it will never reuse an ID.
-
December 11th, 2009, 04:01 AM
#9
Re: auto increment problem.
 Originally Posted by Mr. Geeker
I'm not really understanding what you are asking, but if you are trying to resuse auto incroment values, the answer is simply, you can't.
Even if you delete a record you cannot recover that auto incroment value and use it again in another record. The PK only incroments it will never reuse an ID.
Well - you can, but it'll require a lot of work which aren't worthwhile.
-
December 11th, 2009, 10:29 AM
#10
Re: auto increment problem.
 Originally Posted by Alsvha
Well - you can, but it'll require a lot of work which aren't worthwhile.
Not sure how you plan on doing that, even if you figure out which numbers arent being used, there is no way to reset the database back and use them. The "auto"incroment values are "auto"matic.
There's no way (at least in MS SQL and probably not in MySQL either) to tell the database to reuse old numbers, and you can't manually insert data into a auto incromenting field.
I don't understand what the big deal is though, if this is a problem the database isn't designed correctly. If your worried you will run out of numbers, then you are using the wrong data type, you need something more custom, or use a Guid.
-
December 14th, 2009, 06:58 AM
#11
Re: auto increment problem.
 Originally Posted by Mr. Geeker
Not sure how you plan on doing that, even if you figure out which numbers arent being used, there is no way to reset the database back and use them. The "auto"incroment values are "auto"matic.
There's no way (at least in MS SQL and probably not in MySQL either) to tell the database to reuse old numbers, and you can't manually insert data into a auto incromenting field.
<snip>
Well - "identity_insert on" and "reseed" should take you a long way.
Last edited by Alsvha; December 14th, 2009 at 07:01 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|