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 ?
Part A is false since it would always be used in joins and the candidate key would NEVER be used in joins but Part B is correct because it is the candidate key that would most likely be the one searched on.It can't be effectively used in joins and is never used in a where clause.
I'm not sure why you would ever do this. You can't enforce RI and the database engine defines the relationship as Inderterminate rather than 1-1 or 1-m. Joining two tables in a query on fields without formal relationships is necessary on occassion but that isn't a reason to define a formal relationship. If you attempt to use this indeterminate relationship in a query with Jet/ACE (and probably SQL Server also), you will end up with a non-updateable query. That may be OK if that suits your purpose.Very often the primary key is the one referenced by foreign keys in other tables but that isn't invariably the case. Some database designers adopt the convention of designating a natural key as "primary", regardless of whether a foreign key will actually reference it or not and even if such a table also has a surrogate. There's nothing fundamentally wrong with doing that. As with other design conventions, what matters is that the community using the convention understands it and can apply it correctly.
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);
That is scary bug. Compact and repair fixes the problem. Luckily, in the normal course of events, appending records with existing autonumber values is rarely done. I do it occassionally as part of a conversion but when doing conversions, I always compact frequently so I've never experienced it.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.
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.