Microsoft SQL Server Add foreign key on existing table


Example

FOREIGN KEY columns with constraint can be added on existing tables that are still not in relationship. Imagine that we have Company and Employee tables where Employee table don't have CompanyId column. ALTER TABLE statement enables you to add new column with foreign key constraint that references some other table and primary key in that table:

alter table Employee
    add CompanyId int foreign key references Company(CompanyId)