我们如何在不同的DB表-azure sql上创建FK-KEY列 [英] How can we create FK-KEY column on different DB tables-azure sql
问题描述
我们有两个数据库Azure sql。 A1数据库中的country(Countryid,CountryName)表和B1数据库中的语言(Id,LanguageName,CountryId)
表。我们如何在语言表中添加外键(CountryId)。
FOREIGN KEY约束只能引用同一服务器上同一数据库中的表。必须通过触发器实现跨数据库参照完整性
。有关详细信息,请参阅 CREATE TRIGGER(Transact-SQL) 。
使用
创建触发器dbo.MyTableTrigger ON dbo.MyTable,插入后,更新
作为
开始
如果不存在(从OtherDB.dbo.TableName中选择PK,其中PK在(从插入中选择FK)BEGIN
- 处理参考错误这里
END
END< span style ="font-family:'Segoe UI',SegoeUI,'Segoe WP','Helvetica Neue',Helvetica,Tahoma,Arial,sans-serif;字体大小:16px的"> 跨度>
We have two data base in Azure sql. country(Countryid, CountryName) table in A1 data base and language(Id, LanguageName,CountryId) table in B1 data base. How can we add foreign key(CountryId) in language table.
FOREIGN KEY constraints can reference only tables within the same database on the same server. Cross-database referential integrity must be implemented through triggers. For more information, see CREATE TRIGGER (Transact-SQL).
Use something like
Create Trigger dbo.MyTableTrigger ON dbo.MyTable, After Insert, Update As Begin If NOT Exists(select PK from OtherDB.dbo.TableName where PK in (Select FK from inserted) BEGIN -- Handle the Referential Error Here END END
这篇关于我们如何在不同的DB表-azure sql上创建FK-KEY列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!