1. CS424 PK, FK, FD NormalizationCS424 PK, FK, FD Normalization
Primary and Foreign KeysPrimary and Foreign Keys
Primary and foreign keys are the most basicPrimary and foreign keys are the most basic
components on which relational theory iscomponents on which relational theory is
based. Primary keys enforce entity integritybased. Primary keys enforce entity integrity
by uniquely identifying entity instances.by uniquely identifying entity instances.
Foreign keys enforce referential integrity byForeign keys enforce referential integrity by
completing an association between twocompleting an association between two
entities. The next step in building the basicentities. The next step in building the basic
data model todata model to
identify and define the primary key attributes foridentify and define the primary key attributes for
each entityeach entity
validate primary keys and relationshipsvalidate primary keys and relationships
migrate the primary keys to establish foreign keysmigrate the primary keys to establish foreign keys
2. CS424 PK, FK, FD Normalization
Define Primary Key AttributesDefine Primary Key Attributes
AttributesAttributes are data items that describe anare data items that describe an
entity. Anentity. An attribute instanceattribute instance is a single valueis a single value
of an attribute for an instance of an entity. Forof an attribute for an instance of an entity. For
example, Name and hire date are attributes ofexample, Name and hire date are attributes of
the entity EMPLOYEE. "Jane Hathaway" andthe entity EMPLOYEE. "Jane Hathaway" and
"3 March 1989" are instances of the attributes"3 March 1989" are instances of the attributes
name and hire date.name and hire date.
TheThe primary keyprimary key is an attribute or a set ofis an attribute or a set of
attributes that uniquely identify a specificattributes that uniquely identify a specific
instance of an entity. Every entity in the datainstance of an entity. Every entity in the data
model must have a primary key whose valuesmodel must have a primary key whose values
uniquely identify instances of the entity.uniquely identify instances of the entity.
3. CS424 PK, FK, FD Normalization
Con’tCon’t
To qualify as a primary key for an entity,To qualify as a primary key for an entity,
an attribute must have the followingan attribute must have the following
properties:properties:
it must have a non-null value for eachit must have a non-null value for each
instance of the entityinstance of the entity
the value must be unique for each instance ofthe value must be unique for each instance of
an entityan entity
the values must not change or become nullthe values must not change or become null
during the life of each entity instanceduring the life of each entity instance
4. CS424 PK, FK, FD Normalization
candidate keycandidate key..
In some instances, an entity will have moreIn some instances, an entity will have more
than one attribute that can serve as athan one attribute that can serve as a
primary key. Any key or minimum set of keysprimary key. Any key or minimum set of keys
that could be a primary key is called athat could be a primary key is called a
candidate keycandidate key. Once candidate keys are. Once candidate keys are
identified, choose one, and only one, primaryidentified, choose one, and only one, primary
key for each entity. Choose the identifierkey for each entity. Choose the identifier
most commonly used by the user as long asmost commonly used by the user as long as
it conforms to the properties listed above.it conforms to the properties listed above.
Candidate keys which are not chosen as theCandidate keys which are not chosen as the
primary key are known as alternate keys.primary key are known as alternate keys.
5. CS424 PK, FK, FD Normalization
contcont
An example of an entity that could haveAn example of an entity that could have
several possible primary keys isseveral possible primary keys is
Employee. Let's assume that for eachEmployee. Let's assume that for each
employee in an organization there areemployee in an organization there are
three candidate keys: Employee ID, Socialthree candidate keys: Employee ID, Social
Security Number, and Name.Security Number, and Name.
6. CS424 PK, FK, FD Normalization
contcont
Name is the least desirable candidate. While it mightName is the least desirable candidate. While it might
work for a small department where it would be unlikelywork for a small department where it would be unlikely
that two people would have exactly the same name, itthat two people would have exactly the same name, it
would not work for a large organization that hadwould not work for a large organization that had
hundreds or thousands of employees. Moreover, there ishundreds or thousands of employees. Moreover, there is
the possibility that an employee's name could changethe possibility that an employee's name could change
because of marriage. Employee ID would be a goodbecause of marriage. Employee ID would be a good
candidate as long as each employee were assigned acandidate as long as each employee were assigned a
unique identifier at the time of hire. Social Security wouldunique identifier at the time of hire. Social Security would
work best since every employee is required to have onework best since every employee is required to have one
before being hired.before being hired.
7. CS424 PK, FK, FD Normalization
Composite KeysComposite Keys
Sometimes it requires more than one attribute to uniquely identify anSometimes it requires more than one attribute to uniquely identify an
entity. A primary key that made up of more than one attribute is knownentity. A primary key that made up of more than one attribute is known
as aas a composite key.composite key. Figure 1 shows an example of a composite key.Figure 1 shows an example of a composite key.
Each instance of the entity Work can be uniquely identified only by aEach instance of the entity Work can be uniquely identified only by a
composite key composed of Employee ID and Project ID.composite key composed of Employee ID and Project ID.
Employee IDEmployee ID Project IDProject ID Hours_WorkedHours_Worked
11 11 200200
11 22 120120
22 33 142142
22 66 2424
33 55 5454
33 99 2525
Example of Composite Key
8. CS424 PK, FK, FD Normalization
Primary Key MigrationPrimary Key Migration
Dependent entities, entities that dependDependent entities, entities that depend
on the existence of another entity for theiron the existence of another entity for their
identification, inherit the entire primary keyidentification, inherit the entire primary key
from the parent entity. Every entity within afrom the parent entity. Every entity within a
generalization hierarchy inherits thegeneralization hierarchy inherits the
primary key of the root generic entity.primary key of the root generic entity.
9. CS424 PK, FK, FD Normalization
Define Key AttributesDefine Key Attributes
Once the keys have been identified for the model, itOnce the keys have been identified for the model, it
is time to name and define the attributes that haveis time to name and define the attributes that have
been used as keys.been used as keys.
There is no standard method for representingThere is no standard method for representing
primary keys in ER diagrams. For this document, theprimary keys in ER diagrams. For this document, the
name of the primary key followed by the notationname of the primary key followed by the notation
(PK) is written inside the entity box.(PK) is written inside the entity box.
Entities with Key Attributes
10. CS424 PK, FK, FD Normalization
Validate Keys and RelationshipsValidate Keys and Relationships
Basic rules governing the identification and migration ofBasic rules governing the identification and migration of
primary keys are:primary keys are:
Every entity in the data model shall have a primary key whoseEvery entity in the data model shall have a primary key whose
values uniquely identify entity instances.values uniquely identify entity instances.
The primary key attribute cannot be optional (i.e., have nullThe primary key attribute cannot be optional (i.e., have null
values).values).
The primary key cannot have repeating values. That is, theThe primary key cannot have repeating values. That is, the
attribute may not have more than one value at a time for a givenattribute may not have more than one value at a time for a given
entity instance is prohibited. This is known as the No Repeatentity instance is prohibited. This is known as the No Repeat
Rule.Rule.
Entities with compound primary keys cannot be split intoEntities with compound primary keys cannot be split into
multiple entities with simpler primary keys. This is called themultiple entities with simpler primary keys. This is called the
Smallest Key Rule.Smallest Key Rule.
Two entities may not have identical primary keys with theTwo entities may not have identical primary keys with the
exception of entities within generalization hierarchies.exception of entities within generalization hierarchies.
The entire primary key must migrate from parent entities to childThe entire primary key must migrate from parent entities to child
entities and from supertype, generic entities, to subtypes,entities and from supertype, generic entities, to subtypes,
category entities.category entities.
11. CS424 PK, FK, FD Normalization
Foreign KeysForeign Keys
AA foreign keyforeign key is an attribute that completesis an attribute that completes
a relationship by identifying the parenta relationship by identifying the parent
entity. Foreign keys provide a method forentity. Foreign keys provide a method for
maintaining integrity in the data (calledmaintaining integrity in the data (called
referential integrity) and for navigatingreferential integrity) and for navigating
between different instances of an entity.between different instances of an entity.
Every relationship in the model must beEvery relationship in the model must be
supported by a foreign key.supported by a foreign key.
12. CS424 PK, FK, FD Normalization
Identifying Foreign KeysIdentifying Foreign Keys
Every dependent and category (subtype)Every dependent and category (subtype)
entity in the model must have a foreign keyentity in the model must have a foreign key
for each relationship in which itfor each relationship in which it
participates. Foreign keys are formed inparticipates. Foreign keys are formed in
dependent and subtype entities bydependent and subtype entities by
migrating the entire primary key from themigrating the entire primary key from the
parent or generic entity. If the primary keyparent or generic entity. If the primary key
is composite, it may not be split.is composite, it may not be split.
Foreign keys attributes are indicated byForeign keys attributes are indicated by
the notation (FK) beside themthe notation (FK) beside them
13. CS424 PK, FK, FD Normalization
SummarySummary
Primary and foreign keys are the mostPrimary and foreign keys are the most
basic components on which relationalbasic components on which relational
theory is based. Each entity must have atheory is based. Each entity must have a
attribute or attributes, the primary key,attribute or attributes, the primary key,
whose values uniquely identify eachwhose values uniquely identify each
instance of the entity. Every child entityinstance of the entity. Every child entity
must have an attribute, the foreign key,must have an attribute, the foreign key,
that completes the association with thethat completes the association with the
parent entity.parent entity.