Alter statement 与外键冲突,但ID在两者中都存在table

Alter statement Conflict with foreign key, but ID exisits in both table

我有 2 个 table,我在这里尝试插入 FK 关系。但是,当我尝试更改 table 以创建 FK 关系时出现错误,请帮助...我在两个 table 中都有俱乐部 ID,我在下面评论过,但我不确定为什么我仍然收到约束错误。

是什么导致了约束错误?

CREATE TABLE [dbo].[Clubs](
[ClubID] [int] IDENTITY(1,1) NOT NULL,  // ClubID is here
[Name] [nvarchar](128) NOT NULL,
[Description] [nvarchar](2047) NULL,
[Created] [datetime] NOT NULL,
[Modified] [datetime] NULL,


CONSTRAINT [PK_Clubs] PRIMARY KEY CLUSTERED 
(
    [ClubID] 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

CREATE TABLE [dbo].[MemberClubs](
[MemberClubID] [int] IDENTITY(1,1) NOT NULL,
[MemberID] [bigint] NOT NULL,
[ClubID] [int] NOT NULL,              //Club ID is here as well
 CONSTRAINT [PK_MemberClubs] PRIMARY KEY CLUSTERED 
(
    [MemberClubID] 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    SET IDENTITY_INSERT [dbo].[MemberClubs] ON 
GO    INSERT [dbo].[MemberClubs] ([MemberClubID], [MemberID], [ClubID]) VALUES (1, 1, 1)
GO    INSERT [dbo].[MemberClubs] ([MemberClubID], [MemberID], [ClubID]) VALUES (2, 2, 1)
GO    INSERT [dbo].[MemberClubs] ([MemberClubID], [MemberID], [ClubID]) VALUES (3, 3, 1)
GO    INSERT [dbo].[MemberClubs] ([MemberClubID], [MemberID], [ClubID]) VALUES (4, 4, 1)
GO    INSERT [dbo].[MemberClubs] ([MemberClubID], [MemberID], [ClubID]) VALUES (5, 5, 1)
GO    SET IDENTITY_INSERT [dbo].[MemberClubs] OFF
GO


// *** The error is over here 
ALTER TABLE [dbo].[MemberClubs]  WITH CHECK ADD  CONSTRAINT [FK_MemberClubs_Clubs] FOREIGN KEY([ClubID])
REFERENCES [dbo].[Clubs] ([ClubID])
GO

// $$$ The second FK constraint error is over here
ALTER TABLE [dbo].[MemberClubs] CHECK CONSTRAINT [FK_MemberClubs_Clubs]
GO

ALTER TABLE [dbo].[MemberClubs]  WITH CHECK ADD  CONSTRAINT [FK_MemberClubs_Members] FOREIGN KEY([MemberID])
REFERENCES [dbo].[Members] ([MemberID])
GO

ALTER TABLE [dbo].[MemberClubs] CHECK CONSTRAINT [FK_MemberClubs_Members]
GO

错误:

消息 547,级别 16,状态 0,行 * * *(以上)

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_MemberClubs_Clubs". The conflict occurred in database "newDb", table "dbo.Clubs", column 'ClubID'.

消息 4917,级别 16,状态 0,行 $$$(以上)

Constraint 'FK_MemberClubs_Clubs' does not exist.

消息 4916,级别 16,状态 0,行 $$$(以上)

Could not enable or disable the constraint. See previous errors.

您将 5 行添加到 table MemberClubs 引用 Club.ClubId = 1,然后将外键关系添加到 Clubs

因此,为了保持参照完整性,您需要在添加/启用外键约束之前将带有 ClubId = 1 的行添加到 Clubs 中:

SET IDENTITY_INSERT [dbo].[Clubs] ON 
INSERT INTO [dbo].[Clubs](ClubId, Name, Description, Created, Modified) 
       VALUES (1, 'Foo', 'Bar', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
SET IDENTITY_INSERT [dbo].[Clubs] OFF

则外键创建成功:

ALTER TABLE [dbo].[MemberClubs]  WITH CHECK 
    ADD  CONSTRAINT [FK_MemberClubs_Clubs] FOREIGN KEY([ClubID])
REFERENCES [dbo].[Clubs] ([ClubID])
GO

如果这不是一个例子,而是您实际想要完成的,我可以通过查看您的脚本看到的一件事是您正在尝试与 table 使用创建 FK 关系数据不一致。

为了创建约束,必须存在一致的数据。在您发布的代码中,Clubs 没有数据,因此由于 MemberClubs 具有 "orphaned" 数据,因此无法成功创建约束 - 例如MemberClubs 正在尝试引用 Clubs 中不存在的项目。