Beginner

516. Foreign Key and JOIN

Learn how foreign keys connect child records to parent records and support JOIN operations.

📘 Lesson 516 of 728 ⏱ 11 min 🎯 Beginner

A foreign key is a column or group of columns that references a key in another table. It commonly represents the relationship between a child table and a parent table.

Foreign keys provide a clear relationship between tables and help maintain referential integrity.

Orders.CustomerID references Customers.CustomerID.

SQL
SELECT
    o.OrderID,
    c.CustomerName
FROM Orders o
INNER JOIN Customers c
    ON o.CustomerID = c.CustomerID;

Common in customer-order, employee-department, product-category, and account-transaction relationships.

  • Foreign keys represent relationships.
  • They commonly reference primary keys.
  • They can be used in JOIN conditions.
  • Foreign keys can help prevent invalid references.
  • A foreign key can contain repeated values.
What is a foreign key?

A foreign key is a column or set of columns that references a key in another table.

Can a foreign key contain duplicate values?

Yes. Multiple child records can reference the same parent record.

Have you finished this lesson?

Introduction to SQL – All Lessons