Keys are used to establish relationships between tables and uniquely identify records. The main types of keys are:
- Primary key - uniquely identifies each record and is never null; every table must have a primary key.
- Foreign key - references a primary key in another table to link the tables; ensures referential integrity.
- Candidate key - minimum set of fields that uniquely identify records; one is selected as the primary key while others are alternate keys.
2. KEYS
Used to establish and identify relation between
tables
Each record with in a table can be uniquely
identified bye combination of one more fields in a
table
Help to enforce integrity and identify relationship
Prepared by Visakh V, Assistant
Professor,Dept. of CSE, LBSITW
3. Different types of keys
1. Super Key
2. Candidate Key
3. Primary Key
4. Foreign Key
5. Secondary / Alternate key
6. Simple Key
7. Compound Key
8. Composite Key
Prepared by Visakh V, Assistant
Professor,Dept. of CSE, LBSITW
4. Super Key
A Super key is any combination of fields
within a table that uniquely identifies each
record within that table.
Superset of candidate key
Prepared by Visakh V, Assistant
Professor,Dept. of CSE, LBSITW
5. Candidate Key
A subset of a super key.
A candidate key is a single field or the least
combination of fields that uniquely identifies
each record in the table.
The least combination of fields distinguishes
a candidate key from a super key. (ie.Minimal
Set of Super Key)
Every table must have at least one candidate
key but at the same time can have several.
Prepared by Visakh V, Assistant
Professor,Dept. of CSE, LBSITW
7. In order to be eligible for a candidate key it
must pass certain criteria
I. It must contain unique values
II. Must not contain null values
III.Contain minimum number of fields to
ensure uniqueness
IV. Must uniquely identify each record in a
table
Prepared by Visakh V, Assistant
Professor,Dept. of CSE, LBSITW
Note: Once your candidate keys have been identified
you can now select one to be your primary key
8. Primary Key
Prepared by Visakh V, Assistant
Professor,Dept. of CSE, LBSITW
A primary key is a candidate key that is most
appropriate to be the main reference key for the
table.
It is the primary key of reference for the table
It is used throughout the database to help
establish relationships with other tables.
10. Prepared by Visakh V, Assistant
Professor,Dept. of CSE, LBSITW
Primary key must contain
• Unique values
• Must never be null
• Uniquely identify each record in the
table.
• Note : Primary keys are mandatory for
every table each record must have a value
for its primary key.
• Note: When choosing a primary key from
the pool of candidate keys always choose a
single simple key over a composite key.
11. Prepared by Visakh V, Assistant
Professor,Dept. of CSE, LBSITW
Foreign Key
Foreign key is a column(s) that references a
column(s) of a table and it can be same table also
A foreign key is generally a Unique key from one
table that appears as a field in another where the
first table has a relationship to the second.
In other words, if we had a table A with a
Unique key X that linked to a table B where X was a
field in B, then X would be a foreign key in B.
12. Prepared by Visakh V, Assistant
Professor,Dept. of CSE, LBSITW
This relationship ensures
• Records cannot be inserted into a detail table if
corresponding record in the master table do not exist.
• Record of a master table cannot be deleted if
corresponding records in the detail table actually exist
13. Prepared by Visakh V, Assistant
Professor,Dept. of CSE, LBSITW
Parent
(or)
Master Table
Child
(or) Detail
table
14. Prepared by Visakh V, Assistant
Professor,Dept. of CSE, LBSITW
Parent must be UNIQUE OR PRIMARY KEY
Child may have DUPLICATE/NULL unless it is
specified
Constraint specified on child not on parent
Parent record can delete only if no child
record exist
Parent cannot modify if child record exist
15. Prepared by Visakh V, Assistant
Professor,Dept. of CSE, LBSITW
Secondary / Alternate Key
A table may have one or more choices for
the primary key. Collectively these are
known as candidate keys.
One is selected as the primary key. Those
not selected are known as secondary keys
or alternative keys.
16. Prepared by Visakh V, Assistant
Professor,Dept. of CSE, LBSITW
Simple Key
A simple key consists of a single field to uniquely
identify a record.
In addition the field in itself cannot be broken
down into other fields
17. Prepared by Visakh V, Assistant
Professor,Dept. of CSE, LBSITW
Compound Key
A compound key consists of more than one
field to uniquely identify a record.
Each attribute that makes up the compound
key is a simple key in its own right.
18. Prepared by Visakh V, Assistant
Professor,Dept. of CSE, LBSITW
Composite Key
A composite key consists of more than one
field to uniquely identify a record.
This differs from a compound key in that one
or more of the attributes, which make up the
key, are not simple keys in their own right