SlideShare a Scribd company logo
1 of 13
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
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.
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
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.
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.
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.
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
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.
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
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.
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.
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
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.

More Related Content

Similar to Part 3 primary and foreign keys

Types of Database Keys
Types of Database KeysTypes of Database Keys
Types of Database Keysandyr91
 
Data modelingpresentation
Data modelingpresentationData modelingpresentation
Data modelingpresentationfikirabc
 
Understanding 'Authentication' and 'Identity Federation'
Understanding 'Authentication' and 'Identity Federation'Understanding 'Authentication' and 'Identity Federation'
Understanding 'Authentication' and 'Identity Federation'Naohiro Fujie
 
Database management systems 3 - Data Modelling
Database management systems 3 - Data ModellingDatabase management systems 3 - Data Modelling
Database management systems 3 - Data ModellingNickkisha Farrell
 
How to Draw an Effective ER diagram
How to Draw an Effective ER diagramHow to Draw an Effective ER diagram
How to Draw an Effective ER diagramTech_MX
 
Keys in Database Management System
Keys in Database Management SystemKeys in Database Management System
Keys in Database Management SystemAnkit Rai
 
Free video lectures for mca
Free video lectures for mcaFree video lectures for mca
Free video lectures for mcaEdhole.com
 
Entity-Relationship Data Model
Entity-Relationship Data ModelEntity-Relationship Data Model
Entity-Relationship Data ModelBishrul Haq
 

Similar to Part 3 primary and foreign keys (10)

Types of Database Keys
Types of Database KeysTypes of Database Keys
Types of Database Keys
 
Data modelingpresentation
Data modelingpresentationData modelingpresentation
Data modelingpresentation
 
Understanding 'Authentication' and 'Identity Federation'
Understanding 'Authentication' and 'Identity Federation'Understanding 'Authentication' and 'Identity Federation'
Understanding 'Authentication' and 'Identity Federation'
 
Database management systems 3 - Data Modelling
Database management systems 3 - Data ModellingDatabase management systems 3 - Data Modelling
Database management systems 3 - Data Modelling
 
How to Draw an Effective ER diagram
How to Draw an Effective ER diagramHow to Draw an Effective ER diagram
How to Draw an Effective ER diagram
 
SQL_DBMS_KEYS.pptx
SQL_DBMS_KEYS.pptxSQL_DBMS_KEYS.pptx
SQL_DBMS_KEYS.pptx
 
Ermodeling
ErmodelingErmodeling
Ermodeling
 
Keys in Database Management System
Keys in Database Management SystemKeys in Database Management System
Keys in Database Management System
 
Free video lectures for mca
Free video lectures for mcaFree video lectures for mca
Free video lectures for mca
 
Entity-Relationship Data Model
Entity-Relationship Data ModelEntity-Relationship Data Model
Entity-Relationship Data Model
 

Recently uploaded

CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 

Recently uploaded (20)

CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 

Part 3 primary and foreign keys

  • 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.