David Portas (RE****************************@acm.org) writes:
If I tell them "no, you can't store this instrument in the database,
because there is no identification on it than I can use for a primary
key", they will give me very sinister glances.
Keys don't prevent you storing information. They prevent you storing
redundant data. You lose nothing by eliminating that redundancy and you
may gain a lot because redundancy can cause incorrect results.
If I require that when users registers instruments that they must
specify an ISIN code, users have two choices when they have an instrument
that do not have an ISIN code (or the users don't know the code):
1) don't register until they have found an ISIN code for the instrument.
2) make one up.
1) is not not a serious option, since an OTC instrument agreed on between
two parties never will have an ISIN code.
And what with 2)? Well, what if users invents a new code, instead of
using an existing instrument?
So much did that key help from preventing redudant data.
Another example: for customers you get the idea to use the national
registration number. Never mind the fine detail that potential customers
without a national registration number cannot get into the database.
Anyway, at the end of the year we need to produce reporting to the
tax authorities. A customer has exactly one tax country, but oops, here
is this guy who changed his taxation country in the middle of the
year. Of course, rather than making the tax country an attribute of
a customer, we could have a customertaxcountryhistory table, but, eh,
we can't fix that this year. Anyway, the workaround is simple: register
the customer anew, with a new accounts, so his transactions for the
part of the year we need to report him. Since we do not require uniqueness
on national registration number (and even less are foolish to make
this a key), this was possible.
Building a system too much on natural keys means that when you have
goofed on the model and missed the 0.1% exceptional case, mean that
users will have to fund ugly workarounds - which in worst cases can
mean entirely manual procedures. And don't tell me goofs in the design
don't happen. They do.
Bad data can also get into the system in ways you cannot protect the
system from. You can prevent a user from entering that a person is
living in Flance, by having countries as entity, and only permit a
set of defined countries. But you cannot prevent a user from entering
Switzerland for a customer who is living in Sweden.
So while you by means of constraints, keys etc can prevent some crap data
from getting into the database, you can not prevent all sorts of crap.
But if you are too restrictive, you put users in a straightjacket
where your system hinders their job.
In this context is a fallacy to turn every stone to find a natural primary
key. Sometimes it is simply not there. In an order registration system,
the key is the order number generated by the application. That key can
serve as a natural key in other system, but in the order registration
system we should not try to find some other key. If you want to detect
double-registration of orders, it's probably better to develop some sort
of fuzzy-logic report, where the users manually have to consider the
suspects.
--
Erland Sommarskog, SQL Server MVP,
es****@sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp