We all know the way to create foreign key on column, and we are using UI to create it mostly instead of script. Did you used script to add foreign key?
Here I am sharing the script to add foreign key on new/existing table/column and consider RefColumn is a primary key column on RefTable table.
Creating foreign key constaint while creating a new table
CREATE TABLE ChildTable
(
Id int not null identity(1,1),
ChildColumn int
CONSTRAINT fk_RefId
FOREIGN KEY REFERENCES RefTable(RefColumn)
)
-- OR
CREATE TABLE ChildTable
(
Id int not null identity(1,1),
ChildColumn int ,
CONSTRAINT fk_RefId
FOREIGN KEY(ChildColumn) REFERENCES RefTable(RefColumn)
)
Adding a new column with foreign key constraint on existing table
ALTER TABLE ChildTable
ADD ChildColumn int
CONSTRAINT fk_RefId
FOREIGN KEY REFERENCES RefTable(RefColumn)
Adding a foreign key constraint on existing column
ALTER TABLE ChildTable
ADD CONSTRAINT fk_RefId
FOREIGN KEY(ChildColumn) REFERENCES RefTable(RefColumn)
Hope you liked these scripts.