2
我有兩個表與外鍵約束
衝突琪:
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;
我想創建一個FK在我的E_VOYAGEUR
桌上我這樣做:
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
我有這樣的錯誤:
The ALTER TABLE statement is in conflict with the FOREIGN KEY constraint " FK_E_VOYAGE_A_VOYAGEU_E_ACADEMIE " . The conflict occurred in database " DEV ", " dbo.R_ACADEMIE " table, column ' ID_ACADEMIE ' .
聽起來好像你在R_ACADEMIE中不存在E_VOYAGEUR中的ID_ACADEMIE值。當引用的表中不存在某個值時,無法將外鍵添加到現有數據中。您將需要插入缺失的值。 – SMM
@SMM我更新了我的文章。在'R_ACADEMIE'中,我有'ID_ACADEMIE' – Mercer
您是否將E_VOYAGEUR.ID_ACADEMIE中的值作爲R_ACADEMIE中的行插入?例如您用'not null default 0'將新列添加到E_VOYAGEUR中...您是否在R_ACADEMIE中有ID_ACADEMIE = 0的記錄? – SMM