我有两张桌子
ACADEMIE:
CREATE TABLE [dbo].[R_ACADEMIE](
[ID_ACADEMIE] [dbo].[IDENTIFIANT] NOT NULL,
[LC_ACADEMIE_CODE] [dbo].[LIBELLE_COURT] NOT NULL,
[LM_ACADEMIE_LIBELLE] [dbo].[LIBELLE_MOYEN] NOT NULL,
CONSTRAINT [PK_R_ACADEMIE] PRIMARY KEY NONCLUSTERED
(
[ID_ACADEMIE] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
我在我的E_VOYAGEUR
表
ALTER TABLE E_VOYAGEUR ADD ID_ACADEMIE IDENTIFIANT NOT NULL DEFAULT 0;
我想在E_VOYAGEUR
表格上创建一个FK,我这样做:
ALTER TABLE [dbo].[E_VOYAGEUR] WITH CHECK ADD CONSTRAINT [FK_E_VOYAGE_A_VOYAGEU_E_ACADEMIE] FOREIGN KEY([ID_ACADEMIE])
REFERENCES [dbo].[R_ACADEMIE] ([ID_ACADEMIE])
GO
我有这个错误:
ALTER TABLE语句与FOREIGN KEY冲突 约束" FK_E_VOYAGE_A_VOYAGEU_E_ACADEMIE" 。冲突 发生在数据库" DEV"," dbo.R_ACADEMIE"表,列' ID_ACADEMIE'
答案 0 :(得分:4)
看起来您的表E_VOYAGEUR已经有了一些值而您正在更改该表以添加新列ID_ACADEMIE,默认值为0。 这使得表E_VOYAGEUR中的所有行都为列ID_ACADEMIE的0值。当您向该表添加foreigh键约束时,参考表,[R_ACADEMIE]可能在ID_ACADEMIE列中没有值为“0”的条目,这似乎是导致此问题的原因。
在将外键添加到表E_VOYAGEUR之前,请确保ID_ACADEMIE列中的值具有表[R_ACADEMIE]中的引用。