我想删除子表上的行。我收到错误
DELETE 语句与 REFERENCE 约束“FK_Address_UserDataSet”冲突。冲突发生在数据库“XYZ”、表“dbo.Address”、列“DataSetId”中。该语句已终止。
我有一个带有父表UserDataSet
和子 Address
表的数据库结构(父表可以有任意数量的子表)。
有一个外键约束(在错误中提到)要求孩子DataSetId
与有效的UserDataSet
.
以下是以MS SQL Server Management Studio 2008
简化形式创建的表和约束脚本:
CREATE TABLE [dbo].[Address](
[AddressId] [int] IDENTITY(1,1) NOT NULL,
[DataSetId] [int] NOT NULL,
--other fields
CONSTRAINT [PK_Address] PRIMARY KEY CLUSTERED
(
[AddressId] 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].[UserDataSet](
[DataSetId] [int] IDENTITY(1,1) NOT NULL,
--other fields
CONSTRAINT [PK_UserDataSet] PRIMARY KEY CLUSTERED
(
[DataSetId] 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 the constraint
ALTER TABLE [dbo].[Address] WITH NOCHECK ADD CONSTRAINT [FK_Address_UserDataSet] FOREIGN KEY([DataSetId])
REFERENCES [dbo].[UserDataSet] ([DataSetId])
GO
ALTER TABLE [dbo].[Address] CHECK CONSTRAINT [FK_Address_UserDataSet]
GO
但是,在这个设置中删除一个孩子(而不是父母)怎么会成为一个问题呢?
是否可能是要删除的行当前无效,可能是在约束尚未(尚未)使用时添加的),现在在删除具有无效外键的子项时强制执行约束?