Domanda

If we have a table whose primary key is a Composite Key, do we always create a new column as a primary key instead of the composite key?, or it depends on DB Analysis?

Which way is the best? And why?

È stato utile?

Soluzione

Probably the main reason I'd add a surrogate key primary key as well as a unique composite, is if I needed it for a foreign key for another table.

The second would be if I suspected the primary composite key was likely to become not unique in the near future.

Other than that you'd have to be talking some sort of very specific optimisation.

Best is subjective always.

Altri suggerimenti

This is the classical natural vs surrogate key discussion.

Neither is absolutely "better" - it all depends on what kind of engineering trade-off you are trying to achieve. Fot the list of pros and cons, see here.

I think you can use the way you want based on your analysis, but if your primary keys (composite) may be repetition you should use a new key as primary. and you should notice foreign keys and another properties making a composite key, if you used a non-key property to make part of your composite key, you would have to be sure from your analysis and then rebuild you keys again.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top