What is the degree and cardinality of this relation?
Keys
keys
super
primary
Foreign
candidate
Why we need Keys
• Keys help you to identify any row of data in a table. In a real-world application, a
table could contain thousands of records.
• Records could be duplicated. Keys ensure that you can uniquely identify a table
record despite these challenges.
• Allows you to establish and identify relationship between tables
• Help you to enforce integrity in the relationship.
Types of Keys in Database Management System
• Super Key - A super key is a group of single or multiple attributes which
identifies rows in a table uniquely.
• Candidate Key - is a set of attributes that uniquely identify tuples in a table.
Candidate Key is a super key with no extraneous attributes.
• Primary Key - is a column or group of columns in a table that uniquely identify
every row in that table.
• Foreign Key - is a column that creates a relationship between two tables. The
purpose of Foreign keys is to maintain data integrity and allow navigation
between two different instances of an entity.
Keys
Super key
• A super key is a set of one of more columns (attributes) to uniquely identify
rows in a table.
Pancard_No First_name Last_name DOB City
{ {Pancard_No} , {Pancard_No , First_name} , {Pancard_No , Last_name}
{Pancard_No , DOB} , {Pancard_No , City} , {First_name, Last_name,DOB}
{Pancard_No , First_name , Last_name } ,{First_name, Last_name , City}
{First_name, Last_name , City , DOB} }
Candidate key
• A super key is a set of one of more columns (attributes) to uniquely identify
rows in a table. Candidate key is minimal Super Key. When extraneous attribute
of super key is eliminated, it results in candidate key
Pancard_No First_name Last_name DOB City
Super Key-{ {Pancard_No} , {Pancard_No , First_name} , {Pancard_No , Last_name},{Pancard_No , DOB} ,
{Pancard_No , City} , {First_name, Last_name,DOB},{Pancard_No ,
First_name , Last_name}, {First_name, Last_name , City},{First_name,
Last_name , City , DOB} }
Candidate key-{{Pancard_No} , {Pancard_No , First_name} , {Pancard_No , Last_name},{Pancard_No , DOB}
{Pancard_No , City},{First_name, Last_name,DOB}, {Pancard_No ,
First_name , Last_name } {First_name, Last_name , City}, {First_name,
Last_name , City , DOB} }
Therefore Candidate keys are {{Pancard_No} , {First_name, Last_name,DOB}, {First_name, Last_name , City}}
Clarification:The super key {Pancard_No , First_name} is not a candidate key because, Pancard_No itself is
sufficient for identifying the tuples uniquely, First_Name is extraneaous .
Primary key
Pancard_
No
First_na
me
Last_na
me
DOB City
ABC Jim Mathew 19-10-
1998
Coimba
tore
KLM Lilli Rose 11-02-
1999
Coimba
tore
PQR Jim Vijay 01-01-
2000
Bangalo
re
RRR Srijay Mathew 19-10-
1998
Hydera
bad
• Several distinct set of attributes
could serve as candidate keys.
• The candidate key which is primarily
chosen by the database designer is
the primary key.
Pancard
_No
Primary key
A primary is a column or set of columns in a table that uniquely identifies tuples (rows) in that table.
Aadhar No
License No
Passport_No
Voter_Id
Pancard_No
Composite Primary key
When Primary key has more than one attribute it is termed as Composite Primary
key
First_name Last_name DOB City
Jim Mathew 19-10-1998 Coimbatore
Lilli Rose 11-02-1999 Coimbatore
Jim Vijay 01-01-2000 Bangalore
Srijay Mathew 19-10-1998 Hyderabad
Which attribute can be a pk ?
Can you identify Super, candidate and primary keys for the
given relation?
Super keys:
● {Roll_no}
● {Registration_no}
● {Roll_no, Registration_no},
● {Roll_no, Name}
● {Name, Registration_no}
● {Roll_no, Name, Registration_no}
Candidate keys:
● {Roll_no}: This key doesn't have any redundant or
repeating attribute. So, it can be considered as a candidate
key.
● {Registration_no}: This key also doesn't have any
repeating attribute. So, it can be considered as a candidate
key.
● {Roll_no, Registration_no}: This key cannot be considered
as a candidate key because when we take the subset of this
key we get two attributes i.e Roll_no or Registration_no.
Each of these attributes is the candidate key. So, it is not a
minimal super key. Hence, this key is not a candidate key.
● {Roll_no, Name}: This key cannot be considered as a
candidate key because when we take the subset of
this key we get two attributes i.e. Roll_no or Name.
Roll_no is a candidate key. So, it is not a minimal
super key. Hence, this key is not a candidate key.
● {Name, Registration_no}: This key cannot be
considered as a candidate key because when we take
the subset of this key we get two attributes i.e
Registration_no or Name. Registration_no is a
candidate key. So, it is not a minimal super key.
Hence, this key is not a candidate key.
● {Roll_no, Name, Registration_no}: This key cannot
be considered as a candidate key because when we
take the subset of this key we get three attributes i.e
Roll_no, Registration_no and Name. Two of these
attributes i.e Roll_no and Registration_no are the
candidate key. So, it is not a minimal superkey. Hence,
this key is not a candidate key.
● So, from the above discussion, we conclude that we
can have only 2 out of above 6 super keys as the
candidate key. i.e. (Roll_no) and(Registration_no).
Primary key:
● There are two candidate keys i.e (Roll_no) and (Registration_no). From this set, we can select
any key as the primary key for our table. It depends upon our requirement. Here, if we are
talking about class then selecting ‘Roll_no’ as the primary key is more logical instead of
‘Registration_no’.
Foreign Keys
● A relation, say r1, may include among its attributes
the primary key of another relation, say r2.
● This attribute is called as foreign key from r1,
referencing to r2.
● The relation r1 is called as the referencing relation
and r2 is called the referenced relation.
● The foreign key forms a dependency between r1
and r2.
● Eg. dept_name attribute, from instructor relation to
department relation.
Instructor
Department
Foreign Key
● A FOREIGN KEY is a key used to link two tables together.
● A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in
another table.
Course_enrollment Student
Course_id Stud_id
C01 101
CO2 102
CO3 101
CO5 102
CO6 103
CO7 102
Stud_id Name Age
101 John 21
102 Arya 18
103 Bran 19
104 Amanda 18
In the below example
the Stud_Id column
in Course_enrollment table is
a foreign key as it points to
the primary key of the
Student table.
Schema Diagram
● The database schema and its keys can be visualized in Schema Diagram.
Summary
 Super Key
 Candidate Kay
 Primary Key
 Composite Key
 Foreign Key
Resource
● Text Book:
Silberschatz A Korth H F and SudharshanS , “Database System Concepts”, 6th Edition, TMH publishing
company limited, 2011.
7th edition of text book is released in 2019 https://www.db-book.com/db7/index.html
6th edition Textbook Link https://www.db-book.com/db6/index.html
○In 7th
edition , Chapter 2-Refer 2.3,2.4 for Keys and Schema diagram
THANK YOU

L5_Keys in DBMS Database Systems and Programming

  • 3.
    What is thedegree and cardinality of this relation?
  • 4.
  • 5.
    Why we needKeys • Keys help you to identify any row of data in a table. In a real-world application, a table could contain thousands of records. • Records could be duplicated. Keys ensure that you can uniquely identify a table record despite these challenges. • Allows you to establish and identify relationship between tables • Help you to enforce integrity in the relationship.
  • 6.
    Types of Keysin Database Management System • Super Key - A super key is a group of single or multiple attributes which identifies rows in a table uniquely. • Candidate Key - is a set of attributes that uniquely identify tuples in a table. Candidate Key is a super key with no extraneous attributes. • Primary Key - is a column or group of columns in a table that uniquely identify every row in that table. • Foreign Key - is a column that creates a relationship between two tables. The purpose of Foreign keys is to maintain data integrity and allow navigation between two different instances of an entity.
  • 7.
  • 8.
    Super key • Asuper key is a set of one of more columns (attributes) to uniquely identify rows in a table. Pancard_No First_name Last_name DOB City { {Pancard_No} , {Pancard_No , First_name} , {Pancard_No , Last_name} {Pancard_No , DOB} , {Pancard_No , City} , {First_name, Last_name,DOB} {Pancard_No , First_name , Last_name } ,{First_name, Last_name , City} {First_name, Last_name , City , DOB} }
  • 9.
    Candidate key • Asuper key is a set of one of more columns (attributes) to uniquely identify rows in a table. Candidate key is minimal Super Key. When extraneous attribute of super key is eliminated, it results in candidate key Pancard_No First_name Last_name DOB City Super Key-{ {Pancard_No} , {Pancard_No , First_name} , {Pancard_No , Last_name},{Pancard_No , DOB} , {Pancard_No , City} , {First_name, Last_name,DOB},{Pancard_No , First_name , Last_name}, {First_name, Last_name , City},{First_name, Last_name , City , DOB} } Candidate key-{{Pancard_No} , {Pancard_No , First_name} , {Pancard_No , Last_name},{Pancard_No , DOB} {Pancard_No , City},{First_name, Last_name,DOB}, {Pancard_No , First_name , Last_name } {First_name, Last_name , City}, {First_name, Last_name , City , DOB} } Therefore Candidate keys are {{Pancard_No} , {First_name, Last_name,DOB}, {First_name, Last_name , City}} Clarification:The super key {Pancard_No , First_name} is not a candidate key because, Pancard_No itself is sufficient for identifying the tuples uniquely, First_Name is extraneaous .
  • 10.
    Primary key Pancard_ No First_na me Last_na me DOB City ABCJim Mathew 19-10- 1998 Coimba tore KLM Lilli Rose 11-02- 1999 Coimba tore PQR Jim Vijay 01-01- 2000 Bangalo re RRR Srijay Mathew 19-10- 1998 Hydera bad • Several distinct set of attributes could serve as candidate keys. • The candidate key which is primarily chosen by the database designer is the primary key. Pancard _No
  • 11.
    Primary key A primaryis a column or set of columns in a table that uniquely identifies tuples (rows) in that table. Aadhar No License No Passport_No Voter_Id Pancard_No
  • 12.
    Composite Primary key WhenPrimary key has more than one attribute it is termed as Composite Primary key First_name Last_name DOB City Jim Mathew 19-10-1998 Coimbatore Lilli Rose 11-02-1999 Coimbatore Jim Vijay 01-01-2000 Bangalore Srijay Mathew 19-10-1998 Hyderabad Which attribute can be a pk ?
  • 13.
    Can you identifySuper, candidate and primary keys for the given relation?
  • 14.
    Super keys: ● {Roll_no} ●{Registration_no} ● {Roll_no, Registration_no}, ● {Roll_no, Name} ● {Name, Registration_no} ● {Roll_no, Name, Registration_no} Candidate keys: ● {Roll_no}: This key doesn't have any redundant or repeating attribute. So, it can be considered as a candidate key. ● {Registration_no}: This key also doesn't have any repeating attribute. So, it can be considered as a candidate key. ● {Roll_no, Registration_no}: This key cannot be considered as a candidate key because when we take the subset of this key we get two attributes i.e Roll_no or Registration_no. Each of these attributes is the candidate key. So, it is not a minimal super key. Hence, this key is not a candidate key. ● {Roll_no, Name}: This key cannot be considered as a candidate key because when we take the subset of this key we get two attributes i.e. Roll_no or Name. Roll_no is a candidate key. So, it is not a minimal super key. Hence, this key is not a candidate key. ● {Name, Registration_no}: This key cannot be considered as a candidate key because when we take the subset of this key we get two attributes i.e Registration_no or Name. Registration_no is a candidate key. So, it is not a minimal super key. Hence, this key is not a candidate key. ● {Roll_no, Name, Registration_no}: This key cannot be considered as a candidate key because when we take the subset of this key we get three attributes i.e Roll_no, Registration_no and Name. Two of these attributes i.e Roll_no and Registration_no are the candidate key. So, it is not a minimal superkey. Hence, this key is not a candidate key. ● So, from the above discussion, we conclude that we can have only 2 out of above 6 super keys as the candidate key. i.e. (Roll_no) and(Registration_no).
  • 15.
    Primary key: ● Thereare two candidate keys i.e (Roll_no) and (Registration_no). From this set, we can select any key as the primary key for our table. It depends upon our requirement. Here, if we are talking about class then selecting ‘Roll_no’ as the primary key is more logical instead of ‘Registration_no’.
  • 16.
    Foreign Keys ● Arelation, say r1, may include among its attributes the primary key of another relation, say r2. ● This attribute is called as foreign key from r1, referencing to r2. ● The relation r1 is called as the referencing relation and r2 is called the referenced relation. ● The foreign key forms a dependency between r1 and r2. ● Eg. dept_name attribute, from instructor relation to department relation.
  • 17.
  • 18.
    Foreign Key ● AFOREIGN KEY is a key used to link two tables together. ● A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table. Course_enrollment Student Course_id Stud_id C01 101 CO2 102 CO3 101 CO5 102 CO6 103 CO7 102 Stud_id Name Age 101 John 21 102 Arya 18 103 Bran 19 104 Amanda 18 In the below example the Stud_Id column in Course_enrollment table is a foreign key as it points to the primary key of the Student table.
  • 19.
    Schema Diagram ● Thedatabase schema and its keys can be visualized in Schema Diagram.
  • 20.
    Summary  Super Key Candidate Kay  Primary Key  Composite Key  Foreign Key
  • 21.
    Resource ● Text Book: SilberschatzA Korth H F and SudharshanS , “Database System Concepts”, 6th Edition, TMH publishing company limited, 2011. 7th edition of text book is released in 2019 https://www.db-book.com/db7/index.html 6th edition Textbook Link https://www.db-book.com/db6/index.html ○In 7th edition , Chapter 2-Refer 2.3,2.4 for Keys and Schema diagram
  • 22.