설문조사
PostgreSQL/PPAS 관련 듣고 싶은 교육은


총 게시물 162건, 최근 0 건
   

디비랑-7) 상관관계 서브쿼리시 이슈 - 고전

글쓴이 : 디비랑 날짜 : 2014-12-11 (목) 20:54 조회 : 4448
#-- 테이블 생성
create table ttt
( tkey varchar(30) not null
, seq smallint not null
, name varchar(300)
, constraint pk_ttt primary key (tkey, seq)
);

create table fff
( del_tkey varchar(30) not null
, kind char(2) not null
);

#-- 데이터 입력
insert into ttt
select 'aaaaa', 1, 'aaaaaaaaaaaaaa 1111111111111' union all
select 'aaaaa', 2, 'aaaaaaaaaaaaaa 2222222222222' union all
select 'bbbbb', 1, 'bbbbbbbbbbbbbb 1111111111111' union all
select 'ccccc', 1, 'cccccccccccccc 1111111111111' union all
select 'ccccc', 2, 'cccccccccccccc 2222222222222' union all
select 'ccccc', 3, 'cccccccccccccc 3333333333333' union all
select 'ddddd', 1, 'dddddddddddddd 1111111111111' union all
select 'ddddd', 2, 'dddddddddddddd 2222222222222';

insert into fff
select 'bbbbb', 'b1' union all
select 'ddddd', 'd1';

#-- 데이터 확인
select * from ttt;
select * from fff;

#-- 데이터 삭제
/*-- 서브쿼리에서 fff에 없는 tkey 사용
delete from ttt where tkey in (select tkey from fff);

#-- 모든 데이터 삭제 확인
select * from ttt;

#-- 이유

서브쿼리 내부에서 외부 테이블 열을 참조할 수 있고, 이 경우 상관 서브쿼리로 취급됩니다(ANSI).
그래서 Alias를 지정해 주는 것이 좋습니다.

delete from ttt where tkey in (select a.tkey from fff a); 




   

postgresdba.com