QamoosTech
DatabasesData modelingBeginner

Foreign Key

PronunciationFAWR-uh-n KEE

Definition

A field or group of fields in one table that uniquely identifies a row of another table, used to link two tables together and enforce referential integrity.

Where you hear it

During database design, writing SQL constraints, or discussing table relationships.

Examples

  • The orders table includes a foreign key that references the users table.
  • A foreign key prevents the database from deleting a customer who still has active purchases.
  • The payments table uses a foreign key to point at the order it pays for.

Common mistake

Assuming a foreign key automatically creates an index for fast lookups, which is not true for all database systems and often needs to be created manually.

Don't confuse with

Foreign Key vs. Primary Key: A primary key uniquely identifies a record within its own table, whereas a foreign key is used to establish a link between data in two different tables.

Say it at work

  • In a meeting or chatI think we need to add a foreign key to the logs table so we can track which user triggered each event.
  • In an email or ticketPlease ensure that the foreign key constraint is properly defined in the migration file to maintain referential integrity between these two tables.