What is the difference between a candidate key and a primary key?

后端 未结 11 1205
不思量自难忘°
不思量自难忘° 2020-12-23 17:24

Is it that a primary key is the selected candidate key chosen for a given table?

相关标签:
11条回答
  • 2020-12-23 18:02

    A Primary key is a special kind of index in that:

    there can be only one;
    it cannot be nullable
    it must be unique.
    

    Candidate keys are selected from the set of super keys, the only thing we take care while selecting the candidate key is: It should not have any redundant attribute.

    Example of an Employee table: Employee ( Employee ID, FullName, SSN, DeptID )

    1. Candidate Key: are individual columns in a table that qualifies for the uniqueness of all the rows. Here in Employee table EmployeeID & SSN are Candidate keys.

    2. Primary Key: are the columns you choose to maintain uniqueness in a table. Here in Employee table, you can choose either EmployeeID or SSN columns, EmployeeID is a preferable choice, as SSN is a secure value.

    3. Alternate Key: Candidate column other the Primary column, like if EmployeeID is PK then SSN would be the Alternate key.

    4. Super Key: If you add any other column/attribute to a Primary Key then it becomes a super key, like EmployeeID + FullName, is a Super Key.

    5. Composite Key: If a table does not have a single column that qualifies for a Candidate key, then you have to select 2 or more columns to make a row unique. Like if there is no EmployeeID or SSN columns, then you can make FullName + DateOfBirth as Composite primary Key. But still, there can be a narrow chance of duplicate row.

    0 讨论(0)
  • 2020-12-23 18:05

    First you have to know what is a determinant? the determinant is an attribute that used to determine another attribute in the same table. SO the determinant must be a candidate key. And you can have more than one determinant. But primary key is used to determine the whole record and you can have only one primary key. Both primary and candidate key can consist of one or more attributes

    0 讨论(0)
  • 2020-12-23 18:06

    There is no difference. A primary key is a candidate key. By convention one candidate key in a relation is usually chosen to be the "primary" one but the choice is essentially arbitrary and a matter of convenience for database users/designers/developers. It doesn't make a "primary" key fundamentally any different to any other candidate key.

    0 讨论(0)
  • 2020-12-23 18:08

    Think of a table of vehicles with an integer Primary Key.

    The registration number would be a candidate key.

    In the real world registration numbers are subject change so it depends somewhat on the circumstances what might qualify as a candidate key.

    0 讨论(0)
  • 2020-12-23 18:09

    A table can have so many column which can uniquely identify a row. This columns are referred as candidate keys, but primary key should be one of them because one primary key is enough for a table. So selection of primary key is important among so many candidate key. Thats the main difference.

    0 讨论(0)
提交回复
热议问题