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

DBMS-Note-3

The document explains various types of keys in a Database Management System (DBMS), including Primary Key, Candidate Key, Super Key, Foreign Key, Alternate Key, and Compound Key. Each key type serves a specific purpose in ensuring data integrity and uniqueness within tables, with distinct properties and examples provided for clarity. The document also highlights the differences between these keys and their importance in maintaining relationships between tables.

Uploaded by

soumen.da.ace03
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

DBMS-Note-3

The document explains various types of keys in a Database Management System (DBMS), including Primary Key, Candidate Key, Super Key, Foreign Key, Alternate Key, and Compound Key. Each key type serves a specific purpose in ensuring data integrity and uniqueness within tables, with distinct properties and examples provided for clarity. The document also highlights the differences between these keys and their importance in maintaining relationships between tables.

Uploaded by

soumen.da.ace03
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Explanation of Keys in DBMS

In a DBMS, KEYS are attributes or sets of attributes that enable the identification of
a row or tuple within a relation or table. They establish connections between different
tables and assist in uniquely identifying a row by utilizing one or more columns in the
table.

Types of Keys in DBMS


Now we have understood what is a key in DBMS, let’s look at the major types of
keys in DBMS in detail.

1. Primary Key

This type of keys in DBMS refers to a column that uniquely identifies all the records
within that table. A table has one primary key only, and this key must not contain
repeated or duplicated values across its rows. Each value within the primary
key must be unique, with no repetitions allowed.

Applying the PK (PRIMARY KEY) constraint to a column or set of columns ensures


that they cannot have null values or duplicates. Furthermore, any foreign key that
references the primary key is unable to modify the values present in the primary key.

Properties of a Primary Key

 The Primary Key field shouldn’t be left NULL; the Primary Key column must
contain a value.
 In that column, no two rows in the table may contain identical values.
 If a foreign key in a DBMS refers to the primary Key, no value may be altered
or modified in this primary key column.

Examples of Primary Keys


In this example, the student ID is the primary key.
StudID Roll No First Name Last Name Email

1 11 Tom Prince [email protected]

2 12 Nick Wright [email protected]

3 13 Dana Natan [email protected]

Guidelines for Choosing a Primary Key

1. Uniqueness is the most crucial element while selecting this key in DBMS. It
means that this column’s value does not occur in any other table row.
2. The definition and values shouldn’t be altered. While altering a PK column
value would need updating all referenced rows in the child (related) table,
altering a PK’s columns would necessitate redefining all pertinent foreign
keys.
3. If a composite primary key is used, no single column or smaller group of
columns should be able to identify each individual uniquely.
4. Use as few columns as you can, and if you can, pick columns whose values
are simple to read and recall.

2. Candidate Key

Candidate keys play a vital role in upholding the integrity and consistency of a
database. The purpose of this key in DBMS is to guarantee each row’s uniqueness
and independent identification within a table. Additionally, candidate keys enforce
relationships between tables, ensuring data integrity and maintaining overall
database consistency.

Properties of a candidate key

 It must have distinct values.


 Create a unique identifier for each table record.

Examples of candidate keys


StudID Roll No First Name Last Name Email

1 11 Tom Prince [email protected]

2 12 Nick Wright [email protected]

3 13 Dana Natan [email protected]

The candidate keys Roll No., Stud ID, and Email in the table enable us to identify
each student record uniquely.

Differences Between Primary and Candidate keys


Primary Key Candidate Key

A primary key is a column that allows each A column that can uniquely identify
entry in a database to be identified only once. each record in a database is sometimes
It is selected as the primary key for the table referred to as a candidate key. It might
from the list of potential keys. be used as the primary key.

A candidate key is also a unique


A primary key is a unique identifier for a
identifier for a record in a database
record in a database table.
table.

There can be multiple candidate keys


There can be only one primary key in a table.
in a table.

The primary key enforces entity integrity, Candidate keys are potential candidates

The primary key is chosen by the database A candidate key can be chosen from
designer or administrator. the set of candidate keys for a table.

3. Super Key

The collection of all keys enabling us to recognize every row in the table is a super
key. This type of key in DBMS specifies that all the table columns that may identify
the columns uniquely function as the super keys.

Properties of a super key


 A super key must ensure that each record in a table is unique.
 Nevertheless, the minimal collection of characteristics that can guarantee
uniqueness should be a super key.
 Multiple subsets that are likewise regarded as super keys can exist for a
super key.

4. Foreign Key: To create connections between two accessible tables, we


employ a foreign key. Every value in a column or collection must match the primary
key in the referential table for the foreign key to function. We can preserve referential
integrity and data integrity with the aid of a foreign key.

Properties of a foreign key

 It is a key that serves as both a secondary key and a primary key in two
different tables.
 At any given time, it combines two or more relations.
 They serve as cross-references for the tables.

Examples of foreign keys

Dept code Dept name

121 Science

213 English

513 Computer

Teacher ID Fname Lname

B002 David Warner

B017 Sara Joseph


B009 Mike Brunton

In this Key in DBMS example, we have two tables: instructor and department at a
school. However, it is hard to distinguish which instructor is assigned to which
division.

We can link the two tables in this table by adding the Foreign Key in Deptcode to the
Teacher name.

Importance of foreign keys in DBMS

Data in one table is connected to another through foreign keys. To develop a manner
of cross-referencing two columns, a foreign key column in one table links to the
column in another table with unique values.

5. Alternate Key

A key in DBMS might be selected as the main Key in a table in multiple ways. Any
key that has the potential to replace the primary key but is not yet the primary key is
considered an alternate key. It’s a prospective main key that hasn’t been selected
yet.

Properties of an alternate key

 Alternate keys refer to all keys that are not main keys.
 It’s a backup key.
 It has two or more fields that allow it to recognize two or more records.
 These criteria are reiterated.

Examples of alternate keys

StudID Roll No First Name Last Name Email

1 11 Tom Prince [email protected]


2 12 Nick Wright [email protected]

3 13 Dana Natan [email protected]

StudID, Roll No., and Email serves as the main keys. However, because StudID is
the main key, Roll No. and Email is now the secondary key.

6. Compound Key
This key in DBMS contains two or more characteristics that recognise a specific
record exclusively. It’s conceivable that none of the columns in the database are
unique on their own. However, when paired with the additional column or columns,
the composite key combination becomes unique. Each record in the table is to be
uniquely identified using the database’s compound key.

Properties of a compound key

 Each record in a table must be guaranteed unique by a compound key.


 The bare minimal collection of characteristics necessary to assure uniqueness
should be a compound key.
 A compound key attribute order is essential.

Examples of compound keys

Order No Product ID Productname Quantity

B005 JAP1024 Mouse 5

B005 JHD1345 USB 10

B005 KDL0973 LCD Monitor 20

B002 FRG1204 USB 6

B004 JUW1287 Laser printer 2


Below is the example of this key in DBMS:

OrderNo and ProductID cannot be the main key since they do not uniquely identify a
record. A compound key combining the Order ID and Product ID might be utilized to
identify each record uniquely.

Importance of compound keys in DBMS

Compound keys are always constructed from two or more other tables’ primary keys.
Both keys uniquely identify data in their respective tables, but both are required to do
so in the table utilizing the compound key.

Comparison of 6 Types of keys in DBMS


Here’s a comparison between different types of keys in a relational database
management system (DBMS) presented in a table format:

Null
Key Definitio Unique Alterati
Valu Purpose Index Usage
Type n ness on
es

Uniquely Data Create May be


identifies Not integrity, s Within complex
Primar Require
each Allo record Uniqu same , impact
y Key d
record in wed identific e table other
a table. ation. Index tables.

Data
Establish
consisten More
es a
Not cy, May Betwee flexible
Foreig relations Allo
Require relations create n for
n Key hip wed
d hip Index tables mainten
between
maintena ance.
tables.
nce.

Alternati
Backup May
Candid ve Not May Within
Require primary become
ate unique Allo create same
d key primary
Key keys that wed Index table
options. key.
could be
Null
Key Definitio Unique Alterati
Valu Purpose Index Usage
Type n ness on
es

primary
keys.

Combina
tion of
Concept May Concep
Super attributes Require Allo
ual, not create tual N/A
Key that d wed
enforced. Index use
uniquely
identify.

Combine
Specializ Create May be
d
Comp Not ed s Within complex
attributes Require
osite Allo unique Comp same , impact
used as a d
Key wed identific osite table perform
single
ation. Index ance.
key.

Ensures Enforce Create


May be
column(s uniquene s Within
Uniqu Require Allo used as
) have ss, no Uniqu same
e Key d wed primary
unique primary e table
key.
values. key. Index

Artificial
Enhance
keys Create Generall
d data
Surrog assigned Not s Within y static,
Require privacy,
ate for Allo Uniqu same non-
d data
Key record wed e table changin
warehou
identific Index g.
sing.
ation.

You might also like