更改语句与外键冲突,但两个表中的ID都存在

时间:2015-04-22 19:05:06

标签: sql sql-server foreign-keys database-schema database-administration

我有两张桌子,这里我试图插入一个FK关系。但是,当我尝试改变表格以创建FK关系时我遇到错误请帮助...我在两个表中都有俱乐部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

错误

Msg 547,Level 16,State 0,Line * * * (上图)

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

Msg 4917,Level 16,State 0,Line $$$ (上图)

Constraint 'FK_MemberClubs_Clubs' does not exist.

Msg 4916,Level 16,State 0,Line $$$ (上图)

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

2 个答案:

答案 0 :(得分:2)

在引用MemberClubs的表Club.ClubId = 1中添加5行,然后将外键关系添加到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

答案 1 :(得分:2)

如果这不是一个例子并且是你实际想要完成的事情,那么我只能通过查看你的脚本看到的一件事就是你尝试使用不一致的数据创建与表的FK关系

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