Difference Between Primary Key and Foreign Key | Testbook.com

Last Updated on Jul 31, 2023
Download As PDF
IMPORTANT LINKS

Primary Key Vs. Foreign Key: Unravel the distinctions between Primary Key and Foreign Key

A primary key guarantees uniqueness in a specific column, whereas a foreign key establishes a connection between two tables. Discover the key differences between a primary key and a foreign key.


Defining Primary Key

A primary key is primarily concerned with ensuring the uniqueness of a table. It is either a single column or a combination of columns that uniquely identify each row in the database. This implies that it should not contain any duplicate or NULL values.

Defining Foreign Key

A foreign key is typically used to establish a relationship between two tables. Its main function is to maintain data integrity between two different instances of an entity.

Test Series
403.4k Students
SBI PO Mock Test Series 2025 (Pre + Mains)
367 TOTAL TESTS | 13 Free Tests
  • 13 Full Test (Mains)
  • 70 GA/CA Pack
  • 12 Sectional Test (Mains)
  • 89 Chapter Test
  • 2 2025 Trend Based FTs
  • 28 Full Test (Prelims)
  • 45 Most Saved Qs
  • 15 Sectional Test (Prelims)
  • 24 Memory Based Paper
  • 46 Memory Based Sectional Test
  • 23 IBPS Recent Exam MBTs

Get Started

Contrasting Primary Key and Foreign Key
S.No. Primary Key Foreign Key
1 A primary key is primarily concerned with ensuring the uniqueness of a table. It guarantees the value in the specific column is unique. A foreign key is typically used to establish a relationship between two tables.
2 Only one primary key is allowed per table. A table can accommodate multiple foreign keys.
3 Primary keys do not permit null values. Foreign keys can accept multiple null values.
4 A primary key can uniquely identify a record in a database table. A foreign key is a field in one table that serves as the primary key in another table.
5 In the case of a primary key, the value cannot be removed from the parent table. In the case of a foreign key, the value can be deleted from the child table.
6 Restrictions of a primary key can be fully defined on temporary tables. Restrictions of a foreign key cannot be defined on global or local temporary tables.

Continue learning and stay updated with the latest information on GATE Exam , including GATE Eligibility Criteria , GATE 2024 , GATE Admit Card , GATE Application Form , GATE Syllabus , GATE Cutoff , GATE Previous Year Question Paper , and more.

More Articles for Key Differences

Frequently Asked Questions

A primary key is a column or a set of columns that uniquely distinguishes every row in the database. It is unique and doesn't contain any NULL value.

A foreign key is used to build a relationship between the two tables. The major purpose of the foreign key is to sustain data integrity between two separate instances of an entity.

The main differences include: a primary key focuses on uniqueness and doesn't allow NULL values, while a foreign key builds relationships between tables and can accept multiple NULL values. Also, a table allows only one primary key but can allow more than one foreign key.

Report An Error