I'm talking about a SURROGATE key (ID), not a known Primary Key like CustomerID which would be indexed.
I guess my real question is: can an Autonumber column ever have duplicates ?
A surrogate key is a minimal superkey and therefore by definition it is a candidate key. I've not before heard anyone say that a surrogate isn't a candidate key, which is a strange misuse of terminology. In terms of the relational model it is certainly incorrect to imagine that primary keys could be something other than candidates. Candidate keys are a fundamental feature of the RM and if a table lacks any candidate keys then it is not a relation.Surrogate keys are not generally referred to as Candidate keys since they do not exist in the underlying data. They are artificially created because no suitable Candidate key exists or because the database designer prefers to use artifical keys rather than natural keys.
I assumed the OP was asking about Access but not necessarily about Jet/ACE. In any case, all my remarks apply equally to Jet/ACE as far as I'm aware.This is an Access forum so my remarks are biased toward how Jet/ACE work and in the absense of a statement to the contrary all posts should be considered to be asking about Access/Jet/ACE.
I'm talking about a SURROGATE key (ID), not a known Primary Key like CustomerID which would be indexed.
I guess my real question is: can an Autonumber column ever have duplicates ?
if the foregoing hasn't clarified this, then the answer to this is BY DEFINITION - NO - an autonumber is defined NOT to have duplicates.
CREATE TABLE tbl1 (x AUTOINCREMENT NOT NULL);
INSERT INTO tbl1 (x) VALUES (1);
INSERT INTO tbl1 (x) VALUES (1);
CREATE TABLE tbl1 (x AUTOINCREMENT NOT NULL UNIQUE);
INSERT INTO tbl1 (x) VALUES (1);
INSERT INTO tbl1 (x) VALUES (1);
partial quote from buttonmoon
There is also another related problem. Inserting a value of N to an autoincrement column resets the seed value so that the next generated value will be N+1 - even if that value already exists.
no - this is just not correct. The seed definitely does not reset in this case. try it and see.
no - this is just not correct. The seed definitely does not reset in this case. try it and see.[/QUOTE]
I tried it and got the result I described (in Access 2007). Example code follows. After running this I opened the table and added a new row in the grid in the usual way. The autogenerated value was 2 and this caused an error "The changes you requested to the table were not successful because they would create duplicate values in the index...". The same error occurred when I tried to insert again because the next value generated was 3.
Dave, what are you doing differently? Maybe it was fixed in some versions? Maybe you could post some code to reproduce what works for you.
Code:CREATE TABLE tbl1 (x AUTOINCREMENT NOT NULL PRIMARY KEY, z INT NOT NULL); INSERT INTO tbl1 (x,z) VALUES (3,0); INSERT INTO tbl1 (x,z) VALUES (2,0); INSERT INTO tbl1 (x,z) VALUES (1,0);
Thanks for that Gemma. I have been amazed from this thread on all of the misconceptions of table design....zowie, no wonder there have been so many software disasters recently that were publicized.if the foregoing hasn't clarified this, then the answer to this is BY DEFINITION - NO - an autonumber is defined NOT to have duplicates.
A certain bug in access occasionally produces a situation where the next number seed fails., and duplciates could arise, although general a unque constraint prevents the addition of the duplicate. but this perverse, and is not expected by any means.
An Autonumber column as surrogate key SHOULD never have duplicates as it should always be increasing in value.
An Autonumber column as surrogate key SHOULD never have duplicates as it should always be increasing in value. And I contend that an index on such column is totally worthless and wasteful. It consumes space and slows-down inserts.
I have. Since you seem to be able to, please post a working example so we can all learn something.A foreign key constraint can reference any key* and the logic and behaviour is exactly the same whether that key happens to be "primary" or not. I suggest you try it out for yourself.
You are the one who said it was wasteful to have an index on the surrogate key.
Of course it can't be used effectively to relate tables if you don't give it a unique index and once you go that route, why not make it the PK? After all, its reason for existence was to provide uniqueness because there wasn't a viable candidate key!
Once you add an autonumber to a table, it doesn't just become A candidate key, it becomes THE candidate key or there is no point to adding it at all.