---
title: "Difference between Primary Kay, Foreign Key, and Composite Key?"  
description: "Difference between Primary Kay, Foreign Key, and Composite Key?"  
author: "Utpal Vishwas"  
published: 2023-08-27  
updated: 2023-08-28  
canonical: https://www.mindstick.com/forum/159679/difference-between-primary-kay-foreign-key-and-composite-key  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 3 minutes  

---

# Difference between Primary Kay, Foreign Key, and Composite Key?

[Difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between [Primary](https://www.mindstick.com/forum/33739/difference-between-primary-key-and-unique-key) Kay, [Foreign Key](https://www.mindstick.com/blog/174/foreign-key-self-reference-constraint), and [Composite Key](https://www.mindstick.com/articles/11986/sql-keys-in-rdbms)?

## Replies

### Reply by Aryan Kumar

Sure. Here are the differences between primary key, [foreign](https://yourviews.mindstick.com/story/2164/here-are-the-best-foreign-universities-for-higher-education) key, and composite key in SQL:

- **Primary key:** A primary key is a unique identifier for each row in a table. It cannot have any duplicate values, and it cannot be NULL. A primary key can be a single column or a combination of columns.
- **Foreign key:** A foreign key is a column or combination of columns in one table that references the primary key of another table. The foreign key must have the same values as the primary key in the other table. Foreign keys are used to establish relationships between tables.
- **Composite key:** A composite key is a primary key that consists of two or more columns. Composite keys are used when no single column is sufficient to uniquely identify a row in a table.

Here is a table summarizing the differences between primary key, foreign key, and composite key:

| Feature | Primary key | Foreign key | Composite key |
| --- | --- | --- | --- |
| Uniqueness | Yes | Yes | Yes |
| Nullability | No | No | Can be null |
| Number of columns | One or more | One | Two or more |
| Purpose | Uniquely identifies each row in a table | Establishes a relationship between tables | Uniquely identifies each row in a table |

Here are some additional details about the differences between primary key, foreign key, and composite key:

- A primary key can be used to uniquely identify a row in a table, even if other columns in the table have duplicate values. For example, a table of students might have a primary key of `student_id`, even if there are multiple students with the same name.
- A foreign key is used to establish a relationship between two tables. The foreign key in one table must reference the primary key in another table. For example, a table of orders might have a foreign key of `customer_id` that references the primary key in a table of customers.
- A composite key is a primary key that consists of two or more columns. Composite keys are used when no single column is sufficient to uniquely identify a row in a table. For example, a table of books might have a composite key of `ISBN` and `edition`.


---

Original Source: https://www.mindstick.com/forum/159679/difference-between-primary-kay-foreign-key-and-composite-key

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
