Once a record is deleted, its Autonumber value can be reused. Even with a
standard incremental Autonumber, a value can be reused if the highest-numbered
value is deleted, then the database is compacted. For this reason, no kind of
Autonumber is suitable for use as a logical key.
If, however, the Autonumber value will only be used as a surrogate key within
the database app and will never be used as an external refrence, and if the
database makes good use of enforced referential integrity rules, there should
be no consequences to possible reuse of previously deleted Autonumber values.
So - you may frequently want to use Autonumer keys as surrogate keys, but use
another technique other than autonumber to generate unique logical keys. A
good logical key might be a string representation of a new GUID obtained using
a Windows API call. Another option I rather like for building unique logical
keys is CCur(Format(Now,"yyyymmdd") & Format(Timer*10,"000000.00") &
Format(Rnd*100,"00")), making sure to call Randomize at some point prior to
executing this code. If, for some reason, you'd like make sure records will
sort randomly using the logical key, change that formula to
CCur(Format(Rnd*100,"00") & Format(Timer*1000,"00000000") &
Format(Now,"ddmm.yyyy")).
If you need more assurance that the random number part won't be duplicated
during the same millisecond, you can replace the call to Rnd with a call to a
custom non-repeating hash procedure and seed its initial counter with a random
value using Rnd. An easy hash (though not very random if that matters) is to
count from 1 and XOR the counter value with the random seed. To be fancier,
you could use modulus multiplication or exponentiation, though that can be
tricky in VB/VBA.
On 6 Dec 2004 13:16:43 -0800, "yf" <yf******@gmail.com> wrote:
A KB article
"http://support.microsoft.com/default.aspx?scid=kb;en-us;209599" tells
that the maximum number of records that a table may hold if the PRIMARY
key data type is set to AUTONUMBER is 4,294,967,295.
Suppose the PRIMARY key data type is set to "RANDOM" AutoNumber.
Suppose an application
(a) successfully INSERTS "X" records, then
(b) successfully DELETES "Y" records (X >= Y), then
(c) successfully INSERTS "Z" records to a table to the capacity limit
of that primary key (i.e. insertion of a "Z+1"th record would have
caused a "duplicate primary key" condition).
Suppose there are no hardware or software failures and unlimited
HDD/RAM space.
Is it guaranteed that (X-Y+Z) = 4,294,967,295 ?
I.e. will Random AutoNumber reuse all "holes" left after deletion of Y
records?