Selected topic
Constraints
Prefer practical output? Use related tools below while reading.
A FOREIGN KEY constraint is a type of integrity constraint that ensures the relationship between two tables. It specifies which column(s) in a table (the child table) contains foreign keys that reference the primary key in another table (the parent table).
Customers and Orders.In this example:
CustomerID column in the Customers table is the PRIMARY KEY (PK).CustomerID column in the Orders table is the FOREIGN KEY (FK) that references the PRIMARY KEY in the Customers table.sql
CREATE TABLE Orders (
OrderID int PRIMARY KEY,
CustomerID int, -- REFERENCES Customers(CustomerID)
OrderDate date
);In this syntax:
Orders with columns OrderID, CustomerID, and OrderDate.CustomerID column is specified as the FOREIGN KEY that references the PRIMARY KEY in the Customers table.