INSERT 语句与 FOREIGN KEY 约束冲突,即使它为空?
嗨,
我有一个包含外键值(id)的表。该字段可以为 null,但问题是,如果将其设置为 null,则会出现以下异常:
INSERT 语句与 FOREIGN KEY 约束冲突
为什么?它可以为空
Edit1 :
USE [biss]
GO
/****** Object: Table [dbo].[VoucherCodes] Script Date: 10/16/2011 19:55:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[VoucherCodes](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Code] [nvarchar](50) NOT NULL,
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL,
[InactivatedDate] [datetime] NULL,
[AdCategoryId] [int] NULL,
CONSTRAINT [PK_VoucherCodes] PRIMARY KEY CLUSTERED
(
[Id] 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
ALTER TABLE [dbo].[VoucherCodes] WITH CHECK ADD
CONSTRAINT [FK_VoucherCodes_AdCategories] FOREIGN KEY([AdCategoryId])
REFERENCES [dbo].[AdCategories] ([Id])
ON UPDATE SET NULL
GO
ALTER TABLE [dbo].[VoucherCodes] CHECK CONSTRAINT [FK_VoucherCodes_AdCategories]
GO
ALTER TABLE [dbo].[VoucherCodes] WITH CHECK ADD
CONSTRAINT [FK_VoucherCodes_VoucherCodes] FOREIGN KEY([Id])
REFERENCES [dbo].[VoucherCodes] ([Id])
GO
ALTER TABLE [dbo].[VoucherCodes] CHECK CONSTRAINT [FK_VoucherCodes_VoucherCodes]
GO
ALTER TABLE [dbo].[VoucherCodes] WITH CHECK ADD
CONSTRAINT [FK_VoucherCodes_VoucherCodes1] FOREIGN KEY([Id])
REFERENCES [dbo].[VoucherCodes] ([Id])
GO
ALTER TABLE [dbo].[VoucherCodes] CHECK CONSTRAINT [FK_VoucherCodes_VoucherCodes1]
GO
ALTER TABLE [dbo].[VoucherCodes] ADD
CONSTRAINT [DF_VoucherCodes_Inactive] DEFAULT ((1)) FOR [InactivatedDate]
GO
Hi,
I have a table that contains a foreign key value(id). This field is able to be null but the problem is that I get the following exception if I set it to null :
The INSERT statement conflicted with the FOREIGN KEY constraint
Why? it is nullable
Edit1 :
USE [biss]
GO
/****** Object: Table [dbo].[VoucherCodes] Script Date: 10/16/2011 19:55:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[VoucherCodes](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Code] [nvarchar](50) NOT NULL,
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL,
[InactivatedDate] [datetime] NULL,
[AdCategoryId] [int] NULL,
CONSTRAINT [PK_VoucherCodes] PRIMARY KEY CLUSTERED
(
[Id] 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
ALTER TABLE [dbo].[VoucherCodes] WITH CHECK ADD
CONSTRAINT [FK_VoucherCodes_AdCategories] FOREIGN KEY([AdCategoryId])
REFERENCES [dbo].[AdCategories] ([Id])
ON UPDATE SET NULL
GO
ALTER TABLE [dbo].[VoucherCodes] CHECK CONSTRAINT [FK_VoucherCodes_AdCategories]
GO
ALTER TABLE [dbo].[VoucherCodes] WITH CHECK ADD
CONSTRAINT [FK_VoucherCodes_VoucherCodes] FOREIGN KEY([Id])
REFERENCES [dbo].[VoucherCodes] ([Id])
GO
ALTER TABLE [dbo].[VoucherCodes] CHECK CONSTRAINT [FK_VoucherCodes_VoucherCodes]
GO
ALTER TABLE [dbo].[VoucherCodes] WITH CHECK ADD
CONSTRAINT [FK_VoucherCodes_VoucherCodes1] FOREIGN KEY([Id])
REFERENCES [dbo].[VoucherCodes] ([Id])
GO
ALTER TABLE [dbo].[VoucherCodes] CHECK CONSTRAINT [FK_VoucherCodes_VoucherCodes1]
GO
ALTER TABLE [dbo].[VoucherCodes] ADD
CONSTRAINT [DF_VoucherCodes_Inactive] DEFAULT ((1)) FOR [InactivatedDate]
GO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的外键来自两个自引用循环 FK 约束。
您将 ID 列作为其自身的 FK:它们当然会失败,因为它们在 INSERT 时不存在。
运行这个
Your foreign key comes from the two self referencing circular FK constraints.
You have the ID columns as FKs of themselves: of course they will fail because they won't exists at INSERT time.
Run this