Database “key/ID” design ideas, Surrogate Key, Primary Key, etc

前端 未结 7 1864
北荒
北荒 2021-02-09 16:15

So I\'ve seen several mentions of a surrogate key lately, and I\'m not really sure what it is and how it differs from a primary key.

I always assumed that ID was my prim

7条回答
  •  暖寄归人
    2021-02-09 16:38

    • The primary key is whatever you make it. Whatever you define as the primary key is the primary key. Usually its an integer ID field.
    • The surrogate key is also this ID field. Its a surrogate for the natural key, which defines uniqueness in terms of your application data.

    The idea behind having an integer ID as the primary key (even it doesnt really mean anything) is for indexing purposes. You would then probably define a natural key as a unique constraint on your table. This way you get the best of both worlds. Fast indexing with your ID field and each row still maintains its natural uniqueness.

    That said, some people swear by just using a natural key.

提交回复
热议问题