Homeglossary

Foreign Key

A foreign key is a database field that links one table's records to matching records in another table. It usually stores a value that identifies a row through a primary key, helping a relational database keep connected data accurate. Foreign keys can also define what happens to related records when data is updated or deleted.

Build connected, reliable apps with Emergent.

What Is a Foreign Key in a Database?

A foreign key is a database field that links records in one table to matching records in another table. In a relational database, it is a column or group of columns in a child table that refers to a primary key or unique value in a parent table.

It defines a relationship. When enforced as a foreign key constraint, it prevents invalid references, such as an order pointing to a customer that does not exist.

How Foreign Key Constraints Work

Consider customers and orders tables. The orders.customer_id field can reference customers.id, preserving referential integrity so related data stays valid.

  1. Create a customer row with a unique ID, such as 42.
  2. Create an order row and store 42 in its customer_id column.
  3. On an insert or update, the database checks that customer ID 42 exists.
  4. If someone changes or deletes the customer, the database applies the configured rule.

Primary Key vs. Foreign Key

PK means primary key, and FK means foreign key. They work together but have different jobs.

FeaturePrimary keyForeign key
PurposeIdentifies one row in its own tableReferences a related row in another table
UniquenessMust be uniqueCan repeat across many rows
Null valuesNot allowedMay be allowed for an optional relationship
Relationship roleParent record identifierChild record connection

Benefits of Foreign Keys

Foreign keys enforce relationship checks across all application paths.

  • They prevent orphan records, such as an order assigned to a missing customer.
  • They make a database schema easier to understand.
  • They support safer joins, reports, and data cleanup.
  • They make update and deletion rules explicit instead of leaving them to application code.

Practical Limits and Trade-Offs

Constraints improve consistency but require deliberate design.

  • Checks add work during inserts, updates, and deletes.
  • Cascading deletes can remove related data unexpectedly if chosen carelessly.
  • Bulk imports often need parent rows loaded before child rows.
  • Separate services or databases may use application-level checks, which are not equivalent to an enforced database constraint.

Foreign Key Design Best Practices

Design relationships around real data ownership and lifecycle rules.

  • Use compatible data types for referenced and referencing columns.
  • Index foreign key columns used often in joins and lookups.
  • Choose actions such as RESTRICT, CASCADE, or SET NULL intentionally.
  • Allow NULL only when the relationship is truly optional.
  • Test deletion paths and their effect on related records.
  • Use composite foreign keys when a relationship needs more than one field.

Foreign Keys When Building Applications with Emergent

When building a full-stack product, define entities, required and optional relationships, and deletion behavior from the start. Good app design and development turns these choices into dependable workflows, while an AI app builder can help translate a well-defined data model into an application. Build connected, reliable apps with Emergent.

Start Building
on Emergent today
Start Building