create table Person
(
Id int identity(1,1) primary key,
Name nvarchar(20)
)
create table Pet
(
Id int identity(1,1) primary key,
Pid int references Person(Id),
Name nvarchar(20)
)插入一些测试数据:
insert into Person select 'user1'
insert into Pet select SCOPE_IDENTITY(),'pet1'然后尝试删除Petson表的记录:
delete from Person where Id=1结果不能删除,错误信息如下:
消息 547,级别 16,状态 0,第 1 行
Delete 语句与 REFERENCE 约束"FK_Pet_Person"冲突。该冲突发生于数据库"test",表"dbo.Pet", column 'Pid'。
若确实要删除数据,并且要求相关联的数据也一起删除,只需在数据库外键关系中将更新/删除规则设置为级联即可:
评论(0)
暂无评论。