A primary key is used to ensure data in the specific column is unique. It is 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. 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.
Difference between Primary Key and Foreign Key are stated below
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 are 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. |