Difference between Primary key and Foreign key in Database


In a relational database, keys are the most important elements to maintain the relationship between two tables or to uniquely identify the data from a table. Primary key is used to identify data uniquely therefore two rows can't have the same primary key. It can't be null. On the other hand, foreign key is used to maintain relationship between two tables.

The primary key of a table acts as the foreign key in another table. The Foreign key in a table helps enforce Referential Integrity constraint. Read this tutorial to find out more about Primary and Foreign keys and how they are different from each other.

What is a Primary Key?

A primary key is a column (or set of columns) in a table that uniquely identifies each row in the table. It cannot contain null values and must be unique across all rows in the table. Only one primary key is allowed in a table.

A primary key is basically a combination of the 'UNIQUE' and 'Not Null' constraints. Thus, it cannot be a NULL value. Another important point to be noted about primary key is that its value cannot be deleted from the parent table.

What is a Foreign Key?

A foreign key is a column (or set of columns) in a table that refers to the primary key in another table. It is used to establish a link between the two tables and is used to enforce referential integrity in the database. Foreign key is basically the field/column in a table that is analogous to the primary key of other table.

Unlike a primary key, a table can have more than one foreign key. Also, the foreign key can contain duplicate and null values in a relational database. The value of a foreign key can be deleted from the child table.

Difference between Primary Key and Foreign Key in Database

The following table highlights all the important differences between primary key and foreign key −

Key

Primary Key

Foreign Key

Basic

It is used to uniquely identify data in the table.

It is used to maintain relationship between tables.

Null

It can't be NULL.

It can accept the NULL values.

Duplicate

Two or more rows can't have same primary key.

It can carry duplicate value for a foreign key attribute.

Index

Primary has clustered index.

By default, It is not clustered index.

Tables

Primary key constraint can be defined on temporary table.

It can't be defined on temporary tables.

Conclusion

Primary keys and foreign keys are important concepts in database design as they help to establish relationships between tables and help ensure data integrity. A primary key uniquely identifies a row in a table, while a foreign key is used to link two tables together by referencing the primary key of the related table. The most important difference that you should note here is that a primary key cannot have a NULL value, whereas a foreign key can accept NULL values.

Updated on: 12-Sep-2023

27K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements