Using FKs to non PK unique-indexes
Date: 9 Feb 2006 08:39:41 -0800
Message-ID: <1139503181.879478.24680_at_g14g2000cwa.googlegroups.com>
Hello
Recently, I decided to use the MSSQL feature that allows an FK to point
to a unique-index that is *not* a PK. I must say that this lead me to
several - not to say existential - questions about database modeling
and theory. I am basically looking for your opinions on the subject.
Before discussing the case, here is a simplification of my design
(tables in capitals) :
PRODUCTS
CLIENTS
CLIENTS_PRODUCTS
productID PK
clientID PK
clientID PK and FK on CLIENTS.clientID
productID PK and FK on PRODUCTS.productID
Perfect ... but how do I set the FK between ORDERS and ORDERS_PRODUCTS? One might say I have 2 options :
1- I put the FK between ORDERS_PRODUCTS.orderID and ORDERS.orderID. 2. I extend the ORDERS PK table to the clientID column and I put the FK to ORDERS_PRODUCTS.orderID and ORDERS_PRODUCTS.clientID
Option 1 is absolutely out of question since it would create a very weak integrity model (allowing an order to have products that are not part of the order's client profile).
Option 2 has more sens ... but I dont like it for 2 reasons. First, having the ORDERS table PK on both clientID and orderID has no logic. It gives the impression (and actually allows the situation) that a single orderID can be repeated for many clients. Second, it creates a situation where all subsequent relationship entities that "inherit" from ORDERS will be stuck with the clientID column in their PK.
I then came up with a third option :
3- I put a unique-index on ORDERS.orderID and ORDERS.clientID and point the FK on that index
Thank for your comments.
BTG Received on Thu Feb 09 2006 - 17:39:41 CET