데이터는 없고, SQL만 저장되어 있는 Object를 의미한다.
View를 select 하게 되면 View가 가지고 있는 SQL 문이 실행되는 것과 같다.
1. View를 사용하는 이유
inline view를 쓰면 되지 굳이 view를 Object로 만들어서 따로 관리하는 이유
보안과 함께 사용자의 편의성을 높힌다.
여러 테이블을 조인할 때, View를 통하면 SQL을 어느정도 간소화 시킬 수 있다.
복잡한 SQL을 편리하게 재생성할 수 있다.
예) 여러개의 schema나 user로 구성된 database가 있는데, 특정 유저가 A라는 스키마의 테이블을 보는 권한이 없고, 봐서도 안될 때는 view를 생성하여 필요한 데이터만 보여주고 전체 데이터는 공개하지 않도록 할 수 있다.
2. View의 특징
일반 테이블과 같은 형태를 가지고 있다.
데이터의 논리적 독립성을 제공하며, 필요한 데이터만 뷰로 정의하여 사용하기 때문에 관리가 용이하고 SQL 명령문이 간단해진다.
만들어진 뷰에 기본 테이블의 기본키를 포함하여 뷰를 생성하면 삽입, 삭제, 갱신, 연산이 가능하다.
한번 정의가 된 뷰의 경우 다른 뷰의 기본 데이터가 될 수 있다.
뷰에 정의되어 있는 기본 테이블이나 뷰를 삭제하게 되면 해당 데이터를 기초로 한 다른 뷰들이 모두 자동으로 삭제된다.
Alter 명령어를 사용할 수 없다. drop & create를 반복하여야 한다.
원본 테이블과 같은 이름으로 생성할 수 없으며, 보통 'vw_' 등으로 생성한다.
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 );
View 란?
가상의 테이블
데이터는 없고, SQL만 저장되어 있는 Object를 의미한다.
View를 select 하게 되면 View가 가지고 있는 SQL 문이 실행되는 것과 같다.
1. View를 사용하는 이유
inline view를 쓰면 되지 굳이 view를 Object로 만들어서 따로 관리하는 이유
2. View의 특징
일반 테이블과 같은 형태를 가지고 있다.
데이터의 논리적 독립성을 제공하며, 필요한 데이터만 뷰로 정의하여 사용하기 때문에 관리가 용이하고 SQL 명령문이 간단해진다.
만들어진 뷰에 기본 테이블의 기본키를 포함하여 뷰를 생성하면 삽입, 삭제, 갱신, 연산이 가능하다.
한번 정의가 된 뷰의 경우 다른 뷰의 기본 데이터가 될 수 있다.
뷰에 정의되어 있는 기본 테이블이나 뷰를 삭제하게 되면 해당 데이터를 기초로 한 다른 뷰들이 모두 자동으로 삭제된다.
Alter 명령어를 사용할 수 없다. drop & create를 반복하여야 한다.
원본 테이블과 같은 이름으로 생성할 수 없으며, 보통 'vw_' 등으로 생성한다.
3. 생성 및 삭제 예제
4. View 실행 원리 및 예제
1) view 생성문
2) view를 이용해 SQL
3) DBMS 옵티마이저가 인식하는 SQL
Reference