What's the point of a candidate key?

前端 未结 9 627
借酒劲吻你
借酒劲吻你 2020-12-09 18:00

I\'m fairly new to database management and this question never seems to be answered in more than one sentence. All other SO answers say \"A candidate key is a minimal super

9条回答
  •  眼角桃花
    2020-12-09 18:43

    Candidate key is a concept that appears when you are designing a database system.

    Suppose your system will have a table named User, defined as below:

    User (fullName, socialSecurityNumber, creditCardNumber, age).
    

    Well, you have to choose which subset of these columns will be your primary key. The goal when designing the database is, of course, to keep this set minimal. You wouldn't use the pair (SSN, creditCardNumber) if the SSN alone already guarantees uniqueness.

    Now, suppose that fullName, SSN and creditCardNumber are all fields that you know, somehow, that are unique for all users. You could use any of those as your PK, so they are all candidate keys (whereas age, on the other hand, is not).
    Which will you choose? That will depend on factors such as the datatype of the field (it's preferable to set an index on an integer column rather than on an varchar column, for example).

提交回复
热议问题