데이터베이스

SQL8: Joined Relations

소재훈 2021. 12. 23. 19:03

데이터베이스 목차

이 글에서는 University Database가 사용된다.

 

join연산은 두개의 relation을 받아서 하나의 결과테이블을 받는다.

join 연산은 두가지에 의해서 결정된다.

  1. join type
  2. join condition

Join type과 Join condition

join type은 다음과 같이 네가지가 있다.

  • Inner join
  • Left outer join
  • Right outer join
  • Full outer join
  • inner join과 outer join은 join 수행 시 매치가 되지 않는 튜플의 처리방법에 차이가 있다. inner join은 매치되지 않은 것은 결과 테이블에 넣지 않고, outer join은 포함시킨다.

 

join condition은 두개의 relation이 주어졌을 때 매치되는 tuple들을 어떻게 찾아내고, 결과 테이블을 만들어 낼 때 어떤 속성을 column list에 포함시킬 것인지를 말한다. 3가지의 join condition이 있다.

  • natural
    : 공통되는 column의 값이 같은 튜플을 찾고, 중복 column은 한번만 표현한다.
  • on <predicate>
    : 조건식을 만족시키는 tuple을 찾아낸다.
  • using (A1, A2, ..., An)
    : A1, A2, ..., An column의 값이 모두 같으면 tuple이 매칭된다.

join type 4가지, join condition 3가지를 고려하면 join operation은 총 12개가 만들어 질 수 있다.


Join Relation Example

myCourse, myPrereq

myCourse inner join myPrereq on myCourse.cID = myPrereq.cID

myCourse left outer join myPrereq on myCourse.cID = myPrereq.cID

on 키워드는 바로 매칭되는 튜플을 찾고 싶을 때 사용한다.

 

myCourse natural right outer join myPrereq

myCourse natural full outer join myPrereq

natural join condition만 join type 앞에 명시해준다. full outer join에서는 양쪽에서 매치되지 않은 튜플 추가된 것을 볼 수 있다.

myCourse natural left outer join myPrereq
myCourse left outer join myPrereq using(cID)

두 문장이 같은 역할을 한다. using condition을 사용해서 cID의 값이 같은 튜플을 매칭한다.