Keys

Relational Data Modeling

Entity instance indentifiers and references.

Page contents

Overview

Keys are attributes (or combinations of attributes) in an entity that identify instances of that or another entity. The declaration of a key on an attribute of an entity implicitly places a constraint on the values that instances of that entity may have for the given attribute. For example, declaring an attribute as a primary key or unique key of an entity also declares that no two instances of that entity may have the same value for that attribute. Similarly, declaring an attribute of an entity as a foreign key, referencing a primary key or unique key of another entity, also declares that no instance of the first entity may have a value for that attribute that doesn’t match the specified primary or unique key attribute value in an instance of the second entity.

Primary keys

A primary key is an attribute (or combination of attributes) designated to uniquely identify (and distinguish between) instances of an entity. Strong entities are required to have primary keys; in practice, weak entities usually have primary keys as well.

Primary keys must follow some rules quite strictly:

In addition, there are additional rules and guidelines that are either enforced by most database management systems, or followed widely in practice:

In the example below, repeated from the previous page, student_id is the attribute designated as the primary key. As described previously, it is shown in bold, with the PK indicator.

Attributes, including primary key

Unique keys

A unique key is an attribute (or combination of attributes), other than that used for a primary key, that uniquely identifies an entity instance. While unique keys and primary keys are similar in some ways, the rules and practical guidelines for unique keys are less strict than for primary keys.

In the classroom management example, it might be useful to declare the name attribute of the course entity as a unique key, to avoid potential confusion caused by multiple courses having the same name; we might represent this as follows. (Notice the use of the UQ1 indicator on the name attribute. Also, note that the attributes of course are still rather minimal; we’ll be adding to them in later examples.)

Attributes, including unique key

Foreign keys

A foreign key is an attribute (or combination of attributes) of an entity that references another entity (or, in some cases, the same entity). That is, every instance of the first entity has a value for the foreign key attribute that matches a unique identifier (usually the primary key value) of an instance of the second entity. Putting it another way: foreign keys—and more specifically, the constraints implied by them—effectively define relatonships between entities.1

Of course, there are strict rules and some practical guidelines for foreign keys:

Let’s look again at the student-advisor relationship mentioned previously. This is a many-to-one relationship: multiple students may be assigned to any single advisor, and exactly one advisor is assigned to any given student. Thus, the foreign key would be in the many side of the relationship—that is, in the student entity.

Relationship showing foreign key

Note that in this ERD fragment, the relationship is shown as an optional-many-to-mandatory-one. This implies that any given student will always have exactly one advisor, but an advisor might spend some period of time without any students assigned. Also, notice that while it may feel natural to do so, it is not necessary to draw the relationship line from the foreign key to the referenced primary (or unique) key; it is only ncessary to draw it between the related entities.

Composite keys

As noted above, a primary key, unique key, or foreign key may be declared for a combination of attributes, rather than a single attribute; such a key is a composite key (in addition to being a primary key, unique key, or foreign key).

It’s fairly common for a composite key to be used as a unique key. However, composite keys are virtually never used as primary keys of strong entities; they are somewhat more commonly used as primary keys of weak entities, and much more commonly as primary keys of associative entities.

  1. It’s possible to have a relationship between entities that is not supported or enforced by a foreign key. For example, this is sometimes the case when accessing a legacy database—especially one implemented in a lightweight RDBMS (e.g., outdated versions of SQLite or MySQL). However, when implementing a new data model, virtually all relationships should be enforced by foreign keys.