본문 바로가기

Database

[Database] relation Algebra 관계대수

반응형


이번 포스트에서는 위사진을 토대로 예시설명을 하겠습니다.

Unary operations

단항연산에 관하여 설명하겠습니다.

Select operator

pick certain rows(열). 즉 조건을 만족하는 열 가져옵니다.

  • students with GPA >3.7 == σ GPA>3.7 Student
  • applications to stanford cs major == σ cName ='stanford' Λ major ='cs' Apply
    이를 일반화 해서 나타내자면 σ conditions (expr) 이 됩니다.

Project operator

pick certain columns. 즉 조건을 만족하는 행 (attributes)을 가져옵니다.
project연산을 하게 되면 중복이 제거됩니다. 즉 distinctive한 튜플입니다.

  • id and desition of all applications == 𝚷sId, dec Apply
    이를 일반화해서 나타내자면 𝚷 attr1,attr2,attr3....(expr)이 됩니다.

두 단항 연산자는 행과열 중에서 어떠한것을 가져오는지에 관한 차이가 있습니다.

두 operator를 같이 사용하면 어떻게 될까

예제를 들어서 설명해 보도록하겠습니다.

  • id and name of students with GPA>3.7
    이 뜻은 GPA가 3.7이 넘는 학생들중 id, name만 뽑아내라는 것입니다.
    id, name 은 attributes이기 때문에 이는 project operator를 사용해서 뽑아내고 GPA는 select를 사용해서 뽑아내면 됩니다. 이를 쿼리문으로 나타내면 𝚷 sid,sname(σ GPA>3.7 Student) 가 됩니다.

Binary Operations

cross-product

combine two relations.
이 연산의 결과는

  • attributes수 : Atable의 attributes수 + Btable의 attributes가 됩니다.
  • tuple수 : Atable tuples * Btable tuples 가 됩니다.
  • names and GPAs of students with HS>1000 who applied to CS and were rejected. == 𝚷 sName,GPA(σ student.sID=Apply.sID(Student * Apply)Λ HS>1000 Λ major='cs'Λdec ='r')로 나타내면 됩니다.
  • 이때 주의해햐할점은 두테이블을 연결시켜주는 조건을 추가해주는것인데 위의 쿼리문에서는 student.sID=Apply.sID 이 해당됩니다

Natural Join ⋈

join에는 theta join, equi join, natural join, semi join, outer join등이 있습니다. natrual join의 특직을 살펴보겠습니다 .

  • enforce equality on all attributes with same name.
  • eliminate one copy of duplicate attributes.
    natural join은 두 relations사이의 중복된 attributes를 제거하고 합친 결과를 return 합니다.

Theta Join ⋈𝜽

두 relation의 attributes 값을 비교하여 조건을 만족하는 tuple만 반환한다.

Λ 𝚷 ⋈

반응형

'Database' 카테고리의 다른 글

[Database] trigger, constraints  (0) 2019.12.16
[Database] Index, Transaction Isolation  (0) 2019.12.14
[Database] SQL subquery  (0) 2019.10.10
[Database] Select -1편  (0) 2019.10.06