且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

多个表的 SQL Server 外键

更新时间:2023-01-29 08:42:01

外键只能引用一个表,如 文档(强调我的):

A foreign key can only reference one table, as stated in the documentation (emphasis mine):

外键 (FK) 是使用的列或列组合在两个表中的数据之间建立和强制链接.

A foreign key (FK) is a column or combination of columns that is used to establish and enforce a link between the data in two tables.

但是,如果您想开始清理工作,您可以按照@KevinCrowell 的建议创建一个 members 表,从两个 members_company 表中填充它并用视图替换它们.您可以在视图上使用 INSTEAD OF 触发器将更新重定向"到新表.这仍然是一些工作,但这是在不破坏现有应用程序的情况下修复数据模型的一种方法(当然,如果在您的情况下可行)

But if you want to start cleaning things up you could create a members table as @KevinCrowell suggested, populate it from the two members_company tables and replace them with views. You can use INSTEAD OF triggers on the views to 'redirect' updates to the new table. This is still some work, but it would be one way to fix your data model without breaking existing applications (if it's feasible in your situation, of course)