0% found this document useful (0 votes)
3 views28 pages

L10 - Keys in Relational Database

The document provides an overview of keys in a relational database, detailing their types such as Super Key, Candidate Key, Primary Key, Composite Key, Secondary Key, Surrogate Key, Foreign Key, and Unique Key. It explains the purpose of these keys in uniquely identifying rows in a database and discusses related constraints like entity integrity and referential integrity. Additionally, it includes a session plan and a case study on SMS design with constraints.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views28 pages

L10 - Keys in Relational Database

The document provides an overview of keys in a relational database, detailing their types such as Super Key, Candidate Key, Primary Key, Composite Key, Secondary Key, Surrogate Key, Foreign Key, and Unique Key. It explains the purpose of these keys in uniquely identifying rows in a database and discusses related constraints like entity integrity and referential integrity. Additionally, it includes a session plan and a case study on SMS design with constraints.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Keys in a Relational Database

Module Objective

To describe the structure of Relational Model .

To understand database schema , relational model objects and characteristics of relations

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

To discuss SMS Case study with all constraints

To understand basic concepts of SQL

2
Topics Covered

Day 9 Day 10 Day 11 Day 12


• Structure of • Keys in • Constraints • SMS case
Relational Relational • Schema Based Study design
Model Database Constraints or with complete
• Database • Super Key Integrity Constraints
Schema • Candidate Key Constraints • Introduction to
• Relational • Primary Key • Entity Integrity SQL
Model of • Composite Key Constraint or
Objects • Secondary Key Key Constraint
• Characteristics or Alternate Key • Referential
of Relations • Surrogate Keys Integrity
• Foreign and Constraints
• Domain
Unique Key
constraint

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

A database key (database relation key) is an attribute or a set of attributes in a


particular relation that can uniquely identify each row (tuple) in that relation.
 Database keys have three primary objectives given in below figure.

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

Example:1.(roll_no, first_name, last_name)


2.(roll_no)
We can say that t1[roll_no, first_name, last_name] ≠ t2[roll_no, first_name, last_name]
similarly, t1[roll_no] ≠ t2[roll_no].

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.

Example: Consider STUDENT(adhaar_no, roll_no, first_name, last_name, address,


dob, gender, mobile_no, email_id) below are the candidate keys:

CK1=(adhaar_no), CK2 = (roll_no) , CK3 = (first_name, last_name,address),


CK4 = (email_id)

11
Some facts to remember about candidate keys

 All the attributes in a candidate key are sufficient as well as necessary to


identify each tuple uniquely.​
 Removing any attribute from the candidate key fails in identifying each tuple
uniquely.​
 The value of the candidate key must always be unique.​
 The value of the candidate key can never be NULL.​
 It is possible to have multiple candidate keys in a relation.​
 Those attributes which appear in any candidate key are called prime
attributes.​
 A candidate key is a super key but not vice versa.

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

The primary key should be chosen such that:

 Its attributes are never, or very rarely, changed ,


i.e., time-invariant.

 It should consist of either a single attribute or a


small number of attributes.

14
Composite key
A composite key is a primary key consisting of two or more columns that uniquely
identify rows in a relation.

 The combination of columns guarantees uniqueness, though individually uniqueness is


not guaranteed. Hence, they are combined to identify records in a relation uniquely.

Example: Consider Section table


composite key consisting of
(section_name) and (dept_id).

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.

 If in the student relation, (roll_no) is designated as a primary key, the remaining


candidate keys – (adhaar_no), (email_id), (first_name, last_name, address) are the
secondary keys or alternate keys of the student relation.

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 ?

2. The subset of a super key is a candidate key under what condition?

a) No proper subset is a super key


b) All subsets are super keys
c) Subset is a super key
d) Each subset is a super key

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

A surrogate key is a system-generated value with no business meaning that is


used to identify a record in a relation uniquely. This key is used as a primary key
in a given relation when a natural primary key is not available.

 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:

 key comprising of a combination of various components of address


(house_no, street_name, city, state, Pincode).
 A surrogate key can be introduced as address_id for each unique combination
of address (house_no, street_name, city, state, Pincode).

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.

Example: In Department relation, dept_name is a unique key and in Course relation,


course_name is a unique 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

You might also like