If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
Autonumber Fields
Access allows a table to have an autonumber field which could be considered a
record number. In an application I am involved in developing there are a number of code tables which are using the autonumber field as the code. This is autonumber code is then used in the tables holding the data. I am seeking opinions on this approach. Is this a reasonable practice? What are the dangers of doing this? I come from a mainframe environment where this sort of approach is avoided by generating a unique code value which is not an effective record number as a record is added. -- Denis |
#2
|
|||
|
|||
Denis wrote:
Access allows a table to have an autonumber field which could be considered a record number. In an application I am involved in developing there are a number of code tables which are using the autonumber field as the code. This is autonumber code is then used in the tables holding the data. I am seeking opinions on this approach. Is this a reasonable practice? What are the dangers of doing this? I come from a mainframe environment where this sort of approach is avoided by generating a unique code value which is not an effective record number as a record is added. An AutoNumber is also a generated value that is NOT an effective record number. Its only guarantee is uniqueness, not a gapless progression of incrementing numbers. -- I don't check the Email account attached to this message. Send instead to... RBrandt at Hunter dot com |
#3
|
|||
|
|||
Autonumber fields make excellent Primary Keys. That's what they were
designed for, and as Rick said, they are not designed to be a record number. They are used only to create a guaranteed unique value for relating tables. I use them in every database and highly recommend their use. -- --Roger Carlson Access Database Samples: www.rogersaccesslibrary.com Want answers to your Access questions in your Email? Free subscription: http://peach.ease.lsoft.com/scripts/...UBED1=ACCESS-L "Denis" wrote in message ... Access allows a table to have an autonumber field which could be considered a record number. In an application I am involved in developing there are a number of code tables which are using the autonumber field as the code. This is autonumber code is then used in the tables holding the data. I am seeking opinions on this approach. Is this a reasonable practice? What are the dangers of doing this? I come from a mainframe environment where this sort of approach is avoided by generating a unique code value which is not an effective record number as a record is added. -- Denis |
#4
|
|||
|
|||
Roger Carlson wrote: Autonumber fields make excellent Primary Keys. You've misunderstood what PRIMARY KEY means. An unique integer which has no meaning in respect fo the entities being modelled makes a lousy PRIMARY KEY. Google for "clustered index" in the Access groups. An autonumber is a convenient uniqueifier but unquieness for its own sake make not be such a good thing. |
#5
|
|||
|
|||
That you disagree with somebody does not make that person wrong. Roger has
provided a wide range of assistance in this forum, and has made samples available on his web site. Based on his track record I would be inclined to follow his advice. If you are trying to convert people to the idea of using clustered indexes, a very basic discussion of what they are would be most helpful. I have taken your suggestion to look at Google groups. There is indeed a lot of discussion, but I have not yet found how I would create a clustered index if I wanted to. My databases with a few thousand records seem to work just fine. Why would I want to put extra effort into something that already works well? I know you have posted code that includes MAKE TABLE or some such, but the utility of such code is not clear. The other thing I noted in Google groups is that most of the discussion of clustered indexes seems to be in discussions about SQL server. wrote in message oups.com... Roger Carlson wrote: Autonumber fields make excellent Primary Keys. You've misunderstood what PRIMARY KEY means. An unique integer which has no meaning in respect fo the entities being modelled makes a lousy PRIMARY KEY. Google for "clustered index" in the Access groups. An autonumber is a convenient uniqueifier but unquieness for its own sake make not be such a good thing. |
#6
|
|||
|
|||
Jet 4.0 and 3.5 (and earlier versions) cluster on the Primary Key and a
Compact will keep it managed. Indeed a clue to this is the Registry entry for: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\3.5\Engi nes. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engi nes both contain the setting CompactByPKey. I am not sure what would happen if you changed the above setting from 1, I expect 0 would skip the clustering - I am not sure if any other setting would be valid. SQL Server generally clusters on the Primary Key, however, you can select another index. AutoNumbers are very poor devices to truly define a unique record in the real world, You can enter the name John Doe 1,000,000 times in your database if the Primary Key is an AutoNumber and you have failed to do something to prevent the creation of 1,000,000 John Doe's. You may have 1,000,000 unique records but so what? Recommending the AutoNumber as Primary Key without pointing out the dangers, and suggesting the definition and declaration of the natural key (should one exist), is unwise. BTW A clustered index is merely a physical ordering of the records in a table in the database file. Using the true natural key (should one exist) as the primary key will ensure that all the records with a similar PK will be physically located next to each other. Using an AutoNumber (sequential order) as PK will mean the records are clustered according to their creation order. Using IDENTITY and AutoNumber as PK defeats the purpose of PK, this is not so bad in SQL Server as it allows you to choose something more sensible if you have an IDENTITY field in use as PK. -- Slainte Craig Alexander Morrison Crawbridge Data (Scotland) Limited "BruceM" wrote in message ... That you disagree with somebody does not make that person wrong. Roger has provided a wide range of assistance in this forum, and has made samples available on his web site. Based on his track record I would be inclined to follow his advice. If you are trying to convert people to the idea of using clustered indexes, a very basic discussion of what they are would be most helpful. I have taken your suggestion to look at Google groups. There is indeed a lot of discussion, but I have not yet found how I would create a clustered index if I wanted to. My databases with a few thousand records seem to work just fine. Why would I want to put extra effort into something that already works well? I know you have posted code that includes MAKE TABLE or some such, but the utility of such code is not clear. The other thing I noted in Google groups is that most of the discussion of clustered indexes seems to be in discussions about SQL server. wrote in message oups.com... Roger Carlson wrote: Autonumber fields make excellent Primary Keys. You've misunderstood what PRIMARY KEY means. An unique integer which has no meaning in respect fo the entities being modelled makes a lousy PRIMARY KEY. Google for "clustered index" in the Access groups. An autonumber is a convenient uniqueifier but unquieness for its own sake make not be such a good thing. |
#7
|
|||
|
|||
Using IDENTITY and AutoNumber as PK defeats the purpose of PK, this
is not so bad in SQL Server as it allows you to choose something more sensible if you have an IDENTITY field in use as PK. When I wrote that it defeats the purpose of the PK it should have read it defeats the purpose of clustering on the PK unless the creation order is the one you want to physically order your records. -- Slainte Craig Alexander Morrison Crawbridge Data (Scotland) Limited "Craig Alexander Morrison" wrote in message ... |
#8
|
|||
|
|||
Thank you for the explanation. It makes sense that it has to do with
physical ordering in a table rather than on the disk. Having said that, I cannot discover the connection between indexes, the table's Order By property, and anything else that suggests an order within the table, on the actual order of records in the table. Order By, in particular, seems to accomplish nothing. Regarding John Doe, it may well be a name used by more than one person. How does this fit in with clustered indexes? I may need duplication in that field. Suppose I wanted to create a clustered index in an Access table. How would I do that? The term does not appear in Access Help, and discussions of the subject tend to assume the reader knows what a clustered index is and how to create one. Even if one is created, what benefits will I notice? "Craig Alexander Morrison" wrote in message ... Jet 4.0 and 3.5 (and earlier versions) cluster on the Primary Key and a Compact will keep it managed. Indeed a clue to this is the Registry entry for: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\3.5\Engi nes. HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engi nes both contain the setting CompactByPKey. I am not sure what would happen if you changed the above setting from 1, I expect 0 would skip the clustering - I am not sure if any other setting would be valid. SQL Server generally clusters on the Primary Key, however, you can select another index. AutoNumbers are very poor devices to truly define a unique record in the real world, You can enter the name John Doe 1,000,000 times in your database if the Primary Key is an AutoNumber and you have failed to do something to prevent the creation of 1,000,000 John Doe's. You may have 1,000,000 unique records but so what? Recommending the AutoNumber as Primary Key without pointing out the dangers, and suggesting the definition and declaration of the natural key (should one exist), is unwise. BTW A clustered index is merely a physical ordering of the records in a table in the database file. Using the true natural key (should one exist) as the primary key will ensure that all the records with a similar PK will be physically located next to each other. Using an AutoNumber (sequential order) as PK will mean the records are clustered according to their creation order. Using IDENTITY and AutoNumber as PK defeats the purpose of PK, this is not so bad in SQL Server as it allows you to choose something more sensible if you have an IDENTITY field in use as PK. -- Slainte Craig Alexander Morrison Crawbridge Data (Scotland) Limited "BruceM" wrote in message ... That you disagree with somebody does not make that person wrong. Roger has provided a wide range of assistance in this forum, and has made samples available on his web site. Based on his track record I would be inclined to follow his advice. If you are trying to convert people to the idea of using clustered indexes, a very basic discussion of what they are would be most helpful. I have taken your suggestion to look at Google groups. There is indeed a lot of discussion, but I have not yet found how I would create a clustered index if I wanted to. My databases with a few thousand records seem to work just fine. Why would I want to put extra effort into something that already works well? I know you have posted code that includes MAKE TABLE or some such, but the utility of such code is not clear. The other thing I noted in Google groups is that most of the discussion of clustered indexes seems to be in discussions about SQL server. wrote in message oups.com... Roger Carlson wrote: Autonumber fields make excellent Primary Keys. You've misunderstood what PRIMARY KEY means. An unique integer which has no meaning in respect fo the entities being modelled makes a lousy PRIMARY KEY. Google for "clustered index" in the Access groups. An autonumber is a convenient uniqueifier but unquieness for its own sake make not be such a good thing. |
#9
|
|||
|
|||
BruceM wrote: That you disagree with somebody does not make that person wrong. That someone has a track record of providing a wide range of assistance does not make that person right ;-) In this case, I disagree with the person because they do not understand what PRIMARY KEY means. Autonumber does not make a good uniquifier, let alone a good PK (different concepts). Remember this list? the advantages of using autonumber a 1. Convenience, because it's provided by the 'system'; 2. It's an 'efficient' data type; 3. Erm... 4. That's it! |
#10
|
|||
|
|||
Well, the battle between whether to use Natural Keys or Surrogate Keys has
waged for years and we will not resolve it here. But please do not tell me and others what I do not understand just because our opinions differ. A Primary Key has two properites: 1) it must be unique, and 2) it must not be NULL. In any table there may be several fields (or combinations) of fields which could possibly fulfill these conditions. These are called Candidate Keys. The job of the database developer is to decide between these CKs to see which is the absolute best. In the case of the Contacts table (mentioned in a separate post); last_name, first_name and postal_address could provide a key, but it is not a really good choice. While this may provide uniqueness (maybe), there is a real possibility that a person does not have a postal address (no part of a compound primary key can be NULL). This is one of the difficulties of finding a good natural keys to make up the primary key. Now, there are actually two purposes for uniqueness: 1) Real-world uniqueness to make sure an entity appears only once in a table, and 2) Relational uniqueness for relating records between tables. As you noted, autonumber fields are lousy in terms of real-world uniqueness. However, they are perfect for creating uniqueness for relating records. In this sense, the autonumber field is absolutely unique. It can appear ONLY once and can never be repeated. Further, as a long integer, it is VERY efficient for relating records compared to a compound primary key composed of text fields. So when I recommend autonumbers for primary keys, I also recommend creating a separate UNIQUE INDEX that is composed of fields from a Natural Key. This fulfils the function of real-world uniqueness and is more flexible, because you CAN allow Nulls in a unique index if you want. In this way, both functions of a Primary Key are fulfilled to their maximum efficiency. You may not agree with this. I know many respected developers who don't. Opinions differ, but this does not give you the right to disparage those who disagree with you. -- --Roger Carlson Access Database Samples: www.rogersaccesslibrary.com Want answers to your Access questions in your Email? Free subscription: http://peach.ease.lsoft.com/scripts/...UBED1=ACCESS-L wrote in message oups.com... BruceM wrote: That you disagree with somebody does not make that person wrong. That someone has a track record of providing a wide range of assistance does not make that person right ;-) In this case, I disagree with the person because they do not understand what PRIMARY KEY means. Autonumber does not make a good uniquifier, let alone a good PK (different concepts). Remember this list? the advantages of using autonumber a 1. Convenience, because it's provided by the 'system'; 2. It's an 'efficient' data type; 3. Erm... 4. That's it! |
|
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Sorting a table by concatenating several fields in the same table | salsaguy | Running & Setting Up Queries | 3 | March 6th, 2005 09:41 PM |
Sorting a table by concatenating several fields in the same table | salsaguy | Running & Setting Up Queries | 0 | March 6th, 2005 02:33 AM |
Additional fields for form based parameter query/null fields | geeksdoitbetter | Running & Setting Up Queries | 2 | January 7th, 2005 11:05 PM |
Selecting Fields for Update | Steve Daigler | Page Layout | 4 | October 15th, 2004 02:13 PM |
My tables lost their AutoNumber fields | Bill Nicholson | Database Design | 2 | July 2nd, 2004 02:20 AM |