Skip to main content
Engineering LibreTexts

3.4: Keys

  • Page ID
    93649
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    An important constraint on an entity is the key. The key is an attribute or a group of attributes whose values can be used to uniquely identify an individual entity in an entity set.

    Types of Keys

    There are several types of keys. These are described below.

    Candidate key

    candidate key is a simple or composite key that is unique and minimal.  It is unique because no two rows in a table may have the same value at any time. It is minimal because every column is necessary in order to attain uniqueness.

    From our COMPANY database example, if the entity is Employee(EID, First Name, Last Name, SIN, Address, Phone, BirthDate, Salary, DepartmentID), possible candidate keys are:

    • EID, SIN
    • First Name and Last Name – assuming there is no one else in the company with the same name
    • Last Name and DepartmentID – assuming two people with the same last name don’t work in the same department

    Composite key

    composite key is composed of two or more attributes, but it must be minimal.

    Using the example from the candidate key section, possible composite keys are:

    • First Name and Last Name – assuming there is no one else in the company with the same name
    • Last Name and Department ID – assuming two people with the same last name don’t work in the same department

    Primary key

    The primary key is a candidate key that is selected by the database designer to be used as an identifying mechanism for the whole entity set. It must uniquely identify tuples in a table and not be null. The primary key is indicated in the ER model by underlining the attribute.

    • A candidate key is selected by the designer to uniquely identify tuples in a table. It must not be null.
    • A key is chosen by the database designer to be used as an identifying mechanism for the whole entity set.  This is referred to as the primary key. This key is indicated by underlining the attribute in the ER model.

    In the following example, EID is the primary key:

    Employee(EID, First Name, Last Name, SIN, Address, Phone, BirthDate, Salary, DepartmentID)

    Secondary key

    secondary key is an attribute used strictly for retrieval purposes (can be composite), for example: Phone and Last Name.

    Alternate key

    Alternate keys are all candidate keys not chosen as the primary key.

    Foreign key

    A foreign key (FK) is an attribute in a table that references the primary key in another table OR it can be null. Both foreign and primary keys must be of the same data type.

    In the COMPANY database example below, DepartmentID is the foreign key:

    Employee(EID, First Name, Last Name, SIN, Address, Phone, BirthDate, Salary, DepartmentID)


    3.4: Keys is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?