Microsoft SQL Server Adding foreign key relationship on existing table


Example

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

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