A foreign key is a column or combination of columns in one table that references a key in another table. It helps establish relationships and enforce referential integrity.
Foreign keys allow related business information to be stored in separate tables while maintaining a valid relationship between those records.
Orders can contain CustomerID as a foreign key referencing CustomerID in the Customers table.
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
FOREIGN KEY (CustomerID)
REFERENCES Customers(CustomerID)
);
Foreign keys are widely used in banking, e-commerce, HR, healthcare, finance, and other relational systems.
- A foreign key creates a relationship between tables.
- It references a key in another table.
- It supports referential integrity.
- It is commonly used with primary keys.
- Foreign keys are important for JOIN operations.
What is a foreign key?
A foreign key is a column or combination of columns that references a key in another table.
Why are foreign keys important?
They help maintain valid relationships between tables and support referential integrity.