반응형
/********************************************************************************************
-- Title : [2k5] 재귀 관계에서의 데이터 삭제
-- Key word : recursive relation delete
********************************************************************************************/
-- 재귀 릴레이션이 맺어진 관계에서 데이터를 부분 통째로 삭제시
-- 다른 ROW에서 참조되는게 없다면 삭제되는 데이터간에 참조가 어떻게 되든
-- 다~~ 삭제된다... (뭔 소리여...당연한건가??)

drop table ttt;

create table ttt (a int not null, b int null);

alter table ttt
add constraint pk_ttt primary key(a)
, constraint fk_ttt_ttt foreign key(b) references ttt(a);

insert into ttt
select 1, null union all
select 2,1 union all
select 3,1 union all
select 4,2 union all
select 5,3 union all
select 6,4 union all
select 7,5 union all
select 8,5;
select * from ttt;

-- fk_ttt_ttt 충돌로 삭제 안됨.
delete from ttt where a = 1;

-- 상호간에 참조되는 것들을 모두 삭제할거 같은데...
-- a=8에서 b가 5를 참조하기에 삭제가 안된다.
delete from ttt
where a in (select a from ttt where a between 1 and 7);

select * from ttt;

-- 서로 참조가 되나 다른 ROW에서 참조되는게 없어 삭제 가능하다.
delete from ttt
where a in (select a from ttt where a in (6, 4, 2));

select * from ttt;
go
반응형

+ Recent posts