L10 - Keys in Relational Database
L10 - Keys in Relational Database
Module Objective
To discuss different keys in relational database like Super Key , Candidate key ,Primary Key ,
Composite Key , Secondary Key or Alternate Key , Surrogate Key , Foreign and Unique Key
To explain different constraints like schema based or integrity , entity integrity or key ,
Referential integrity and domain constraints
2
Topics Covered
3
Session Plan - Day 10
Database keys
Objectives of Database Keys
Types of Database Keys
4
Need of Database key
In a database relation, it is easy to identify a particular (specific) column; however, we
need a mechanism (way) to identify a particular (specific) row in a database relation. For
this, the concept of database keys is used so that we can identify each row uniquely.
12
Database key
6
Type of database keys
There are mainly seven different types of Keys in DBMS and each key has its
different functionality:
7
Super Key
A super key (SK) is a set of one or more attributes that allows us to uniquely identify a
row (tuple) in the relation.
SK is a set of attributes of a relation schema R with the property that no two tuples t1 and
t2 in any relation state r of R should have the same combination of values for these
attributes. This is known as uniqueness property.
Thus, we can state that: t1[SK] ≠ t2[SK] or t1.SK ≠ t2.SK
8
Super Key(Contd..)
Consider the Schema, STUDENT(adhaar_no, roll_no, first_name, last_name, address,
dob, gender, mobile_no, email_id).
Maximum no. of possible super keys for a relation with n attributes = 2 n-1
Below are some sets of super keys for the above STUDENT schema:
SK1 = (adhaar_no) SK7 = (first_name, last_name,
SK2 = (roll_no) address, mobile_no)
SK3 = (mobile_no) SK8 = (roll_no, first_name, gender)
SK4 = (email_id) SK9 = (first_name, last_name,
SK5=(adhaar_no, first_name, last_name) address, email_id)
SK6 =(roll_no, first_name, last_name ) SK10 = (first_name, mobile_no, email_id,
dob) etc.
Thus student relation with 9 attributes maximum no. of possible super keys will be equal to
29-1.
9
Some facts to remember about super keys
A super key specifies uniqueness property for a row (tuple) i.e. t1 [SK] ≠ t2 [SK] .
Every relation has one default super key, i.e., the set of all attributes of that relation.
A super key can have extraneous attribute/s. For super key SK2 i.e.
{roll_no, first_name, last_name}, first_name and last_name are extraneous attributes. If
we remove these attributes, then also uniqueness property holds.
If SK is a super key, then any superset of SK will also be a super key.
10
Candidate Key
A candidate key is a minimal super key that can identify each row (tuple) of a given
relation uniquely.
A candidate key has a minimum number of attributes that are definitely needed to
establish a row (tuple) uniqueness in a given relation.
The set of all possible candidate keys is known as the candidate key set.
11
Some facts to remember about candidate keys
12
Primary Key
A primary key is a constraint in a table which uniquely identifies each row record in a
database table by enabling one or more the column in the table as primary key.
A relation can have many candidate keys, which can uniquely identify each row (tuple)
of a given relation. Any one of these candidate keys can be chosen as a Primary key
(PK) of a given relation.
Some facts to remember about primary keys are given below:
The value of the primary key field will always be unique
The value of a primary key field can never be NULL
A relation is allowed to have only one primary key
13
Primary Key(Contd..)
14
Composite key
A composite key is a primary key consisting of two or more columns that uniquely
identify rows in a relation.
15
Secondary Key or Alternate key
Secondary or Alternate key (s) are the candidate keys that are left unimplemented
or unused after implementing the primary key.
The secondary keys are used for data retrieval purposes and indexing. Indexing
is done for better and faster data searching and retrieval from the database.
16
Can you answer this ?
Q. Which one of the following is a set of one or more attributes taken collectively to
uniquely identify a record?
a) Candidate key
b) Sub key
c) Super key
d) Foreign key
17
Can you answer this ?
18
Can you answer this ?
Q. A _____ is a property of the entire relation, rather than of the individual tuples in
which each tuple is unique.
a) Rows
b) Key
c) Attribute
d) Fields
19
Can you answer this ?
Q. An attribute in a relation is a foreign key if the _______ key from one relation
is used as an attribute in that relation.
a) Candidate
b) Primary
c) Super
d) Sub
20
Surrogate Keys
The surrogate key is usually an integer and thus does not lend any meaning to
the data in the relation. A surrogate key is a value generated right before the
record is inserted into a relation.
When the primary key is too big or complicated, Surrogate keys are preferred. A
surrogate key is generated for each unique combination of the primary key.
21
Surrogate Keys(Contd..)
Example:
22
Foreign Key
When the primary key (PK) of one relation (also referred to as parent relation) is
included as a non-unique attribute in another relation (also referred to as child relation),
then such a database key is called a foreign key (FK).
The primary purpose of the foreign key (FK) is to define and create a relationship
between the two relations.
The relation which is being referenced is called referenced relation, and the
corresponding attribute is called a referenced attribute.
The relation which refers to the referenced relation is called referencing
relation, and the corresponding attribute is called referencing attribute.
23
Foreign Key (Contd..)
Example:
In below given figure attribute (dept_id) in course relation is a foreign key (FK),
and all values for this attribute will match the values of the primary key (PK)
attribute (dept_id) in the department relation.
24
Unique Key
Unique key constraints also identifies an individual tuple uniquely in a relation or table.
Multiple unique keys can present in a table. NULL values are allowed in case of a
unique key. These can also be used as foreign keys for another table
It can be used when someone wants to enforce unique constraints on a column and
a group of columns which is not a primary key.
25
Difference between Primary Key and Unique Key
26
Review Questions
Q 1.What are Super key, Primary key, Candidate key, and Foreign keys?
Q 2. What is the difference between Primary key and Unique key?
Q 3. What is importance of keys in Database?
27
Thank You
28