我正在查看所有表格的SSMS生成脚本,我注意到了这一点:
/****** Object: ForeignKey [FK_User__ACRNL__1C722D53] Script Date: 10/19/2015 16:28:12 ******/
ALTER TABLE [dbo].[User_Funding] WITH CHECK ADD FOREIGN KEY([ACRNLocation])
REFERENCES [dbo].[Location] ([location])
GO
/****** Object: ForeignKey [FK_User__ACRNL__1D66518C] Script Date: 10/19/2015 16:28:12 ******/
ALTER TABLE [dbo].[User_Funding] WITH CHECK ADD FOREIGN KEY([ACRNLocation])
REFERENCES [dbo].[Location] ([location])
GO
/****** Object: ForeignKey [FK_User__ACRNL__1E5A75C5] Script Date: 10/19/2015 16:28:12 ******/
ALTER TABLE [dbo].[User_Funding] WITH CHECK ADD FOREIGN KEY([ACRNLocation])
REFERENCES [dbo].[Location] ([location])
GO
/****** Object: ForeignKey [FK_User__ACRNL__4EFDAD20] Script Date: 10/19/2015 16:28:12 ******/
ALTER TABLE [dbo].[User_Funding] WITH CHECK ADD FOREIGN KEY([ACRNLocation])
REFERENCES [dbo].[Location] ([location])
GO
/****** Object: ForeignKey [FK_User__ACRNL__4FF1D159] Script Date: 10/19/2015 16:28:12 ******/
ALTER TABLE [dbo].[User_Funding] WITH CHECK ADD FOREIGN KEY([ACRNLocation])
REFERENCES [dbo].[Location] ([location])
GO
/****** Object: ForeignKey [FK_User__ACRNL__50E5F592] Script Date: 10/19/2015 16:28:12 ******/
ALTER TABLE [dbo].[User_Funding] WITH CHECK ADD FOREIGN KEY([ACRNLocation])
REFERENCES [dbo].[Location] ([location])
GO
/****** Object: ForeignKey [FK_User_ACRNL_7834CCDD] Script Date: 10/19/2015 16:28:12 ******/
ALTER TABLE [dbo].[User_Funding] WITH CHECK ADD FOREIGN KEY([ACRNLocation])
REFERENCES [dbo].[Location] ([location])
从我看到的User_Funding表中的位置有7个外键。
我可以删除其中的6个外键,或者我应该深入挖掘为什么有7个外键?
答案 0 :(得分:2)
是。删除除一个以外的所有
您可能正在运行一些SQL来发布更新。它可能是通过FK名称检查FK是否已经存在,但是然后创建一个没有名称的FK,产生随机的" _7834CCDD"。
将其更改为:
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_User_Funding_Location]') AND parent_object_id = OBJECT_ID(N'[dbo].[User_Funding]'))
ALTER TABLE [dbo].[User_Funding] ADD CONSTRAINT [FK_User_Funding_Location] FOREIGN KEY([ACRNLocation])
REFERENCES [dbo].[Location] ([location])
" ADD CONSTRAINT"之后的[FK_User_Funding_Location]确保名称不是随机的。
我已经处理过这个问题,甚至不得不编写一个脚本来删除除了一个之外的所有内容,并将最后一个重命名为非随机的。