我想創建一個BOM
結構我有6個產品表,其中包含不同的屬性和BOMHEADER
和BOMDETAIL
表。在創建BOM結構之前,我希望驗證或檢查六個表中的任何一箇中是否存在bomitem
。所以我試圖創建BOMHEADER
字段,如下所示使用多個約束,但我得到以下錯誤消息 "The INSERT Statement conflicted with the FOREIGN KEY constraint如何引用外鍵到多個表?
什麼是解決問題的最佳方法。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[BOMHEAD](
[bomItem] [int] NOT NULL,
[bomRev] [nvarchar](6) NOT NULL,
[rollup] [bit] NULL,
CONSTRAINT [PK_BOMHEAD_KEY_0] PRIMARY KEY CLUSTERED
(
[bomItem] ASC,
[bomRev] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[BOMHEAD] WITH CHECK ADD FOREIGN KEY([bomItem])
REFERENCES [dbo].[parts] ([itemId])
GO
ALTER TABLE [dbo].[BOMHEAD] WITH CHECK ADD FOREIGN KEY([bomItem])
REFERENCES [dbo].[Tires] ([titemId])
GO
ALTER TABLE [dbo].[BOMHEAD] WITH CHECK ADD FOREIGN KEY([bomItem])
REFERENCES [dbo].[Discs] ([itemId])
GO
ALTER TABLE [dbo].[BOMHEAD] WITH CHECK ADD FOREIGN KEY([bomItem])
REFERENCES [dbo].[Rims] ([itemId])
GO
ALTER TABLE [dbo].[BOMHEAD] WITH CHECK ADD FOREIGN KEY([bomItem])
REFERENCES [dbo].[Wheel] ([wheelItemId])
GO
ALTER TABLE [dbo].[BOMHEAD] WITH CHECK ADD FOREIGN KEY([bomItem])
REFERENCES [dbo].[Assemblies] ([itemId])
GO
我認爲你的問題可能是它沒有檢查任何表中是否存在外鍵,而是檢查它是否存在於所有表中。 –
@RichBenner我厭倦了'NOCHECK AND CHECK'我得到相同的 – afri
我不認爲外鍵是你以後,看看是否有助於http://stackoverflow.com/questions/30918873/check-if- id-exists-in-multiple-tables你可能需要一個觸發器來做你正在做的事情。 –