Msg 1770, Level 16, State 0, Line 1
Foreign key ‘%.*ls’ references invalid column ‘%.*ls’ in referenced table ‘%.*ls’.
This error message appears when you try to create a FOREIGN KEY constraint on a column that does not exist in the referenced table.
The T-SQL statement can be parsed, but causes the error at runtime.
Error of the Severity Level 16 are generated by the user and can be fixed by the SQL Server user. The statement cannot be executed this way. FOREIGN KEY constraints can only reference columns that do exist at the time when the constraint is created.
All versions of SQL Server.
IF OBJECT_ID(‘tempdb..t’) > 0
DROP TABLE t
IF OBJECT_ID(‘tempdb..t1’) > 0
DROP TABLE t1
CREATE TABLE t
c1 INT PRIMARY KEY
CREATE TABLE t1
CONSTRAINT FK_t_t1_c1 FOREIGN KEY REFERENCES t(c)
In the above example we try to create a FOREING KEY constraint for a table t1 with the table t. Because the column c1 does not exist in the table t, the error is raised.