Skip to main content
PreparationZone Logo
PreparationZone
Back to Fundamentals

Fundamentals · Easy · Free

What is a foreign key? How does referential integrity work?

FresherFundamentalsDatabase Fundamentals
Model answer

Foreign keys

sql
CREATE TABLE orders (
  id INT PRIMARY KEY,
  customer_id INT NOT NULL,
  FOREIGN KEY (customer_id) REFERENCES customers(id)
);

Referential integrity blocks orphan orders when customer deleted—ON DELETE CASCADE/SET NULL policies matter.

Explain CASCADE carefully—can surprise in prod.