What is FOREIGN KEY ?
What is FOREIGN KEY ?
A foreign key is a column or a set of columns in a database table that establishes a link between data in two tables. It is used to enforce referential integrity within the database. Specifically, a foreign key in one table points to a primary key in another table, creating a parent-child relationship between the tables.
Consider two tables, Customers
and Orders
. The Customers
table has a primary key CustomerID
, and the Orders
table has a foreign key CustomerID
that references the CustomerID
in the Customers
table.
CREATE TABLE Customers (
CustomerID int PRIMARY KEY,
Name varchar(100),
Age int
);
CREATE TABLE Orders (
OrderID int PRIMARY KEY,
OrderDate date,
CustomerID int,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
In this example, the CustomerID
column in the Orders
table is a foreign key that references the CustomerID
column in the `...
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào