The Wayback Machine - https://web.archive.org/web/20250114153644/https://www.geeksforgeeks.org/difference-between-primary-key-and-foreign-key/
Open In App

Difference between Primary Key and Foreign Key

Last Updated : 27 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Keys are one of the most important elements in a relational database to maintain the relationship between the tables and it also helps in uniquely identifying the data from a table. The primary key is a key that helps uniquely identify the tuple of the database. In contrast, the Foreign Key is a key used to determine the relationship between the tables through the primary key of one table that is the primary key of one table acts as a foreign key to another table. Now, let’s discuss both of them in some detail.

What is Primary Key?

A primary key is used to ensure that data in the specific column is unique. A column cannot have NULL values. It is either an existing table column or a column that is specifically generated by the database according to a defined sequence.

Example: STUD_NO, as well as STUD_PHONE, are candidate keys for relation STUDENT but STUD_NO can be chosen as the primary key (only one out of many candidate keys).

Table STUDENT:

STUD_NO STUD_NAME STUD_PHONE STUD_STATE STUD_COUNT STUD_AGE
1 RAM 9865278251 Haryana India 20
2 RAM 9655470231 Punjab India 19
3 SUJIT 7514290359 Rajasthan India 18
4 SURESH 8564103258 Punjab India 21

Table STUDENT_COURSE:

STUD_NO COURSE_NO COURSE_NAME
1 C1 DBMS
2 C2 Computer Networks
1 C2 Computer Networks

What is Foreign Key?

A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables. It is a column (or columns) that references a column (most often the primary key) of another table.

Example: STUD_NO in STUDENT_COURSE is a foreign key to STUD_NO in STUDENT relation.

Difference Between Primary Key and Foreign Key

PRIMARY KEY FOREIGN KEY
A primary key is used to ensure data in the specific column is unique. A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables.
It uniquely identifies a record in the relational database table. It refers to the field in a table which is the primary key of another table.
Only one primary key is allowed in a table. Whereas more than one foreign key is allowed in a table.
It is a combination of UNIQUE and Not Null constraints. It can contain duplicate values and a table in a relational 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 tables. It constraint cannot be defined on the local or global temporary tables.

Conclusion

In this article, we have mentioned the primary key and foreign key, and the differences between them. Both the keys, whether the primary key or the foreign key, play an important role in the Database management system. Primary Key contains unique values, whereas Foreign Key contains values taking reference from Primary Keys. The main characteristic property of the Primary key is that it can’t be repeated, it is unique. There are some differences between their functions, Primary Keys determine a row in the table and Foreign Key determines the relation between tables.

Difference Between Primary Key and Foreign Key -FAQs

How many types of keys are there in the Relational Database Model?

  • Candidate Key
  • Primary Key
  • Super Key
  • Alternate Key
  • Foreign Key
  • Composite Key

What is the main purpose of a Primary Key?

This Key ensures that each record must be unique in a table or we can say to uniquely identify each record in a table we make a column Unique using the primary key.

What happens when a foreign key points to a record which did not exist?

Referential integrity issues will occur, depending on the setup of the database. Typically, databases enforce foreign key constraints to prevent this from happening.

Can a foreign key reference a column other than the Primary Key?

Yes, it can reference any column but that column must have unique values and is not null (candidate key).

Can a Primary Key be NULL?

No, a primary key cannot contain NULL values.


Our GATE 2026 Courses for CSE & DA offer live and recorded lectures from GATE experts, Quizzes, Subject-Wise Mock Tests, PYQs and practice questions, and Full-Length Mock Tests to ensure you’re well-prepared for the toughest questions.

Take the Three 90 Challenge! Complete 90% of the course in 90 days and earn a 90% refund. Stay motivated, track your progress, and make the most of your preparation time. Plus, enjoy exclusive features like:

--> All India Mock Test
--> Live GATE CSE & DA Mentorship Classes
--> Live Doubt Solving Sessions

Join now and stay ahead in your GATE 2026 journey!


Next Article

Similar Reads

three90RightbarBannerImg