7275 Keys

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Different Types of Keys in Relational Model

What are Keys?


A DBMS key is an attribute or set of an attribute which helps you to identify a row(tuple) in a
relation(table). They allow you to find the relation between two tables. Keys help you uniquely
identify a row in a table by a combination of one or more columns in that table.
Example:

Employee ID FirstName LastName


11 Andrew Johnson
22 Tom Wood
33 Alex Hale

In the above-given example, employee ID is a primary key because it uniquely identifies an


employee record. In this table, no other employee can have the same employee ID.

Why we need a Key?


Here, are reasons for using Keys in the DBMS system.
• Keys help you to identify any row of data in a table. In a real-world application, a table
could contain thousands of records. Moreover, the records could be duplicated. Keys ensure
that you can uniquely identify a table record despite these challenges.
• Allows you to establish a relationship between and identify the relation between tables
• Help you to enforce identity and integrity in the relationship.

Various Keys in Database Management System


DBMS has folwing seven types of Keys each have their different functionality:
• Super Key
• Primary Key
• Candidate Key
• Alternate Key
• Foreign Key

What is the Super key?


A superkey is a group of single or multiple keys which identifies rows in a table. A Super key may
have additional attributes that are not needed for unique identification. Example:

EmpSSN EmpNum Empname


9812345098 AB05 Shown
9876512345 AB06 Roslyn
199937890 AB07 James
In the above-given example, EmpSSN and EmpNum name are superkeys.

What is a Primary Key?


A column or group of columns in a table which helps us to uniquely identifies every row in that
table is called a primary key. This DBMS can't be a duplicate. The same value can't appear more
than once in the table.

Rules for defining Primary key:


• Two rows can't have the same primary key value
• It must for every row to have a primary key value.
• The primary key field cannot be null.
• The value in a primary key column can never be modified or updated if any foreign key
refers to that primary key.
Example:
In the following example, <code>StudID</code> is a Primary Key.

StudID Roll No First Name LastName Email


1 11 Tom Price [email protected]
2 12 Nick Wright [email protected]
3 13 Dana Natan [email protected]

What is the Alternate key?


All the keys which are not primary key are called an alternate key. It is a candidate key which is
currently not the primary key. However, A table may have single or multiple choices for the primary
key.
Example: In this table StudID, Roll No, Email are qualified to become a primary key. But since
StudID is the primary key, Roll No, Email becomes the alternative key.

StudID Roll No First Name LastName Email


1 11 Tom Price [email protected]
2 12 Nick Wright [email protected]
3 13 Dana Natan [email protected]

What is a Candidate Key?


A super key with no repeated attribute is called candidate key.
Properties of Candidate key:
• It must contain unique values
• Candidate key may have multiple attributes
• Must not contain null values
• It should contain minimum fields to ensure uniqueness
• Uniquely identify each record in a table
Example: In the given table Stud ID, Roll No, and email are candidate keys which help us to
uniquely identify the student record in the table.

StudID Roll No First Name LastName Email


1 11 Tom Price [email protected]
2 12 Nick Wright [email protected]
3 13 Dana Natan [email protected]

What is the Foreign key?


A foreign key is a column which is added to create a relationship with another table. Foreign keys
help us to maintain data integrity and also allows navigation between two different instances of an
entity. Every relationship in the model needs to be supported by a foreign key. Example:

DeptCode DeptName
001 Science
002 English
005 Computer

Teacher ID Fname Lname


B002 David Warner
B017 Sara Joseph
B009 Mike Brunton
In this example, we have two table, teach and department in a school. However, there is no way to
see which search work in which department. In this table, adding the foreign key in Deptcode to the
Teacher name, we can create a relationship between the two tables.

Teacher ID DeptCode Fname Lname


B002 002 David Warner
B017 002 Sara Joseph
B009 001 Mike Brunton
This concept is also known as Referential Integrity.
Constraints
Every relation has some conditions that must hold for it to be a valid relation.These conditions are
called Relational Integrity Constraints. There are three main integrity constraints:
•  Key constraints

•  Domain constraints

•  Referential integrity constraints

Key Constraints
There must be at least one minimal subset of attributes in the relation, which can identify a tuple
uniquely. This minimal subset of attributes is called key for that relation. If there are more than one
such minimal subsets, these are called candidate keys.
Key constraints force that:
• in a relation with a key attribute, no two tuples can have identical values for key attributes.

• a key attribute cannot have NULL values.

• Key constraints are also referred to as Entity Constraints.

Domain Constraints
Attributes have specific values in real-world scenario. For example, age can only be a positive
integer. The same constraints have been tried to employ on the attributes of a relation. Every
attribute is bound to have a specific range of values. For example, age cannot be less than zero and
telephone numbers cannot contain a digit outside 0-9.
Referential Integrity Constraints
Referential integrity constraints work on the concept of Foreign Keys. A foreign key is a key
attribute of a relation that can be referred in other relation.Referential integrity constraint states that
if a relation refers to a key attribute of a different or same relation, then that key element must exist.

You might also like