SQL for Smarties | SQL Programming Style | Trees and Hierarchies in SQL | SQL Puzzles and Answers | Data and Databases


Monday, June 05, 2006

covering Primary Keys.. What's too much

SQL Apprentice Question
I have a table which I'd like to create a Primary Key that would have to
cover 6 fields to enforce uniqueness.
My question is, is that too much. I know it's common to have tables with
PK's that cover 2-3 fields, but 6 seems like a little much. [3 int fields, 2
varchar(5) fields, 1 tinyint]
I know I can use a surrogate "ID" identity field as the PK, but I prefer to
use PK's that are more natural if possible.

Any thoughts on this?



Celko Answers

>> I have a table which I'd like to create a Primary Key that would have to cover 6 fields [sic] to enforce uniqueness. My question is, is that too much. <<


This is rare but possible. Since we do not have any specs or knwo
anything about the problem, all we can do is generalize. I have seen
some people create super-keys (sertial number + manufacturer, not
knowning that serial number has manufacturer in it).


>> I know I can use a surrogate "ID" identity field as the PK, but I prefer to use PK's that are more natural if possible. <<


1) it is not a surrogate key because it is exposed to the user; that is
the definition from Dr. Codd.

2) you will need to use a UNIQUE (c1,.. ,c6) constraint anyway, so you
now have two indexes


3) You will need code to assure that the IDENTITY is always validate
and points to the right target.

No comments: