Foreign key

From Wikipedia, the free encyclopedia

A foreign key (FK) is a field or group of fields in a database record that points to a key field or group of fields forming a key of another database record in some (usually different) table. Usually a foreign key in one table refers to the primary key (PK) of another table. This way references can be made to link information together and it is an essential part of database normalization. Foreign keys that refer back to the same table are called recursive foreign keys.

For example, suppose an accounts database has a table of invoices, and each invoice is associated with a particular supplier. Supplier details (such as address or phone number) are kept in a separate table; each supplier is given a 'supplier number' to identify them. Each invoice record has a field containing the supplier number for that invoice. Then, the 'supplier number' is the primary key in the Supplier table, and a foreign key in the Invoices table.

A record may contain more than one foreign key: in our example an invoice may also be associated with a particular department, project etc. Foreign key fields would then link the invoice record to records in Department or Project tables.

The use of a foreign key often assumes its existence as a primary key somewhere else. Improper foreign key/primary key relationships are the source of many database problems (see referential integrity).

A foreign key constraint is a constraint that data which serves as a foreign key in one database record cannot be removed as there is still data in another record that assumes its existence.

[edit] See also


Topics in database management systems (DBMS)view  talk  edit )

Concepts
Database | Database model | Relational database | Relational model | Relational algebra | Primary key - Foreign key - Surrogate key - Superkey
Database normalization | Referential integrity | Relational DBMS | Distributed DBMS | ACID

Objects
Trigger | View | Table | Cursor | Log | Transaction | Index | Stored procedure | Partition

Topics in SQL
Select | Insert | Update | Merge | Delete | Join | Union | Create | Drop
Comparison of syntax

Implementations of database management systems

Types of implementations
Relational | Flat file | Deductive | Dimensional | Hierarchical | Object oriented | Temporal

Products
Caché | db4o | dBASE | Firebird | Helix database | DB2 | Informix | Ingres | InterBase | Microsoft SQL Server | MySQL | OpenLink Virtuoso | Oracle | PostgreSQL | SQLite | Sybase IQ | Sybase | Teradata | Visual FoxPro | Comparison - relational | Comparison - object-relational

Components
Query language | Query optimizer | Query plan | ODBC | JDBC
Lists
List of object-oriented database management systems
List of relational database management systems