Primary Key vs. Foreign Key Comparison Chart
The following comparison chart explains their main differences in a quick manner:
Comparison Basis | Primary Key | Foreign Key |
---|---|---|
Basic | It is used to identify each record into the database table uniquely. | It is used to links two tables together. It means the foreign key in one table refers to the primary key of another table. |
NULL | The primary key column value can never be NULL. | The foreign key column can accept a NULL value. |
Count | A table can have only one primary key. | A table can have more than one foreign key. |
Duplication | The primary key is a unique attribute; therefore, it cannot stores duplicate values in relation. | We can store duplicate values in the foreign key column. |
Indexing | The primary key is a clustered index by default, which means it is indexed automatically. | A foreign key is not a clustered index by default. We can make clustered indexes manually. |
Deletion | The primary key value can’t be removed from the table. If you want to delete it, then make sure the referencing foreign key does not contain its value. | The foreign key value can be removed from the table without bothering that it refers to the primary key of another table. |
Insertion | We can insert the values into the primary key column without any limitation, either it present in a foreign key or not. | The value that is not present in the column of a primary key cannot be inserted into the referencing foreign key. |
Temporary table | The primary key constraint can be defined on the temporary tables. | A foreign key constraint cannot be defined on the temporary tables. |
Relationship | It cannot create a parent-child relationship in a table. | It can make a parent-child relationship in a table. |