The general rule is "keep your keys as narrow as possible, unless you have an explicit requirement to expand them".
Typical situations
Following your example, when you will start designing the OrderDetails
table, with the design 1 your options are:
- Migrate the whole PK,
OrderId, UserId
into the child table, or
- Introduce some single column alternate key into the
Orders
table, and reference this AK, instead of PK.
If you do need the user in the order details (which is extremely unlikely), the former schema is fine, but you probably don't. In addition, if you will need to implement a functionality like "change ownership of an order", with the first approach you will discover that the task suddenly becomes much more convoluted than it normally should be.
As such, in the absolute majority of cases, the latter design is a way to go.
Extreme cases
Suppose you have some peculiar business requirement, say:
For every user, orders' numbers should form a sequence independent from orders of any other users.
Looks like a reasonable justification for design 1, isn't it? Well, yes and no. Sure, you have to create a composite key comprised of UserId, OrderId
so that order numbers can be reused by different users, however:
- Such a key doesn't have to be a primary key. Also, you don't have to reference it by any of the foreign keys from child tables. Just create a surrogate
Id bigint identity(1,1) primary key
and reference it from anywhere.
- The rule "don't update keys, or at least don't reference updateable keys" still stands. If the key is potentially updateable, it's a very poor candidate for being referenced by a foreign key.