0% found this document useful (0 votes)
23 views11 pages

Types of Keys

Uploaded by

NiagaraPopo
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)
23 views11 pages

Types of Keys

Uploaded by

NiagaraPopo
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/ 11

Keys in DBMS

KEYS in DBMS is an attribute or set of attributes 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. Key is also helpful for finding unique record or row from the
table. Database key is also helpful for finding unique record or row from the table.
Types of Keys in DBMS
1. Primary Key
2. Super Key
3. Candidate Key
4. Foreign Key
5. Composite Key
What is a Primary Key?
PRIMARY KEY in DBMS is a column or group of columns in a table that uniquely
identify every row in that table. The Primary Key can’t be a duplicate meaning the
same value can’t appear more than once in the table. A table cannot have more than
one primary key.
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.
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.
What is a Candidate Key?
CANDIDATE KEY in SQL is a set of attributes that uniquely identify tuples in a table.
Candidate Key is a super key with no repeated attributes. The Primary key should be
selected from the candidate keys. Every table must have at least a single candidate
key. A table can have multiple candidate keys but only a single primary key.
Properties of Candidate key:
It must contain unique values
Candidate key in SQL may have multiple attributes
Must not contain null values
It should contain minimum fields to ensure uniqueness
Uniquely identify each record in a table
Candidate key 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.
What is the Foreign key?
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. It acts as a cross-reference between two tables
as it references the primary key of another table.
What is the Foreign key?
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. It acts as a cross-reference between two tables as it references the
primary key of another table.

In this key in dbms 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
What is the Foreign key?
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. It acts as a cross-reference between two tables as it references the
primary key of another table.

In this key in dbms 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
What is the Composite key?
COMPOSITE KEY is a combination of two or more columns that uniquely identify rows
in a table. The combination of columns guarantees uniqueness, though individually
uniqueness is not guaranteed. Hence, they are combined to uniquely identify records
in a table.

Difference Between Primary key & Foreign key


RIMARY KEY FOREIGN KEY

A foreign key is a column or group of columns in a


A primary key is used to ensure data in the specific
relational database table that provides a link between
column is unique.
data in two tables.

It uniquely identifies a record in the relational database It refers to the field in a table which is the primary key of
table. another table.

Only one primary key is allowed in a table. Whereas more than one foreign key is allowed in a table.

It can contain duplicate values and a table in a relational


It is a combination of UNIQUE and Not Null constraints.
database.

It does not allow NULL values . It can also contain NULL values.

Its value cannot be deleted from the parent table. Its value can be deleted from the child table.

It constraint can be implicitly defined on the temporary It constraint cannot be defined on the local or global

You might also like