RSS Feed

FOREIGN KEY constraint / Referential constraint

FOREIGN KEY constraint

A foreign key in one table is the field that relates to the primary key in a second table. For example, whereas CustomerID is the primary key in the Customers table, it may be the foreign key in the Orders table. [Sams teach yourself Microsoft Office Access 2003 in 24 hours By Alison Balter]

FOREIGN KEY constraint is concerned with how data in one table relates to /affects data in another table.

The FOREIGN KEY constraint is used to avoid actions that destroy links between tables.

The FOREIGN KEY constraint prevents invalid data from being entered into the foreign key column, because it has to be one of the values contained in the table it is pointing to.

Example of FOREIGN KEY constraint

The following SQL query creates a FOREIGN KEY on the "Cust_ID" column when the "Orders" table is created:

CREATE TABLE Orders
(
   Order_ID int NOT NULL PRIMARY KEY,
   OrderNo int NOT NULL,
   Cust_ID int FOREIGN KEY REFERENCES Customers(Cust_ID)
)