Primary VS Foreign Key

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.

Foreign Key :-

  1. Foreign key is a column or group of columns in a relational database table that provides an association between data in two tables.

  2. Foreign key accepts NULL value.

  3. Table can have multiple foreign keys.

  4. Tuples can have a duplicate value for a foreign key attribute.

Primary Key :-

  1. Primary key is a special relational database table column or a combination of multiple columns that allows to uniquely identify all table records.

  2. Primary key value cannot be NULL and its a Unique.

  3. Table can have only one primary key.

  4. Two tuples in a relation cannot have duplicate values for a primary key attribute.