DB - Lecture2

Pyro·2021년 9월 1일
0

Database

목록 보기
2/8

데이터베이스 2일차

Relations and Relational Algebra

Relation

relation 을 매우 쉽게 생각하면 그냥 collection of tuples having same attributes 이다.

  • Relation 을 표시하는데 Table 을 사용한다.

  • Row == Tuple

  • Column == Attribute

  • Degree of a relation

    • column 갯수
    • Binary Relation: 2 dgeree
    • Ternary Relation: 3 degree
  • Cardinality of a relation

    • row 갯수

Data Manipulation (relational algebra)

  • Data 를 relation 으로 표현하면, Algebra 이론을 통해 data를 매우 쉽게 조작(manipulate)할 수 있다.
  • Number Algera Manipulation: +, -, /, *
  • Boolean Algebra Manipulation: &, |, !
  • Relational Algebra Manipulation
    • Union, Intersection, Difference, Projection, Selection, Product

Union-compatible relations

  • Domain 이 같아야 한다 == degree 와 attributes 가 같아야한다.
  • 이 조건을 만족해야 다음 manipulation 이 가능하다.
    • Union
    • Difference
    • Intersection

Relation-specific operators

Union

"SQL 레벨업" 책에서는 조건 분기를 UNION 으로 하고 있다면, CASE 구문을 사용하여 처리하도록 고쳐야한다고 한다.

SELECT p_name AS name FROM programmer
UNION
SELECT s_name AS name FROM student;

Intersection

오라클에서만 지원되고, MySQL 에서는 지원되지 않는다고 한다.

SELECT p_name AS name FROM programmer
INTERSECT
SELECT s_name AS name FROM student;

Difference

오라클에서만 지원되고, MySQL 에서는 지원되지 않는다고 한다.

SELECT p_name AS name FROM programmer
MINUS
SELECT s_name AS name FROM student;

Projection

SELECT (column names) FROM (table name)

테이블에서 정보를 가져올 때 * 이 아니라, 가져올 attribute 를 마음껏 정할 수 있는게 projection 이다.

Selection

SELECT * FROM (table name) WHERE (unary operator)

WHERE 문을 쓸 수 있다는게 Selection 이다.

  • Row Selector (unary operator)

Cartesian Product

  • CROSS JOIN 이 Cartesian Product 연산이다.
  • Union-compatible 하지 않아도 사용가능한 연산이다.
  • Cartesian Product 를 새로운 tuple 로 표현하면 너무 더러워지므로, flatten 하는 과정을 거친다. 이를 Extended Cartesian Product 라고 한다.

결론

Relational operator 를 통해 연산한 결과는 다시 relation 이다!

profile
dreams of chronic and sustained passion

0개의 댓글