Technology

Foreign Key

Foreign Key

A foreign key is an attribute or set of attributes in a relation whose values match a primary key in another relation. It is a column or a combination of columns whose values match a Primary Key in a different table. The relation in which foreign key is created is known as Dependent Table or Child Table. The relation to which the foreign key refers is known as Parent Table. The key connects to another relationship when a relationship is established between two relations. A relation may contain more than one foreign key. It is a column or group of columns in a relational database table that provides a link between data in two tables. The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table.

The function of the foreign key is to ensure referential integrity of the data. They are an essential part of relational database systems.

Advantages:

  • It has Referential Integrity, means, every row in a Child table that has a foreign key, there will be a matching row in the Parent table.
  • They are necessary for maintaining the integrity and consistency of your data in a corporate production environment.

Disadvantages:

  • Many database users encounter foreign key errors, often due to referential integrity problems.
  • A foreign key might point to data that no longer exists, or the foreign key’s data type doesn’t match the primary key data type, eroding referential integrity.