Primary Keys...Autonumber or Field Name? (1 Viewer)

RhiannonM

Registered User.
Local time
Tomorrow, 05:38
Joined
May 30, 2019
Messages
19
Hi Guys

Just a quick question...if I have tables that are a single field (eg. tblCategory has field name CategoryName and that's it) do I really need an Autonumber field as well or can the CategoryName be the primary key?

And yes, the category names are unique and not duplicated anywhere else in other tables.

Cheers!
RhiannonM
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:38
Joined
Oct 29, 2018
Messages
21,447
Hi. The use of an Autonumber field for primary keys has always been a personal choice. It was never a requirement. What is required is to have a primary key. Using a number for a primary key or index is much better than using a text field.
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 20:38
Joined
Sep 21, 2011
Messages
14,217
I tend to have surrogate keys that would be the autonumber. Could the Category name change at all.?
You will probably say No now, but further down the line, you never know. :D
 

RhiannonM

Registered User.
Local time
Tomorrow, 05:38
Joined
May 30, 2019
Messages
19
Good point Gasman...I don't see a need to change in the future but...ya never know...so I'll err on the side of caution...thanks for that!:D
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:38
Joined
Feb 28, 2001
Messages
27,128
The ONLY time I ever used a single-column field was a validation using an IN clause. As in, "was this keyword in the list of accepted keywords?" IF you are simply using the single-column table in this way, you do not need a synthetic key. In fact, technically speaking, you don't need ANY key. (You DO still need the unique index for search optimization.)

The only time you would need a PK is if there will ever be a child of this table, in which case you would then need the PK to support the relationship. It DOES NOT MATTER if you need to change something by adding a new keyword (for validation?) because you aren't copying a code from the table. I.e. this is NOT a lookup table or an encode/decode table. You don't have enough infrastructure in that table to use it for either purpose.

You need a PK or code value if/when this table takes on a new purpose besides list verification. If this were an SQL Server table it would need a PK because you can't update the table without one, but if this is a native Access table, that is not a requirement.

One final thought - this is a type of table where you would have to think long and hard about actually changing a record unless the record was incorrectly stored. Validation tables should not be edited unless you are ready to invalidate all previous tests run against that table. This isn't an "Access rule" but rather is in the nature of using this kind of lookup validation. At least, that was what I decided when I used this kind of lookup method.
 

June7

AWF VIP
Local time
Today, 11:38
Joined
Mar 9, 2014
Messages
5,463
How long are these category names? If storage is an issue, saving a number PK/FK would use less storage space. If saving CategoryName into related table, then you are replicating that value. Is that a problem? Maybe. Text values require more storage and are slower to index for sorting/searching processes. Will you notice the difference? Maybe. Depends on size of database and network performance.

I have used text field as primary key with no adverse impact. I worked with a proprietary app using Access as backend that generated unique IDs like 040698080(7+E,/+#XA$.

If category name were to change and you want the change to reflect in historical records and the name is PK, then build a relationship between tables and enforce cascade update or run an UPDATE action sql. Otherwise, it's just a new record in tblCategories.

@The_Doc_Man, "single-column field" in first sentence, expect you mean "single-column table".
 
Last edited:

Micron

AWF VIP
Local time
Today, 15:38
Joined
Oct 20, 2018
Messages
3,478
but if this is a native Access table, that is not a requirement.
This I would question. I've seen many an action query in Access that would not work because there was no primary key on 1 side of 1 or more tables. Perhaps I'm not taking that statement in the correct context.

As for the initial question, I think what is of primary importance up front is to understand the implications of a decision. Where I worked, the CMMS based database was designed around PK text fields. This means that wherever PK field DEPT was linked, you linked (for example) MACH to MACH (Machine Shop). That makes it so much easier when querying a PO table for example, because you didn't need DEPT table; otherwise you would because MACH would be some cryptic number such as 158 if it were an autonumber PK. Thus in a re-organization you could never change MACH to anything else, nor could you ever have a duplicate text value throughout the whole organization. In the unlikely event they ever HAD to change a text value for anything, they probably would have relied on cascade updates, although given the size of the db and the fact that the back ends were not Access tables, I doubt they were concerned about that.

For some, the whole concept might seem less than professional but I can assure you that the company was top notch when it came to IT, and was (and probably still is) a world class leader in computerized maintenance management systems (CMMS). I'm talking maybe 100 db's each with a huge number of tables, some with millions of records covering everything from soup to nuts (ICMS, RCA, FMEA, Purchasing, Safety, BOM, Contracting, Stores, Inventory, Maintenance, Scheduling, Utilization, real time condition monitoring, to name some). So the point of all that is that they are by no means amateurs yet use text based PK fields. That is why I say that the understanding of one's decision either way, replete with the limitations, advantages and any disadvantages of that decision is what's most important.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:38
Joined
Feb 28, 2001
Messages
27,128
Caught me in a mis-type, June7. Spot on - I meant single-column table.

Micron, the point is that this is a stand-alone validation table from its description (though if RhiannonM wishes to correct me, then I will stand corrected.) There is nothing with which to JOIN. There is no infrastructure, no code. It's just a list to be searched for IN/NOT IN as far as this discussion has gone. And I absolutely agree that if you have a "natural" text PK, use it. Synthetic keys have their place and I use them all the time. But in the context that we have seen so far, I haven't seen a need for an extra field.

As far as not being able to update an Access table that didn't have a PK? JUNCTION tables don't need a PK (because they aren't parents) and yet can be updated. If they couldn't, they would never have existed in the first place.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:38
Joined
Sep 12, 2006
Messages
15,634
if you do not include a numeric key AS WELL (you need a unique index on the category name ANYWAY) - then the database will store the longer category names text, rather than the shorter number key. If you change the category name, then every usage of the category name throughout your database will need updating, which will only happen if RI has been set appropriately in the relationships.

In general, I would always add a numeric key. There might be odd exceptions like a US state table. It's still a matter of choice, understanding the pros and cons - Micron's point about the text values being more transparent is a point to take into consideration.


[edit - TheDOCMan
you will still need to refer to the list when validating an entry, and you should still need to have the list included in your relationships to prevent you deleting a value from the list - unless you want to allow users to enter free text, rather than pick from a controlled list.]
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:38
Joined
Feb 28, 2001
Messages
27,128
Good point, Dave. Relational Integrity is important as an anti-deletion safeguard. I did not think of that facet last night, it was already getting quite late and June caught me in a late-night bit of sloppiness. One of the perils of being an insomniac.
 

Micron

AWF VIP
Local time
Today, 15:38
Joined
Oct 20, 2018
Messages
3,478
...if I have tables that are a single field (eg. tblCategory has field name CategoryName and that's it) do I really need an Autonumber field as well or can the CategoryName be the primary key?
I see nothing in that question that defines it as a "stand alone validation table", whatever that may be. I think the point I made is relevant and can be further explained for you.

Table DEPT contains only a single field of 4 alpha-only characters. It is the PK, thus by extension, it is the FK in every other table where it is found. If that makes it a validation table for you then that's fine. Your point about having nothing to join to is therefore lost on me, as is not having any code - in what , a table field?

My major point is that not everyone is hell bent on numeric keys, be they autonumber or whatever, and at least some who are not concerned are professionals. As long as one fully understands the ramifications of that approach when deciding, there is nothing decidedly wrong about it.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:38
Joined
Sep 12, 2006
Messages
15,634
@Micron

I see table DEPT as a table OF departments, (rather than just a single row), to use within the database as required. You might want to give these a 'meaningful' 4 character alpha code, or a numeric code.

eg
WARE , Warehouse
PROD , Production
OFFC , Office
FINC , Finance

The designated alpha code you pick might become less meaningful though if you need to add more departments, and need to change one of your descriptions, and as a result the alpha designation is no longer an "abbreviation" for exactly the same description. This wouldn't happen with a non-meaningful PK - you wouldn't need to change it.

If you use meanngful values, as a PK, then you need to cascade the changes throughout the database, If you use non-meaningful keys, you don't have to, so it's a matter of taste really.
 

Micron

AWF VIP
Local time
Today, 15:38
Joined
Oct 20, 2018
Messages
3,478
gth - I don't know if the meaning behind that is just general comment or if it's about something you think I need to consider. Surely my posts indicated that not only is the db not mine, it is long established, worked well and was created by people a lot smarter than me. Perhaps they went that way because of legacy information/data.

To reiterate the message - meaningful text data as PK can work; you just need to understand the ramifications of that choice. end of message & no one has to agree that it is a wise choice because I'm not saying that. If anyone has questions about the db I will try to answer them. I worked with it on and off for 24 years and there were parts I never touched so I don't know all of it, but any advice for me about that db is kinda pointless.

Thanks.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:38
Joined
Feb 28, 2001
Messages
27,128
I'm actually with Micron on this one - if you have a meaningful natural key, it is OK to use it as such. Less confusion for subsequent maintainers. If in the case described in this thread you made the (only) field the PK, it would by definition also be indexed, which would further aid any lookups, searches, relationships, or JOINs. There are many reasons to declare a synthetic key but at no point do I see any such reasons in this case under scrutiny.
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:38
Joined
Sep 21, 2011
Messages
14,217
Again being curious and keen to learn, or get other opinions.

Is there any benefit in having everything 'the same way'? especially if multiple/new developers
 

RhiannonM

Registered User.
Local time
Tomorrow, 05:38
Joined
May 30, 2019
Messages
19
Oh dear...what did I start? :confused:

First off...please don't complicate things...not for my sake at least...I have diminished capacity and am only using this database project as a way to retrain my brain as I once understood it and hope that by learning it again I can repair some damaged connections in my brain.

Secondly...it's just a pathetic albeit comprehensive recipe database and when I read the Access 2010 Bible they had a table that only used the category name as a primary key instead of an autonumber and I just wanted to make sure I was understanding it correctly.

I have several tables that are basically "single field tables"...they will be used to specify different aspects of the recipe and used as search parameters...primarily in combo boxes on a form and group headings in reports.

I don't plan on this database being for anyone else's use at this stage...it's more just to prove to myself I can do it...but I want to do it right.

So I apologise if I don't react with glee over your answers or thank anyone because unfortunately in this instance...i have no idea what you're all talking about LOL :banghead:

Thank you for your time in answering in tho
Cheers
RhiannonM
 

June7

AWF VIP
Local time
Today, 11:38
Joined
Mar 9, 2014
Messages
5,463
Not the first time this topic has provoked a firestorm.

I offer this as summation: what you have set up with text fields as PK is just fine.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 14:38
Joined
Feb 28, 2001
Messages
27,128
Is there any benefit in having everything 'the same way'? especially if multiple/new developers

That's a loaded question. I happen to think that for group-oriented projects, you need some type of uniform style guide, to be set by the project leader. If you have multiple forms, for example, you probably want the same "look and feel" on all of the forms in a single project. I accomplished this by using template forms that were already set up with the right command buttons for our apps, and for which a control formatter was available to be called with a control name and a state name. It would do the work of determining the color scheme for that state and that class of control, and then applying it. That way, no matter what table was being managed, the forms that did the work were predictable. This reduced the learning curve for the user base and also took some of the detail-management load off the developers.

Even for a single-user, single-developer project, there is a benefit for trying to keep things more or less the same, because there is the time factor. As in, you get something working, then have to go work on something else. Then two months later, something is found to have been broken and suddenly you have to go back and work on it. If you maintained some consistency then it is easier to immerse yourself in that "blast from the past."
 
Last edited:

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 20:38
Joined
Sep 12, 2006
Messages
15,634
I think I misunderstood a point @Micron made. I thought he was talking about a table with just one row, rather than table with a single field. I certainly wasn't attempting to start an argument - not even a 5-minute one.

In point of fact, I don't think it really matters whether you have a text PK or a numeric PK, it will still work.

I think in some cases text values can ultimately present some difficulties. Eg the periodic table has some unexpected (and maybe illogical) choices for the identifiers. So does the US state table. So do the 3LAs used for sports team, and the Alpha PostCodes used for British cities and postal areas. This happens when you have a relatively large number of options, with a limited alphabet to choose from, and the users need a very good familiarity with the abbreviations chosen.

And very good luck with your project @Rhiannon.
 

Users who are viewing this thread

Top Bottom