VOOZH about

URL: https://www.analyticsvidhya.com/blog/2020/07/difference-between-sql-keys-primary-key-super-key-candidate-key-foreign-key/

⇱ Different keys in SQL, Primary Key, Candidate Key, Foreign Key


India's Most Futuristic AI Conference Is Back – Bigger, Sharper, Bolder

  • d
  • :
  • h
  • :
  • m
  • :
  • s

Reading list

Different Keys in SQL (Primary Key, Candidate Key, Foreign Key)

Aniruddha Bhandari Last Updated : 02 May, 2025
9 min read

In database management systems, keys are crucial in maintaining data integrity and facilitating efficient data retrieval. SQL supports various types of keys, including primary, foreign, unique, candidate, and composite keys. Each type of key has unique characteristics and functions, making it essential to understand their differences. In this article, we will explore the different types of keys in SQL, their purpose, and how they relate to the overall data model. By the end of this article, you will have a comprehensive understanding of the different keys in SQL and how to use them effectively in your database applications.

Overview:

  • Learn about what is candidate key in SQL.
  • Gain an understanding of what is super key in SQL.
  • Discover other different types of keys in SQL.

What are the Keys in DBMS?

Databases store massive amounts of information across multiple tables. Each table might have thousands of rows. Needless to say, there will be many duplicate rows with redundant information. How do we deal with that? How do we manage records so that we store only unique data? And how do we relate the multiple tables in the database?

SQL keys are the answer to all these queries.

An SQL key, in Database Management Systems, is either a single attribute (or column) or a set of attributes that can uniquely identify rows (or tuples) in a table.

SQL keys ensure that there are no rows with duplicate values. They also help establish a relationship between multiple tables in the database. Therefore, it becomes imperative to learn about the different keys in SQL.

What are Super Keys in SQL?

Super key is a single key or a group of multiple keys that can uniquely identify tuples in a table.

Super Key can contain multiple attributes that might not be able to identify tuples in a table independently, but when grouped with certain keys, they can uniquely identify tuples.

Let me take an example to clarify the above statement. Have a look at the following table with the schema employees(Id, Name, Gender, City, Email, Dep_Id)

Consider that the ID attribute here corresponds to the employee ID. It is unique to every employee at the table. In that case, we can say that the Id attribute can uniquely identify the tuples of this table. So, ID is a super key to this table. Note that we can have other Super Keys in this table, too.

For instance(Id, Name), (Id, Email), (Id, Name, Email), etc. can all be Super keys as they can all uniquely identify the table’s tuples. This is so because of the presence of the Id attribute, which can uniquely identify the tuples. The other attributes in the keys are unnecessary. Nevertheless, they can still identify tuples.

What are Candidate Keys in SQL?

The candidate key is a single key or a group of multiple keys uniquely identifying table rows.

A candidate key is a column or a combination of columns uniquely identifying each row in a table. It is used to ensure that there are no duplicate or ambiguous records in the table.

A Candidate key is a subset of Super keys devoid of unnecessary attributes that are unimportant for uniquely identifying tuples. For this reason, you can also call the Candidate key a minimal Super key.

The value for the Candidate key is unique and non-null for all tuples. It encapsulates two important constraints – the unique key and not null constraints. This ensures that values in the Candidate key do not contain duplicate values. And every table has to have at least one Candidate key. But there can be more than one Candidate Key too.

For example, in the earlier example, both ID and Email can act as Candidates for the table as they contain unique and non-null values.

πŸ‘ SQL Candidate key

On the other hand, we cannot use attributes like City or Gender to retrieve tuples from the table, as they do not have unique values.

πŸ‘ Candidate Key DBMS

On the other hand, querying the table on the ID attribute will help us retrieve unique tuples.

πŸ‘ Candidate Key

What are the Primary Keys in SQL?

Primary key is the Candidate key selected by the database administrator to uniquely identify tuples in a table.

Out of all the Candidate keys that can be possible for a table, only one key will be used to retrieve unique tuples from the table. This Candidate key is called the Primary Key.

There can be only one Primary key for a table. Depending on how the Candidate Key is constructed, the primary key can be a single attribute or a group of attributes. However, the important point is that the primary key should be a unique and non-null attribute(s).

There can be two ways to create a Primary key for the table. The first way is to alter an already created attribute by adding the primary key constraint. This is shown below:

πŸ‘ SQL Primary Key

Here, I have chosen the ID as the primary key attribute.

If I try to add a new row with a duplicate ID value, it gives me an error message.

πŸ‘ SQL Insert

The second way of adding a Primary key is during the creation of the table itself. All you have to do is add the Primary Key constraint at the end after defining all the attributes in the table.

πŸ‘ Primary key

To define a Primary Key constraint on multiple attributes, you can list all the attributes in the parenthesis, as shown below.

πŸ‘ Primary Key DBMS

But remember that these attributes should be defined as non-null values; otherwise, the whole purpose of using the Primary key to identify tuples uniquely gets defeated.

Note: Knowing the difference between the Candidate and Primary keys is extremely important as it is a popular interview question!

What are Alternate Keys or Secondary Keys in SQL?

Alternate keys are those candidate keys which are not the Primary key.

There can only be one Primary key for a table. Therefore, all the remaining Candidate keys are known as Alternate or Secondary keys. They can also uniquely identify tuples in a table, but the database administrator chose a different Primary key.

If we look at the Employee table once again, since I have chosen Id as the Primary key, the other Candidate Key (Email) becomes the Alternate key for the table.

πŸ‘ Alternate Key

What are Foreign Keys in SQL?

Foreign key is an attribute which is a Primary key in its parent table, but is included as an attribute in another host table.

A foreign key generates a relationship between the parent and host tables. For example, in addition to the Employee table containing the employees’ personal details, we might have another table, Department, containing information related to the employee’s department with the following schema: Department(Id, Name, Location).

πŸ‘ What Are Foreign Keys in SQL?

Primary Key

The Primary key in this table is the Department ID. We can add this attribute to the Employee by making it the foreign key in the table. We can either do this when creating the table or alter the table later to add the foreign key constraint. Here, I have altered the table, but creating a foreign key during table creation is similar to that for the Primary Key.

πŸ‘ Alter Foreing key

Here, Dep_Id is now the foreign key in the Employee table while it is the Primary Key in the Department table.

Foreign Key

The foreign key allows you to create a relationship between two tables in the database, thereby ensuring normalization in relational databases. Each table describes data related to a particular field (employee and department here). Using the foreign key, we can easily retrieve data from both tables.


Note: To operate on foreign keys, you need to know about Joins, which this article explains in detail.

Using foreign keys makes it easier to update the database when required. This is because we only have to make changes in limited rows. For example, suppose the Marketing department shifts from Kolkata to Pune, instead of updating all the relevant rows in the Employee table. In that case, we can simply update the location in the Department table. This ensures that there are only a few places to update and less risk of having different data in different places.

The foreign key concept is fundamental to understanding the referential integrity constraint in RDBMS or relational databases. Referential integrity ensures that any column declared as a foreign key in a table can contain only null values or the values present in the base table’s primary key. For example, the Dep_Id foreign key in the Employees table can contain only the department IDs present in the Department table or the null value. Anything beyond that would break the referential integrity in a relational model.

What are Composite Keys in SQL?

A Composite key is a Candidate key or Primary key that consists of more than one attribute.

Sometimes, no single attribute may have the property to identify tuples in a table uniquely. In such cases, we can use a group of attributes to guarantee uniqueness. Combining these attributes will uniquely identify tuples in the table.

Consider the following table:

Here, neither of the attributes contains unique values to identify the tuples. Therefore, we can combine two or more attributes to create a key that uniquely identifies the tuples. For example, we can group Transaction_Id and Product_Id to create a key uniquely identifying the tuples. These are called composite keys.

Different Types of Keys in SQL

Key TypeCharacteristicsPurpose
Super keyCan contain redundant attributesUniquely identifies tuples in a table
Candidate keyContains only necessary attributesIdentifies tuples uniquely, subset of super key
Primary keyUnique and non-nullUniquely identifies tuples in the table
Alternate key–Not chosen as primary key
Composite keyMore than one attributeIdentifies tuples uniquely
Foreign keyLinked to primary key in another tableMaintains referential integrity, establishes relationships

Conclusion

In this article, we understood the importance of different types of keys in SQL. We covered the most common and widely used SQL keys that any professional looking to work with databases should know about. We also saw how to implement each of those keys in SQL and understood the differences between each type of key.

In this comprehensive article, we explored the crucial role of keys in databases, shedding light on their significance for any professional venturing into database management. We covered the most common and widely used SQL keys, including the candidate key, which is paramount for ensuring data integrity. Additionally, we delved into the practical implementation of each key in SQL, providing a nuanced understanding of the differences between these key types. Ready to bolster your expertise in database management? Dive into it Now!

Key Takeaways

  • Looked at the different types of keys in RDBMS – Super key, Candidate key, Primary key, Secondary key, Composite key, Foreign key
  • The Candidate and Primary keys employ the not null and unique key constraints.
  • A Foreign key ensures the referential constraint in SQL

If you want to work with SQL in Python, I suggest going through this article. If you are looking for some SQL techniques for better data analysis, you shouldn’t miss this great article.

Frequently Asked Questions

Q1. What are the six different types of keys in SQL?

A. There are six types of keys: super key, Candidate key, Primary key, Composite key, Alternate key, and foreign key.

Q2. What are the differences between the Primary and Candidate keys in SQL?

A. A candidate key is a single key or a group of multiple keys uniquely identifying table rows. It is a subset of the Super key. The primary key, on the other hand, is the Candidate key chosen to identify rows in a database table uniquely.

Q3. What is a foreign key in SQL?

A. Foreign key determines the relationship between two tables in a relational database. It is an attribute that is a Primary key in its parent table but is included as an attribute in another host table.

Q4. What are the different types of keys in a database?

A. Depending on the application’s specific needs, a database may contain various types of keys, including primary keys, foreign keys, unique keys, candidate keys, composite keys, surrogate keys, and more.

Q5. What are the different keys in MySQL?

A. MySQL supports various types of keys, including primary, foreign, unique, and indexes, to ensure data integrity and efficient data retrieval.

Q6. What is a Candidate Key?

A. A candidate key in a relational database is a set of one or more attributes (columns) that can uniquely identify each row or record in a table. It must satisfy two conditions: uniqueness (no two rows have the same values for the candidate key) and irreducibility (no proper subset of the candidate key maintains uniqueness). Candidate keys are essential for defining the table’s primary key, which serves as a unique record identifier.

I am on a journey to becoming a data scientist. I love to unravel trends in data, visualize it and predict the future with ML algorithms! But the most satisfying part of this journey is sharing my learnings, from the challenges that I face, with the community to make the world a better place!

Login to continue reading and enjoy expert-curated content.

Free Courses

Generative AI - A Way of Life

Explore Generative AI for beginners: create text and images, use top AI tools, learn practical skills, and ethics.

Getting Started with Large Language Models

Master Large Language Models (LLMs) with this course, offering clear guidance in NLP and model training made simple.

Building LLM Applications using Prompt Engineering

This free course guides you on building LLM apps, mastering prompt engineering, and developing chatbots with enterprise data.

Improving Real World RAG Systems: Key Challenges & Practical Solutions

Explore practical solutions, advanced retrieval strategies, and agentic RAG systems to improve context, relevance, and accuracy in AI-driven applications.

Microsoft Excel: Formulas & Functions

Master MS Excel for data analysis with key formulas, functions, and LookUp tools in this comprehensive course.

Responses From Readers

Michael Allan

Great Effort .. You have described it clearly and easiest way.. [keep it up}

Well explained and helpful. Thank you.

Stephanie

Very detailed explanation! Thanks for sharing!!

Flagship Programs

GenAI Pinnacle Program| GenAI Pinnacle Plus Program| AI/ML BlackBelt Program| Agentic AI Pioneer Program

Free Courses

Generative AI| DeepSeek| OpenAI Agent SDK| LLM Applications using Prompt Engineering| DeepSeek from Scratch| Stability.AI| SSM & MAMBA| RAG Systems using LlamaIndex| Building LLMs for Code| Python| Microsoft Excel| Machine Learning| Deep Learning| Mastering Multimodal RAG| Introduction to Transformer Model| Bagging & Boosting| Loan Prediction| Time Series Forecasting| Tableau| Business Analytics| Vibe Coding in Windsurf| Model Deployment using FastAPI| Building Data Analyst AI Agent| Getting started with OpenAI o3-mini| Introduction to Transformers and Attention Mechanisms

Popular Categories

AI Agents| Generative AI| Prompt Engineering| Generative AI Application| News| Technical Guides| AI Tools| Interview Preparation| Research Papers| Success Stories| Quiz| Use Cases| Listicles

Generative AI Tools and Techniques

GANs| VAEs| Transformers| StyleGAN| Pix2Pix| Autoencoders| GPT| BERT| Word2Vec| LSTM| Attention Mechanisms| Diffusion Models| LLMs| SLMs| Encoder Decoder Models| Prompt Engineering| LangChain| LlamaIndex| RAG| Fine-tuning| LangChain AI Agent| Multimodal Models| RNNs| DCGAN| ProGAN| Text-to-Image Models| DDPM| Document Question Answering| Imagen| T5 (Text-to-Text Transfer Transformer)| Seq2seq Models| WaveNet| Attention Is All You Need (Transformer Architecture) | WindSurf| Cursor

Popular GenAI Models

Llama 4| Llama 3.1| GPT 4.5| GPT 4.1| GPT 4o| o3-mini| Sora| DeepSeek R1| DeepSeek V3| Janus Pro| Veo 2| Gemini 2.5 Pro| Gemini 2.0| Gemma 3| Claude Sonnet 3.7| Claude 3.5 Sonnet| Phi 4| Phi 3.5| Mistral Small 3.1| Mistral NeMo| Mistral-7b| Bedrock| Vertex AI| Qwen QwQ 32B| Qwen 2| Qwen 2.5 VL| Qwen Chat| Grok 3

AI Development Frameworks

n8n| LangChain| Agent SDK| A2A by Google| SmolAgents| LangGraph| CrewAI| Agno| LangFlow| AutoGen| LlamaIndex| Swarm| AutoGPT

Data Science Tools and Techniques

Python| R| SQL| Jupyter Notebooks| TensorFlow| Scikit-learn| PyTorch| Tableau| Apache Spark| Matplotlib| Seaborn| Pandas| Hadoop| Docker| Git| Keras| Apache Kafka| AWS| NLP| Random Forest| Computer Vision| Data Visualization| Data Exploration| Big Data| Common Machine Learning Algorithms| Machine Learning| Google Data Science Agent
πŸ‘ Av Logo White

Continue your learning for FREE

Forgot your password?
πŸ‘ Av Logo White

Enter OTP sent to

Edit

Wrong OTP.

Enter the OTP

Resend OTP

Resend OTP in 45s

πŸ‘ Popup Banner
πŸ‘ AI Popup Banner