Guidelines for Selecting Primary Keys
This topic presents several recommendations for selecting the attributes that make up the primary key for a relation variable. These guidelines apply, the necessary changes being made, to all candidate keys:
Numeric keys are both easier to produce uniquely and easier to manage than character data.
Always assign numeric attributes as the primary key if the key is to be system-generated.
An intelligent key carries semantics about the tuple it identifies.
The expression is also used to describe a key constructed around one or more misprojected attributes that would not be in the relation variable were the database properly normalized.
Note that intelligent keys and natural keys are not the same thing. See “Intelligent key” and “Natural key” under “Definitions” on page 78 for details.
Because it is important to track the originating entity for attributes, always identify foreign keys with a unique code.
For example, if you use the column naming convention described in “Guidelines for Naming Columns” on page 134, you might extend the convention as indicated in the following example:
Suppose the relation Lineitem has the following primary key composed of the three attributes indicated by preliminary attribute names:
line_item |
|
|
line_item_number |
order_number |
order_year_date |
PK |
||
|
FK |
FK |
|
|
|
You might consider naming the primary key attributes as follows.
The FK embedded in order_number and order_year_date indicate that the attributes are foreign keys that reference the order relation variable.
When attribute naming has been completed, the primary key attribute names should look like the following relation definition fragment:
line_item |
|
|
line_item_number |
order_FK_number |
order_FK_year_date |
PK |
||
|
FK |
FK |
|
|
|