日期:2014-05-17 浏览次数:20714 次
create table ta(
id int primary key,
name varchar(10)
)
go
insert ta
select 1,'test' union all
select 2,'test'
go
create table tb
(
id int foreign key references ta(id),
name varchar(10)
)
insert tb
select 1,'test01' union all
select 2,'test01'
go
select * from tb
--直接删除:
delete from ta where id=1
/*
消息 547,级别 16,状态 0,第 1 行
DELETE 语句与 REFERENCE 约束"FK__tb__id__64CCF2AE"冲突。
该冲突发生于数据库"master",表"dbo.tb", column 'id'。
语句已终止。
*/
--处理方法
alter table tb
drop constraint FK__tb__id__64CCF2AE
alter table tb
add constraint FK__tb__id__64CCF2AE foreign key(id) references ta(id)
on delete cascade
delete from ta where id=1
select * from ta
select * from tb
/*
id name
----------- ----------
2 test
(1 行受影响)
id name
----------- ----------
2 test01
(1 行受影响)
*/