Restart Autonumber

T

Traci

If I have a table with an autonumber primary key and 100 records and I delete
the last 50 records, the next record added would have a primary key of 101. Is
there any way to have the primary key start at 51 after the last 50 records are
deleted?

Thanks!

Traci
 
D

Douglas J. Steele

That's the way autonumbers are. In fact, you're lucky that the numbers
currently correspond to the record number: had you started to add a record
and then decided not to save it, you would have lost that number. Also, if
you were to use Access Replication, be aware that the autonumber values
would no longer be sequential, but random.

Autonumber fields really aren't intended for "human consumption": their only
intent is to provide a (practically guaranteed) unique value that can be
used as a primary key, and they fill that purpose with or without gaps.

If, after reading all this, you still want to try and use autonumbers and
have their value mean something, you can compact the database after doing
the delete, and the next autonumber assigned should be one more than the
last one used.
 
R

Reggie

Actually, This will not recover the autonumbers. It will pick up right from
the last autonumber before the delete.
 
J

Jeff Boyce

Traci

In addition to the cautions noted by Doug in his response, be aware that if
your table's primary key (?your Autonumber) is used as a foreign key in
one/more child tables, "restarting" your Autonumbers could hose the
relationship between parent and child records.
 
J

James

I have been reading many posts by users that claim to have years of
experience with Access that promote the use of Autonumber generated
values as primary keys. Don't get me wrong; I use Autonumber values as
primary keys for standalone tables, such as those that really do not
have a primary key unless you use multiple key fields. But I keep
asking myself why anyone would create an architecture of two related
tables upon which the relationship is based on an Autonumber key
field. I'm currently working on one right now where I'm converting an
"Orders" table that uses Autonumber to generate Order numbers. This
Autogenerated value is then stored in the OrderDetails records. If the
scenario that you describe below occurs, guess what? You've just lost
your relationship between the two fields, because you cannot enter
your own values in an Autonumber field, and your OrderDetails table is
useless because the reference to the Orders table is gone. The
OrderDetails table will use an Autonumber key to basically serve as a
surrogate primary key, but I use a "natural" key that I control -
which in this case is my own "rolled" invoice #. The database I'm
converting is going to use my own "rolled" method, upon which a
function will return an incremented value from a table. With the
experience I've had with tables related based on Autonumbers, I will
never willingly design one to be as such.
 
M

Mike Preston

I have been reading many posts by users that claim to have years of
experience with Access that promote the use of Autonumber generated
values as primary keys. Don't get me wrong; I use Autonumber values as
primary keys for standalone tables, such as those that really do not
have a primary key unless you use multiple key fields. But I keep
asking myself why anyone would create an architecture of two related
tables upon which the relationship is based on an Autonumber key
field.

Because there is absolutely no way that you can define a natural key
that will never change, nor one that is guaranteed to be unique
throughout all of time. Autonumbers are both as long as the
appropriate RI have been established. At least they are supposed to
be - there was a bug in Access 2000 I believe, that has since been
fixed, where autonumbers were duplicated, but that was a bug.
I'm currently working on one right now where I'm converting an
"Orders" table that uses Autonumber to generate Order numbers. This
Autogenerated value is then stored in the OrderDetails records. If the
scenario that you describe below occurs, guess what? You've just lost
your relationship between the two fields, because you cannot enter
your own values in an Autonumber field, and your OrderDetails table is
useless because the reference to the Orders table is gone. The
OrderDetails table will use an Autonumber key to basically serve as a
surrogate primary key, but I use a "natural" key that I control -
which in this case is my own "rolled" invoice #. The database I'm
converting is going to use my own "rolled" method, upon which a
function will return an incremented value from a table. With the
experience I've had with tables related based on Autonumbers, I will
never willingly design one to be as such.

There have been many threads over the years on this issue. People
line up on one side or the other and have strong feelings about the
mental acuity of those that feel differently. ;-)

But either system works as long as there are enough safeguards built
into the programming. It just takes fewer safeguards if an autonumber
is used, IMO.

mike
 
J

Jeff Boyce

James

Not sure how this relates to what I interpreted as the original point -- how
can I restart my numbering system? My caution was that any time you
re-initiate a primary key sequence (whether an Autonumber or a "hand-rolled"
custom ID function), you risk orphaning "child" table rows (at best), or
causing totally unrelated child table rows to be connected to the wrong
"parent".

And if there are "child" records, neither an Autonumber or a "hand rolled"
ID function should resequence to "fill in" for missing values in the
sequence. Unless, of course, you have the additional procedures to modify
ALL the child records!

The topic of "natural" keys vs. arbitrary, unique IDs does show up a lot --
and I suspect the discussions border on "religious beliefs". But I'm not
sure that a custom-built procedure that generates a sequential, unique
identifier could be consider a "natural" key.
 
J

John Baker

James said:
I have been reading many posts by users that claim to have years of
experience with Access that promote the use of Autonumber generated
values as primary keys. Don't get me wrong; I use Autonumber values as
primary keys for standalone tables, such as those that really do not
have a primary key unless you use multiple key fields. But I keep
asking myself why anyone would create an architecture of two related
tables upon which the relationship is based on an Autonumber key
field. I'm currently working on one right now where I'm converting an
"Orders" table that uses Autonumber to generate Order numbers. This
Autogenerated value is then stored in the OrderDetails records. If the
scenario that you describe below occurs, guess what? You've just lost
your relationship between the two fields, because you cannot enter
your own values in an Autonumber field, and your OrderDetails table is
useless because the reference to the Orders table is gone.

There's nothing wrong and most things right with Autonumber as a primary
key. The mistake is when people try to do the thing you're talking
about. Once a primary key has been set, it should NEVER change, short
of some sort of massive system-wide upgrade (and then rarely).
 
B

Bernard Peek

James said:
I have been reading many posts by users that claim to have years of
experience with Access that promote the use of Autonumber generated
values as primary keys. Don't get me wrong; I use Autonumber values as
primary keys for standalone tables, such as those that really do not
have a primary key unless you use multiple key fields. But I keep
asking myself why anyone would create an architecture of two related
tables upon which the relationship is based on an Autonumber key
field.

What you have to bear in mind is that there's a difference between a
table and an entity. Entities are in the logical data structure and
tables are in the physical data structure.

Whenever there's a difference between the logical and physical data
structures there is a risk of data corruption.

Joe Celko says that autonumbers don't exist in the logical data
structure, I disagree. I think that autonumbers appear in the LDS as
separate entities with a 1:1 relationship to another entity. The problem
is that that 1:1 relationship isn't inherent in the data, it has to be
enforced by the programmer. If that isn't done then you get situations
where, for instance, a customer's order is entered twice and gets two
different autonumber "Order Numbers."

For that reason it's safest to use a natural key if there is one
available. Adding an autonumber doesn't create a natural key of there
isn't already one in the real data. If there is one already then the
autonumber is probably redundant.

There are times when you need to denormalise the table for performance
reasons, and that may involve adding an autonumber. With current
database systems you are less likely to need this, storage is so cheap
that it's often better to duplicate key fields (even compound keys) than
to add an autonumber.

The most difficult situations arise when you deal with data about
people. There is no simple and easy to use natural key for people. Over
in comp.databases.theory I once suggested that the precise latitude,
longitude, altitude and time of birth would make a good natural key if
we could persuade people to record it.

I'm currently working on one right now where I'm converting an
"Orders" table that uses Autonumber to generate Order numbers. This
Autogenerated value is then stored in the OrderDetails records. If the
scenario that you describe below occurs, guess what? You've just lost
your relationship between the two fields, because you cannot enter
your own values in an Autonumber field, and your OrderDetails table is
useless because the reference to the Orders table is gone.

That's called an update anomaly, and it's expected when you work with
denormalised data. It's up to the programmers to build system where that
type of anomaly can never occur.

There's also an insert anomaly when the same order gets entered twice.
Again, it's up to the designers to build systems where that can't
happen.

So my advice is take a good long look at the logical data structure and
try very hard to find a natural key. Use that if you can, and only use
autonumbers if you are forced to. There are two reasons for doing that.

The first is performance, to avoid having to match long key fields or to
avoid storing long fields both as primary key and as foreign keys.
Modern systems mean that these cause less problems than they once did.

The seconds is that sometimes you don't have a natural key available,
such as when you are dealing with data about people. At these times you
just have to accept that your data is likely to have errors in it, and
there are limits to what you can do about it. You can choose to use
someone else's autonumber field (for instance a social security number)
or create your own. When you create your own then you need to put
procedures in place to make sure that there is always an exact 1:1
relationship between your autonumber field and the people it is supposed
to identify. You can't do that in software.
 
J

James

Because there is absolutely no way that you can define a natural key
that will never change, nor one that is guaranteed to be unique
throughout all of time. Autonumbers are both as long as the
appropriate RI have been established. At least they are supposed to
be - there was a bug in Access 2000 I believe, that has since been
fixed, where autonumbers were duplicated, but that was a bug.

Is there really anything in life that is guaranteed to NOT change? I
supposed there are a few things, like the race of a person. You might
think social security #'s, but people even receive new of those from
time to time. ;-)

My IT manager at work even goes so far as to say that PrimaryKeys
should be totally meaningless in a table; in other words, if your PK
is made up of any part of your data, then you are not using a correct
PK. I had to kindly disagree with him ;)
 
C

--CELKO--

Joe Celko says that autonumbers don't exist in the logical data
structure, I disagree. I think that autonumbers appear in the LDS as
separate entities with a 1:1 relationship to another entity. >>

I am living in Salt Lake City, so LDS has another meaning to me :)
data, it has to be enforced by the programmer. <<

Exactly right: autonumberings is not in the data. It is created
outside the reality of the data model in the PHYSICAL storage;
unverifiable, random, irreproducible and proprietary. Would you take
a drug that uses that kind of "science"? No. So why do you run
databases like that?
entered twice and gets two different autonumber "Order Numbers." <<

It is worse if there is a real key; one UPDATE uses the autonumber and
another uses the real key. If I were using two reald keys -- say,
Dewey Decimal and Library of Congress in a library -- I can
co-ordinate them. But with a phyasical locator, I am screwed.
time of birth would make a good natural key if we could persuade
people to record it.<<

That (location and birthdate) is the Swedish nation id number!
 
J

John Nurick

Is there really anything in life that is guaranteed to NOT change? I
supposed there are a few things, like the race of a person.

In practice "race" is an administrative classification and therefore
subject to change.
 
M

Mike Preston

Is there really anything in life that is guaranteed to NOT change?

No, there isn't. Thanks for making the point for me. ;-)

I
supposed there are a few things, like the race of a person. You might
think social security #'s, but people even receive new of those from
time to time. ;-)

My IT manager at work even goes so far as to say that PrimaryKeys
should be totally meaningless in a table; in other words, if your PK
is made up of any part of your data, then you are not using a correct
PK. I had to kindly disagree with him ;)

And you did so on what grounds again?

mike
 
B

Bernard Peek

--CELKO-- said:
structure, I disagree. I think that autonumbers appear in the LDS as
separate entities with a 1:1 relationship to another entity. >>

I am living in Salt Lake City, so LDS has another meaning to me :)

I did think of that when I wrote it.
data, it has to be enforced by the programmer. <<

Exactly right: autonumberings is not in the data. It is created
outside the reality of the data model in the PHYSICAL storage;
unverifiable, random, irreproducible and proprietary. Would you take
a drug that uses that kind of "science"? No. So why do you run
databases like that?

entered twice and gets two different autonumber "Order Numbers." <<

It is worse if there is a real key; one UPDATE uses the autonumber and
another uses the real key. If I were using two reald keys -- say,
Dewey Decimal and Library of Congress in a library -- I can
co-ordinate them. But with a phyasical locator, I am screwed.

It's quite possible for there to be two natural keys in an entity, but
neither the Dewey nor LoC codes are natural. They are both examples of
autonumbers that have become visible outside the systems that created
them.

In an entity where there are two or more natural keys it is unlikely
that either could change independently of the other, because either key
uniquely identifies an object. If a key changes then the object it
identifies changes.

time of birth would make a good natural key if we could persuade
people to record it.<<

That (location and birthdate) is the Swedish nation id number!

They are part of the data that is used to generate the British
equivalent of the US social security number (SSN), it's called the
National Insurance number or NINO. There have been a few incidents where
people with the same name have been born in the same place on the same
day, and have been given identical NINOs.

That illustrates a point about autonumbers. If you trust the autonumber
issued by some third party you can use them as if they were a natural
key. Some American organisations have used the SSN as a primary key in a
table and discovered that it's not quite good enough to be trusted as if
it really were a natural key.
 
J

John Nurick

Bernard, we're in Humpty-Dumpty country here. You're using the term
"autonumber" in a quite different sense from other people.

For the rest of us, an "autonumber" is a unique value _that the DBMS
itself assigns_ to a _record_ when the record is created, typically
though not necessarily sequential or random, but in any event unrelated
to other attributes of the entity represented by the record.

Attributes such as SSN, ISBN, Dewey or LoC "numbers" are _not_
autonumbers in this usual sense. First, the mechanism by which they are
assigned is outside the DBMS. Second - and in consequence - they are
assigned to the _entity_ (the person, book, subject or whatever), not to
the record representing it. IOW for the purposes of the DBMS they are
natural attributes.

Third, Dewey, LoC and ISBN - I don't know about SSNs - are meaningful
"smart keys" which can be parsed to obtain values of various attributes
of the entity. Generating or assigning subject classifications such as
Dewey and LoC is ultimately a matter of librarians' judgment.
 
M

Mike MacSween

James said:
Is there really anything in life that is guaranteed to NOT change?

PKs are supposed to be stable. That isn't the same as saying they won't ever
change.
I supposed there are a few things, like the race of a person.

Whhhaaattttt? Go on, Mr Anthropologist, define human race for us. And lets
see what the mess we end up in there. Presumably that was your idea of a
joke.
You might
think social security #'s, but people even receive new of those from
time to time. ;-)

Well, if you live in a country with social security numbers. And even in the
USA they have been duplicated, apparently.
My IT manager at work even goes so far as to say that PrimaryKeys
should be totally meaningless in a table; in other words, if your PK
is made up of any part of your data, then you are not using a correct
PK. I had to kindly disagree with him ;)

Ha ha.

Primary keys are a problem. We're 'supposed' to take them from 'natural'
data. Something 'about' the instance of the entity type uniquely identifies
it. Something we could describe in words will make it possible to
distinguish it from another instance of the entity type. You're right, once
you start looking for an absolute guarranteed unique PK it gets a bit
difficult. Especially when we remember that in the relational model PKs
can't include nulls.

So whaddya do? The best you can within the constraints of the fact that the
relational model is an attempt to organise a world of things which isn't
relational.

Is it persons? You could put a unique (maybe even no nulls) index on first
name, last name, DOB (those 3 alone would be pretty strong, depending on
number of records), SSN, 1st Line of address etc. Then use an autonumber as
your 'PK', actually surrogate key.

It's always going to be a compromise.

Mike
 
J

James

-
No, there isn't. Thanks for making the point for me. ;-)

Then why do we worry about establishing a PK that will never change,
especially when the database provided the ability to cascade updates
to all related records?




I

And you did so on what grounds again?


The grounds that I used was the opposite opinion of his; that a PK
should be a representation of the existing data in a table. For
another, if a query a foreign reference table and the PK is one that
makes no sense, then I have to go and join 1 or more tables just to
determine what the data is related to.
 
J

James

And if there are "child" records, neither an Autonumber or a "hand rolled"
ID function should resequence to "fill in" for missing values in the
sequence. Unless, of course, you have the additional procedures to modify
ALL the child records!

My wording was wrong; I didn't mean resequencing on a hand rolled
method, but if the tables have to be recreated, then the relationship
is easily re-established because there are values to related to in
each table, unlike an autonumber field which will contain all new
values.
 
B

Bernard Peek

John Nurick said:
Bernard, we're in Humpty-Dumpty country here. You're using the term
"autonumber" in a quite different sense from other people.

For the rest of us, an "autonumber" is a unique value _that the DBMS
itself assigns_ to a _record_ when the record is created, typically
though not necessarily sequential or random, but in any event unrelated
to other attributes of the entity represented by the record.

An autonumber is created in the database, but what is its counterpart in
the logical data structure which is independent of the database?.
Attributes such as SSN, ISBN, Dewey or LoC "numbers" are _not_
autonumbers in this usual sense.

But they are, the difference is that they are created in someone else's
database.
First, the mechanism by which they are
assigned is outside the DBMS.

It's outside the database which we control. It's in someone else's
database. That is irrelevant when considering the logical data
structure.
Second - and in consequence - they are
assigned to the _entity_ (the person, book, subject or whatever), not to
the record representing it. IOW for the purposes of the DBMS they are
natural attributes.

Natural keys are ones where the 1:1 mapping between the entity and the
database representation of the entity is guaranteed. If you trust the
agencies who generate keys like the SSN then you can treat them as if
they were natural keys, even though they are only autonumber fields in
those agency's own databases.
Third, Dewey, LoC and ISBN - I don't know about SSNs - are meaningful
"smart keys" which can be parsed to obtain values of various attributes
of the entity. Generating or assigning subject classifications such as
Dewey and LoC is ultimately a matter of librarians' judgment.

I agree, and this was sloppy thinking on my part. The Dewey decimal code
isn't actually a primary key in its usual sense. The Dewey code doesn't
uniquely identify a book, it only identifies a category into which books
can be classified. The LoC classification is similar.

The ISBN identifies a title and not a book. To be even more specific it
identifies one or more editions of a specific title. To identify an
individual book a library will use another code, typically an
autonumber, called the Accession Number.

The SSN is supposed to identify a unique individual, although there have
been mistakes in that mapping. In the UK the equivalent code is
generated from the time and location of the individual's birth, and if
more than one person is born in that place and time a serial number
(essentially an autonumber) is also used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top