Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing
Back
  1. Home
  2. Interview prep
  3. What is a foreign key?

Data modeling · Keys & Relationships

What is a foreign key?

Easymodel-14
foreign keyrelationshipsreferential integrity

Question

What is a foreign key?

Solution

A foreign key (FK) is a column (or set of columns) in one table that references the primary key (or unique key) of another table. It expresses a relationship.

orders.customer_id  -->  customers.id

fct_orders.customer_sk  -->  dim_customer.customer_sk

Mental model

The fact row says "this sale belongs to customer version 55." The FK is the pointer; the dimension holds the descriptive attributes.

In OLTP

Databases often enforce FKs: you cannot insert an order for a missing customer.

In warehouses

Enforcement is often soft (tests, not hard DB rejects). You still design as if FKs exist: every fact key should resolve to a dimension row (or a known unknown member).

Orphan keys

Fact rows whose dimension key does not exist break joins and undercount. Pipelines use default "-1 unknown" members or quarantine.

Interview tip

> "Foreign keys link child rows to parent rows. In stars, facts hold FKs to dimension surrogate keys."

PreviousNext