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
FK将是Admin.Document。与Code.CodeTable.CodeID DocumentTypeID地图
I wish to have the constraint that only check Code.CodeTable.CodeID
which the Code.CodeTable.CategoryID = 15
only.
我希望具有仅检查Code.CodeTable的约束。CodeID Code.CodeTable。CategoryID = 15只。
1 个解决方案
#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:
正如OMG小马已经说过的——您不能跨数据库创建fk约束,但是如果这些只是带有点的奇怪表名(非常不鼓励!因为SQL Server已经使用了点模式:(数据库)。(对象名称),因此在你的表名点只是自找麻烦点....),那么你应该能够创建约束如下:
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.
基本上,您需要修改子表并告诉SQL Server该子表中的哪一列指向父表中的哪一列。
#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:
正如OMG小马已经说过的——您不能跨数据库创建fk约束,但是如果这些只是带有点的奇怪表名(非常不鼓励!因为SQL Server已经使用了点模式:(数据库)。(对象名称),因此在你的表名点只是自找麻烦点....),那么你应该能够创建约束如下:
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.
基本上,您需要修改子表并告诉SQL Server该子表中的哪一列指向父表中的哪一列。