0

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.

2
  • 1
    You can't make a foreign key constraint across databases, or is that your naming convention--to put periods in the table name? If that is your table naming convention, I don't recommend it. Commented Sep 14, 2010 at 4:38
  • 1
    @OMGPonies: the OP is referring to a schema with the period and not another database. Commented Oct 4, 2013 at 18:16

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.