views:

795

answers:

11

For example if I have an auto-numbered field, I add new records without specifying this field and let DB engine to pick it for me.
So, will it pick the number of the deleted record? If yes, when?

// SQL Server, MySQL. //

Follow-up question: What happens when DB engine runs out of numbers to use for primary keys?

+12  A: 

NO. numerical primary keys will not reused, except you specify them manually(you should really avoid this!)

Peter Parker
+2  A: 

Depends on the auto-numbering system. If you're using a sequence of any kind, the numbers of deleted records will not get reused, as the sequence does not know about them.

Elie
+2  A: 

Generally, no, the numbers are not reused.

However, you can -- in products like Oracle -- specify a sequence generator which cycles around and will reuse numbers.

Whether those are numbers of deleted records or not is your applications's problem.

S.Lott
+1  A: 

Not specifically. If the key is being read from a sequence or autoincrementing identity column the sequence will just plug along and produce the next value. However, you can deactivate this (set identity_insert on on SQL Server) and put any number you want in the column as long as it doesn't violate the uniqueness constraint.

ConcernedOfTunbridgeWells
+1, with a minor correction/caveat for SQL Server - to insert a value into the identity column, SET IDENTITY_INSERT ON has to be specified before the explicit identity value is inserted (see http://msdn.microsoft.com/en-us/library/ms188059.aspx).
Lieutenant Frost
Thanks. Well spotted.
ConcernedOfTunbridgeWells
+1  A: 

This question needs to be made more precise:

... "with Oracle Sequences"

... "with MySQL autonumber columns"

... etc...

Mark Renouf
I've specified two engines that I'm interested in.
z-boss
+1  A: 

As long as you create the table correctly you will not reuse numbers. However you can RESEED the identity column (IN MSSQL anyway) by using the following:

-- Enter the number of the last valid entry in the table not the next number to be used

DBCC CHECKIDENT ([TableName], RESEED, [NumberYouWantToStartAt])

This is of course insane... and should never be done :)

Dining Philanderer
I wouldn't say it should NEVER be done. In the right situation, it is entirely appropriate.
Kevin
Sorry I wrote that last line in jest...You are correct that this is useful sometimes.
Dining Philanderer
+1  A: 

MySQL will not reuse IDs unless you truncate the table or delete from the table with no where clause (in which case MySQL, internally, simply does a truncate).

Lucas Oman
+3  A: 

AFAIK, this could happen in MySQL:

How AUTO_INCREMENT Handling Works in InnoDB:

InnoDB uses the in-memory auto-increment counter as long as the server runs. When the server is stopped and restarted, InnoDB reinitializes the counter for each table for the first INSERT to the table, as described earlier.

After a restart of server. Innodb reuse previously generated auto_increment values. :

Suggested fix: innodb table should not lose the track of next number for auto_increment column after restart.

Martin Bøgelund
A: 

Yeah, it really depends on the way you generate the id.

For example if you are using a GUID as the primary key, most implementations of getting a random new Guid are not likely to pick another guid again, but it will given enough time and if the Guid is not in the table the insert statement will go fine, but if there is already a guid there you will get a primary key constraint violation.

Nat
A: 

I consider the MySQL "feature" of reusing id's a bug.

Consider something like processing of file uploads. Using the database id as a filename is a good practice : simple, no risk of exploits with user-supplied filenames, etc.

You can't really make everything transactional when the filesystem is involved... you'll have to commit the database transaction then write the file, or write the file and commit the database transaction, but if one or both fail, or you have a crash, or your network filesystem has a fit, you might have a valid record in the database and no file, or a file without a database record, since the thing is not atomic.

If such a problem happens, and the first thing the server does when coming back is overwrite the ids, and thus the files, of rolled back transactions, it sucks. Those files could have been useful.

peufeu
A: 

no, imagine if your bank decided to re-use your account_id - arghhhh !!

f00