T-SQL 101: 135 What is a Foreign Key Constraint in SQL Server?

Foreign keys are mostly used where you store the primary key of another table, and want to check that it’s valid. This is used to ensure database integrity.
For example, I might have a CustomerID stored in an Orders table, and I want to make sure that the customer already exists. In the example above, I have a Courses table and I want to make sure that the CollegeID that’s stored for each course, actually matches a college that’s already in the Colleges table. I check that the CollegeID already exists by declaring a foreign key reference to that table.
Note also that I’m talking about keys not just columns. The keys might include more than one column.
Unique Key?
While it is possible to reference a unique key instead of a primary key, that’s not common. It’s usually always a primary key.
Learning T-SQL
It’s worth your while becoming proficient in SQL. If you’d like to learn a lot about T-SQL in a hurry, our Writing T-SQL Queries for SQL Server course is online, on-demand, and low cost.
2025-03-26