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.
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.