Can I have a foreign key referencing a column in a view in SQL Server?

marc picture marc · Jan 15, 2009 · Viewed 62.9k times · Source

In SQL Server 2008 and given

TableA(A_ID, A_Data)
TableB(B_ID, B_Data)
ViewC(A_or_B_ID, A_or_B_Data)

is it possible to define TableZ(A_or_B_ID, Z_Data) such that Z.A_or_B_ID column is constrained to the values found in ViewC? Can this be done with a foreign key against the view?

Answer

Brian Fisher picture Brian Fisher · Jan 15, 2009

You can't reference a view in a foreign key.