yeoseon / tip-archive

트러블 슈팅 및 팁을 모아두는 레포 (Today I Learned)
29 stars 2 forks source link

[Database] View #258

Open yeoseon opened 3 years ago

yeoseon commented 3 years ago

View 란?

가상의 테이블
image

데이터는 없고, SQL만 저장되어 있는 Object를 의미한다.
View를 select 하게 되면 View가 가지고 있는 SQL 문이 실행되는 것과 같다.

image

1. View를 사용하는 이유

inline view를 쓰면 되지 굳이 view를 Object로 만들어서 따로 관리하는 이유

2. View의 특징

3. 생성 및 삭제 예제

-- 생성문
CREATE VIEW 뷰이름 AS SELECT 구문;
-- 삭제문
DROP VIEW 뷰이름;

4. View 실행 원리 및 예제

1) view 생성문

SELECT name,
       money_received,
       money_sent,
       (money_received - money_sent) AS balance,
       address,
 ...
  FROM table_customers c
  JOIN accounts_table a
    ON a.customer_id = c.customer_id

2) view를 이용해 SQL

SELECT name,
       balance
  FROM accounts_view;

3) DBMS 옵티마이저가 인식하는 SQL

SELECT name,
       balance
  FROM (SELECT name,
               money_received,
               money_sent,
               (money_received - money_sent) AS balance,
               address,
 ...
          FROM table_customers c JOIN accounts_table a
               ON a.customer_id = c.customer_id        );

Reference