我有两张桌子
学术界:
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表格中添加列
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”中发生了冲突。
看来您的表E_VOYAGEUR已经具有一些值,并且您正在更改该表以添加新列ID_ACADEMIE,其默认值为0。这将使表E_VOYAGEUR中的所有行的ID为ID_ACADEMIE列,其值为0。并且,当您使用foreeigh键约束向该表添加引用表时,[R_ACADEMIE]可能在ID_ACADEMIE列中没有值为‘0’的条目,这似乎是导致此问题的原因。
在将外键添加到表E_VOYAGEUR之前,请确保ID_ACADEMIE列中的值在表[R_ACADEMIE]中具有引用。