0% found this document useful (0 votes)
35 views

Database Keys: J M Githeko September 2019

Keys are important parts of relational databases that uniquely identify records. There are several types of keys: candidate keys, primary keys, alternate keys, foreign keys, and composite keys. Candidate keys are attributes that uniquely identify records and can be used as primary keys. The primary key is the candidate key chosen by the database designer to identify entities. Other candidate keys are called alternate keys.

Uploaded by

bethuel mulanda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Database Keys: J M Githeko September 2019

Keys are important parts of relational databases that uniquely identify records. There are several types of keys: candidate keys, primary keys, alternate keys, foreign keys, and composite keys. Candidate keys are attributes that uniquely identify records and can be used as primary keys. The primary key is the candidate key chosen by the database designer to identify entities. Other candidate keys are called alternate keys.

Uploaded by

bethuel mulanda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Database Keys

J M Githeko
September 2019
Introduction
• Keys are very important part of Relational database model.
• Keys are used to establish and identify relationships between tables
• Keys uniquely identify any record or row of data inside a table.
• A Key can be a single attribute or a group of attributes, where the
combination may act as a key.
Types of database keys
• Super Key
• Primary Key
• Candidate Key
• Alternate Key
• Foreign Key
• Compound Key
• Composite Key
• Surrogate Key
Example Table 1
student_id name phone age email

1 Akenyan 07876723452 17 [email protected]

2 Akenyan 07991165674 19 [email protected]

3 Bkenyan 07898756543 18 [email protected]

4 Ckenyan 07787867898 19 [email protected]

5 Dkenyan 07990080080 17 [email protected]


Candidate Key
• Candidate keys are defined as the minimal set of fields (attributes)
which can uniquely identify each record in a table.
• It is an attribute or a set of attributes that can act as a Primary Key for
a table to uniquely identify each record in that table.
• There can be more than one candidate key.

• In Exmple Table 1, student_id and phone both are candidate keys


Candidate Key - properties
• It must unique (or prime)
• Candidate key may have multiple attributes
• Must not contain null values
• There can be more than one candidate keys for a table.
• Must uniquely identify each record in a table (contains unique values)

• Attributes that make up a Candidate key are called prime attributes.


The other attributes in a table (relation) are called non-prime
attributes.
Super Key
• A Super Key is defined as any set of attributes within a table
(relation) that can uniquely identify each record within a table.
• Super Key is a superset of Candidate key. That is, Candidate keys are
a sub-set of Super Keys for a particular relation.

• In Example Table 1 above Super Key would include student_id,


student_id
(email, name),
name phone
• The student_id is unique
for every row of data, hence
it can be used to identity
each row uniquely.

• Combination (email, name)


name
is unique for each student
this combination can also
be a key.

• Similarly, phone number for


each student is expected to
be unique, hence again,
phone can also be a key.
Super Key vs Candidate Key

• Keys identify each tuple (record)


in a relation uniquely.
• Keys are also used to establish
the relationship among the
tables in a schema.
Cont’d

• While a Super key is any combination of columns which uniquely


identify a row in the table, a Candidate key is minimal super key from
which you cannot remove any fields else it will not be able to
uniquely identify the rows.
• Candidate keys are said to be a proper subset of super keys in any
relation.
• A Super Key can contain Candidate Keys but a Candidate Key cannot
contain a Super Key.
• A key is single field/column/attribute or a set of
fields/columns/attributes derived from the same table/relation
Primary Key
• Each Candidate Key can qualify as Primary Key.
• A Primary Key is a Candidate key that is chosen by the database designer
to identify entities with in an entity set.
• Primary key is the minimal super key, that is, the Super Key with minimal
number of attributes.
• In the ER diagram primary key is represented by underlining the primary
key attribute(s).
attribute(s)
• Ideally a primary key is composed of only a single attribute but it is
possible to have a primary key composed of more than one attribute.
• A Composite Key is a key made up of a combination of attributes. It can be
a Super Key, Candidate Key or Primary Key
Exmple Table 2
student_id First_name Last_name phone age email

1 Akenyan Mukulu 07876723452 17 [email protected]

2 Akenyan Wakala 07991165674 19 [email protected]

3 Bkenyan Nzomu 07898756543 18 [email protected]

4 Ckenyan Mekele 07787867898 19 [email protected]

5 Dkenyan Nyota 07990080080 17 [email protected]


Primary Key vs Candidate Key vs Alternate Key
• Candidate Keys:
• Student_id
• Phone
• Email
• Primary Key:
• Student_id
• Alternate Keys:
• Phone
• Email
Secondary or Alternative key

• The candidate keys which are not selected as primary key are known
as secondary keys or alternative keys.
Non-key Attributes

• Non-key attributes are the attributes or fields of a table, other than


candidate key attributes/fields in a table.
• In other words, attributes that are nt part any any candidate key are
called non-key attributes.
• In Example Table 2, First_name, Last_name and Age are non-key
attributes
Example Using Relational Calculus
• Consider a Relation or Table R1. Let A,B,C,D,E be the attributes of this relation.
R(A,B,C,D,E) is the relation

A→BCDE
• This means the attribute 'A' uniquely determines the other attributes B,C,D,E.
BC→ADE
• This means the attributes 'BC' jointly determines all the other attributes A,D,E in the
relation.

• Primary Key :A
• Candidate Keys :A, BC
• Super Keys : A, BC, ABC, AD
ABC,AD are not Candidate Keys since both are not minimal super keys.

You might also like