John J. said:
I'm about to create a table with a foreign key. It's the Many-table from a
1xM relationship. I recall reading somewhere that it's always good to add
a primary auto-number key to the table. Can someone confirm if this is
right and can someone explain why this is good? I don't see the use of it
in this case.
Thanks,
John
The issue of foreign keys versus that of natural keys is a different
argument than your question (unfortunately the other posters kind of went
off key on that issue - pun intended ).
Simply put your are asking when you have a child table and it has a field
that relates it back to the parent table. The next question you're asking is
should this child table have a primary key in this table when there Seems
little if any need for such a primary key ?
The answer is absolutely yes and their is several reasons why.
Of course the first and most of these reasons is down the road you
eventually might have another table that needs to be related to this table,
and therefore you'll need a primary key.
Another good reason is any kind of sql update code needs to deal with ONE
row of the child record. Thus, to use any SQL to update an individual row in
that table will require you to be able to resolve to one row, or simply put
you'll need a primary key to do that sql udpate.
Perhaps the most common reason it comes up for me personally is when trying
to retrieve the last of something for a customer. Lets assume that we have a
master table of customers, and then our child table is one of customer
invoices and the invoice date. To get the person's LAST invoice date we
would go:
select ID, CustomerName,
(select top 1 InvoiceDate from tblInvoices
where customer_id = tblCcustomers.ID
order by InvoiceDate desc) as LastInvoicdDate
from tblCustomers
In the above query were simply going to list the ID, the customer name, and
the last invoice date for this customer. The problem with the above is what
happens if we have two invoice dates for that customer on the same day? It
turns out that the above query will now crap out (since that sub query must
return only one record, and we have no way to return actually one record if
we don't have a primary key in the child table). If we have an auto number
primary key to in the child table, and we simply change the above query to
the following were are home free:
select ID, CustomerName,
(select top 1 InvoiceDate from tblInvoices
where customer_id = tblCcustomers.ID
order by InvoiceDate desc, ID desc) as LastInvoicdDate
from tblCustomers
Note the : order by InvoiceDate desc, ID desc
By ordering the child table by descending ID, and including it in the order
by clause, we can now resolve to ONE reocrd that is the last invoice record
for the customer. otherwise, if a customer has to invoice dates on one day,
how are we going to know which one was the last one? (furthermore, we might
not really care which is the last one, but we have the two invoice dates
with the same date, and are submarines in our application will start to fail
in this example). An auto number primary key solves this problem very nicely
for us.
Very simply put, when you start using SQL to update your data tables, or are
using SQL to pull data from a table, you REALLY do need to be able to
resolve to ONE ROW and you useally need a primary key to do that.
So as a general rule, you need a primary key to resolve and define that one
row that child table. Often in many cases you can come up with an natural
key that is a composite of several fields that makes the row unique.
However, in some cases such as my invoice example you might have more than
one invoice on the same day and thus it becomes more and more difficult to
resolve to one row by a natural key. So, adding an auto number primary key
to the child table solves quite a few problems.
I highly recommend as a design philosophy and approach to development that
you do add auto number primary key to child tables, even when it's not so
particularly obvious that you need such a primary key as per your example.