Reputation: 419
I wish to create a constraint that state as below
Code.CodeTable ( CodeID smallint, CategoryID smallint,....) --> Parent Table
Admin.Document( DocumentTypeID smallint,.....) --> Child Table
The FK will be Admin.Document.DocumentTypeID map with Code.CodeTable.CodeID
I wish to have the constraint that only check Code.CodeTable.CodeID
which the Code.CodeTable.CategoryID = 15
only.
Upvotes: 0
Views: 2475
Reputation: 754408
As OMG Ponies already said - you cannot create fk constraints across databases, but if those are just odd table names with dots in them (highly discouraged! since SQL Server already uses a dotted schema: (database).(schema).(object name) and thus having dots in your table names is just asking for trouble at some point....), then you should be able to create your constraint like this:
ALTER TABLE [Admin.Document]
ADD CONSTRAINT FK_AdminDocument_CodeTableCodeID
FOREIGN KEY(DocumentTypeID) REFERENCES [Code.CodeTable](CodeID)
Since you have dots in your table names, you need to enclose those names in square brackets [].
Basically, you need to modify the child table and tell SQL Server which column in that child table refers to what parent table and column in the parent table.
Upvotes: 4