10/17/2007 3:57:57 AM
Access Foreign Keys & Space
By Richard Rost
Here's another GREAT question from a customer:
First of all, I now have a small business going as a database developer, and a large part of the credit goes to Richard. Excellent classes! But one question. I think you sometimes use IDs as primary keys in tables unneccesarily. For example, you showed a table of payment types, where you had an ID field and a payment type field. Wouldn't it be better just to have the payment type field only, and make it the primary key? Payment types will be unique, and then when you need the payment type in a query, you don't have to tie in the payment type table just to get the description, since the payment type will be stored as the foreign key in the other table, for example, the orders table. Please keep the Access classes coming!
It's certainly possible to create tables without keys. Personally, I prefer to use an ID field in almost every table because it keeps things clean and easy - and can save space in your database.
Let's take Payment Type for example. Let's look at two tables. The first one has IDs, and the second one doesn't.
1 - Visa
2 - MasterCard
3 - Discover
4 - American Express
Now in the second table, there's no ID to refer to the payment type, so for each customer's order where I have to track the payment type, their ORDER TABLE now has to have the full text of the payment method in it.
What takes up less space in your database:
1001, Joe Smith, American Express
1002, Sue Jones, MasterCard
1003, Bill Watson, American Express
1004, Adam West, American Express
Or this (assuming you use a CustomerID and PaymentID):
1001, 1, 4
1002, 2, 2
1003, 3, 4
1004, 4, 4
See? The space savings alone in a large database make it worthwhile. If you have 10,000 orders, you'll save multiple megabytes worth of space. And remember, a small database is a fast database.
ALSO, using IDs keeps your VBA code cleaner. What's easier:
X = DLOOKUP("Info","PaymentT","PaymentID=" & ID)
X = DLOOKUP("Info","PaymentT","PaymentType='" & PaymentName & "'")
Gotta put those single quotes (or two double quotes) around those string values - I hate working with string value.
So there ya go. Really it's just to keep the database tight and efficient, but there are lots of reasons why it's best to use an ID for every table - even if you don't think you'll need it.
Hope this helps.
Keywords: access index id tips