Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Keys in DBMS | Core Computer Science

Ujjwal Abhishek
Ujjwal Abhishek

What are Keys and why it is needed?

Keys in the relational model of the database are the set of attributes that help in uniquely identifying a row or tuple in a table. Keys help in identifying a particular data in a table by uniquely identifying a tuple. They also help in identifying the relationship between different tables.

Types of Keys 

There are various types of keys in the relational model of the database.

student     
roll_nonamestreamageaadhar_nomob_no
1AshishCSE2223747xxxxxxx8789086342
2AnkitIT2143747xxxxxxx8969505123
3Prashant CSE2175373xxxxxxx6299912752
4UjjwalCSE2132793xxxxxxx7808516143
5PrashantIT2064535xxxxxxx 

 

enrollment  
roll_nonamecourse
1AshishDBMS
2AnkitOS
1AshishCN
Candidate Key

It is the minimal set of attributes that can identify a tuple uniquely. A candidate key must have a unique value in each row. A candidate key can't have a NULL value in any row. 

eg. In the above table ‘student’, roll_no is a candidate key as it has distinct values in each row and it is not null in any row. Other candidate keys in this table are aadhar_no, (name, stream).

(name, stream) can be called a composite candidate key as they both together can uniquely identify a tuple.

Super Key

It is the set of attributes that can uniquely identify a tuple. The only difference between Candidate Key and Super Key is Candidate Key is the minimal set of attributes that can identify a tuple. Every candidate key is a super key but every super key is not a candidate key.

Any number of attributes added to a candidate key will be a super key.

eg. In the above-given table ‘student’, roll_no, aadhar_no is a candidate key, and (roll_no, Aadhar Number is a super key.

Primary Key

A Primary Key is a set of attributes that can uniquely identify the tuples. It is one of the candidate keys which is most suitable to identify a tuple. The value of the primary key in any row can't be null. There can be only one primary key in a table.

eg. 

In the above-given table ‘student’, out of all candidate keys (roll_no, aadhar_no, etc), roll_no is the most suitable attribute for the primary key as it can never be the same for two students and it can't be null. So, it can be chosen as the primary key of the table.

Alternate Key

All other candidate keys except the primary key are called Alternate Key.

eg. In the above-given table ‘student’, all candidate keys except roll_no which is a primary key is alternate key. aadhar_no is a candidate key but not a primary key so it is an alternate key.

Foreign Key

It is the set of attributes that establishes a relationship between two tables. It refers to a column of a table that is the primary key of that table. The foreign key may have duplicate values and can also be null. The attribute which refers to the primary key of another table is called the foreign key.

eg. In the above-given table ‘enrollment’, roll_no is a foreign key to the roll_no in the ‘student’ table. It refers to the roll_no attribute of the student table which is the primary key in that table. 

Unique Key

It also identifies a tuple of a relation uniquely. A table can have more than one unique key. There can be one null in a unique key column. All values in this column must be unique.

eg. aadhar_no can be a unique key in the above table as it is unique for all the citizens of the country. Unique key can also accept one null value like here if a person doesn't have an Aadhar card.

Ujjwal Abhishek
Ujjwal Abhishek
Ujjwal is final-year CSE Undergraduate, a competitive coder, and a web developer passionate about problem-solving and data structures and algorithms.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet