What's the best practice for primary keys in tables?

前端 未结 21 2409
别那么骄傲
别那么骄傲 2020-11-22 14:02

When designing tables, I\'ve developed a habit of having one column that is unique and that I make the primary key. This is achieved in three ways depending on requirements

21条回答
  •  旧时难觅i
    2020-11-22 14:29

    There´s no problem in making your primary key from various fields, that's a Natural Key.

    You can use a Identity column (associated with a unique index on the candidate fields) to make a Surrogate Key.

    That´s an old discussion. I prefer surrogate keys in most situations.

    But there´s no excuse for the lack of a key.

    RE: EDIT

    Yeah, there´s a lot of controversy about that :D

    I don´t see any obvious advantage on natural keys, besides the fact that they are the natural choice. You will always think in Name, SocialNumber - or something like that - instead of idPerson.

    Surrogate keys are the answer to some of the problems that natural keys have (propagating changes for example).

    As you get used to surrogates, it seems more clean, and manageable.

    But in the end, you´ll find out that it's just a matter of taste - or mindset -. People "think better" with natural keys, and others don´t.

提交回复
热议问题