Difference between Primary Key and Foreign Key
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 is 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. |
Difference between Primary Key and Foreign Key
Pre-Requisite: Relational Database Model
Keys are one of the most important elements in a relational database to maintain the relationship between the tables and it also helps in uniquely identifying the data from a table. The primary Key is a key that helps in uniquely identifying the tuple of the database whereas the Foreign Key is a key that is used to identify the relationship between the tables through the primary key of one table that is the primary key one table acts as a foreign key to another table. Now, let’s discuss both of them in some detail.