F
Fred Boer
Ok, well, I've been reading a few threads about Primary Keys, and the use of
natural vs. surrogate keys. Sadly, a little learning is a dangerous thing,
and I've realized, (yet again.. will it never end!<groan>), that I've got a
problem with the design of my tables. When I created my tables, I just went
along cheerfully whistling to myself and created an Autonumber primary keys
for almost all of my tables. I didn't even bother with unique indexes. Only
now do I realize that I've left myself wide open for duplicate records. So,
I'm trying to fix things...
Questions:
(I have included descriptions of some of my tables and their fields
below...)
1. Using Tbl_Status as an example: There are six possible status codes,
including "On Shelf", "Discarded", "Repair", etc. So, since these will
seldom change, and they shouldn't be duplicates, should I simply have one
field "Status" and make it a natural primary key for the table? If it *were*
the PK, how would that work? I mean, would the main table simply contain the
text of the field "Status" as a FK? This would take more space than the
current Autonumber FK, wouldn't it? And it would be slower than a long
integer, correct? But the advantage would be that I'd never run the risk of
a duplicate status code, is that right? (Obviously, with only six codes,
this is only a theoretical problem, but I'd like to use good design...)
2. Currently, I haven't got primary keys or unique indexes in my junction
tables, (like Tbl_BookSeries). Do I need a primary key for that table? Or
would a unique index on both fields be sufficient? Why or why not?
3. And for a table like "Tbl_Place", would you add new fields (such as
Province/State and Country), and then use a composite primary key made up of
the three fields? This assumes I want to know the difference between
London, England, and London, Ontario, Canada...
Thanks for any suggestions! And I thought I had the structure of
tables/relationships down pat! Argh! I find myself, again, feeling a bit
like I might never get this right! <g>
Fred Boer
P.S. I'm not even sure if I'm using FK and PK correctly at this point...!
Tables using Autonumber PK:
Tbl_Library (Main Table)
BookID - Autonumber
Title - Text
Dewey - Text
etc..
Author - FK to Tbl_Author
Series - FK to Tbl_Series
Status - FK to Tbl_Status
Pubplace - FK to Tbl_PubPlace
etc..
Tbl_Author
AuthorID - Autonumber
Firstname - Text
Middlename - Text
Lastname - Text
PrimaryAuthor - Logical
Tbl_Series (Series Name)
SeriesID - Autonumber
Series - Text
Tbl_Status (Status Code)
StatusID - Autonumber
Status - Text
Tbl_PubPlace (Place of Publication)
Pub_PlaceID - Autonumber
Place - Text
Junction Tables - No PK
Tbl_BookAuthor
BookID
AuthorID
Tbl_BookSeries
BookID
SeriesID
Tbl_BookSubject
BookID
SubjectID
natural vs. surrogate keys. Sadly, a little learning is a dangerous thing,
and I've realized, (yet again.. will it never end!<groan>), that I've got a
problem with the design of my tables. When I created my tables, I just went
along cheerfully whistling to myself and created an Autonumber primary keys
for almost all of my tables. I didn't even bother with unique indexes. Only
now do I realize that I've left myself wide open for duplicate records. So,
I'm trying to fix things...
Questions:
(I have included descriptions of some of my tables and their fields
below...)
1. Using Tbl_Status as an example: There are six possible status codes,
including "On Shelf", "Discarded", "Repair", etc. So, since these will
seldom change, and they shouldn't be duplicates, should I simply have one
field "Status" and make it a natural primary key for the table? If it *were*
the PK, how would that work? I mean, would the main table simply contain the
text of the field "Status" as a FK? This would take more space than the
current Autonumber FK, wouldn't it? And it would be slower than a long
integer, correct? But the advantage would be that I'd never run the risk of
a duplicate status code, is that right? (Obviously, with only six codes,
this is only a theoretical problem, but I'd like to use good design...)
2. Currently, I haven't got primary keys or unique indexes in my junction
tables, (like Tbl_BookSeries). Do I need a primary key for that table? Or
would a unique index on both fields be sufficient? Why or why not?
3. And for a table like "Tbl_Place", would you add new fields (such as
Province/State and Country), and then use a composite primary key made up of
the three fields? This assumes I want to know the difference between
London, England, and London, Ontario, Canada...
Thanks for any suggestions! And I thought I had the structure of
tables/relationships down pat! Argh! I find myself, again, feeling a bit
like I might never get this right! <g>
Fred Boer
P.S. I'm not even sure if I'm using FK and PK correctly at this point...!
Tables using Autonumber PK:
Tbl_Library (Main Table)
BookID - Autonumber
Title - Text
Dewey - Text
etc..
Author - FK to Tbl_Author
Series - FK to Tbl_Series
Status - FK to Tbl_Status
Pubplace - FK to Tbl_PubPlace
etc..
Tbl_Author
AuthorID - Autonumber
Firstname - Text
Middlename - Text
Lastname - Text
PrimaryAuthor - Logical
Tbl_Series (Series Name)
SeriesID - Autonumber
Series - Text
Tbl_Status (Status Code)
StatusID - Autonumber
Status - Text
Tbl_PubPlace (Place of Publication)
Pub_PlaceID - Autonumber
Place - Text
Junction Tables - No PK
Tbl_BookAuthor
BookID
AuthorID
Tbl_BookSeries
BookID
SeriesID
Tbl_BookSubject
BookID
SubjectID