T-SQL 101: 103 What is a Foreign Key ?
A foreign key is a situation where we store the primary key of one table in another table. It’s very useful. For example, we might need to know which sales transactions were for which customers.
Again, keep in mind that like primary keys, there could be more than one column in a key. While a foreign key can reference a unique key, it’s almost always the primary key.
In the example shown above, the column OrderID is the primary key of the Orders table. It’s highlighted in red. The column CinemaID is the primary key of the Cinemas table, but it has been stored in the Orders table. It’s highlighted in green. We refer to that as a foreign key. Clearly in a system that handles orders for cinemas, we’d need to know which cinema each order is for.
2025-01-30