When to use one field as primary key instead of 2?

后端 未结 4 1069
借酒劲吻你
借酒劲吻你 2020-12-12 03:11

I often see some database design like this:

Case 1:

UserTable

--id[auto increase]

--UserName

--Password

--Em

4条回答
  •  感情败类
    2020-12-12 03:34

    One reason I can think of for not using things like UserName as the primary key is that they could be subject to change. Having anything that's exposed to the outside world as a primary key runs the risk of those things being changed, and it's best to have a stable primary key.

    What if the user changes an email or username; do you really want to change your keys in all your relationships? IMO, it's best to have a stable key that never sees the outside world, about which everyone knows nothing, and therefore which can remain stable regardless of what changes may occur in your database.

提交回复
热议问题