- 사용자에게 접근이 허용된 자료만을 제한적으로 보여주기 위해 하나 이상의 기본 테이블로부터 유도된, 이름을 가지는 가상 테이블
(주된 목적인듯)
# 뷰 생성
CREATE VIEW 뷰이름[(속성이름[,속성이름])]AS SELECT문;
# 뷰 삭제
DROP VIEW 뷰이름 RESTRICT or CASCADE
RESTRICT : 뷰를 다른곳에서 참조하고 있으면 삭제가 취소된다.
CASCADE : 뷰를 참조하는 다른 뷰나 제약 조건까지 모두 삭제된다.
큐브리드 중앙DB(C*****) 에도 뷰 테이블 존재 => 데이터를 효율적으로 관리하고 사용하기 위해
CREATE [OR REPLACE] {VIEW | VCLASS} view_name
[<subclass_definition>]
[(view_column_name, ...)]
[INHERIT <resolution>, ...]
[AS <select_statement>]
[WITH CHECK OPTION] ;
<subclass_definition> ::= {UNDER | AS SUBCLASS OF} table_name, ...
<resolution> ::= [CLASS] {column_name} OF superclass_name [AS alias]
CREATE TABLE a_tbl(
id INT NOT NULL,
phone VARCHAR(10));
INSERT INTO a_tbl VALUES(1,'111-1111'), (2,'222-2222'), (3, '333-3333'), (4, NULL), (5, NULL);
--creating a new view based on AS select_statement from a_tbl
CREATE VIEW b_view AS SELECT * FROM a_tbl WHERE phone IS NOT NULL WITH CHECK OPTION;
SELECT * FROM b_view;
id phone
===================================
1 '111-1111'
2 '222-2222'
3 '333-3333'
# with check option 사용시 컬럼값 update 불가능
UPDATE b_view SET phone=NULL;
=> ERROR: Check option exception on view b_view.
# create or replace 사용시 기존의 뷰를 새로운 뷰로 대체
CREATE OR REPLACE VIEW b_view AS SELECT * FROM a_tbl ORDER BY id DESC;
SELECT * FROM b_view;
id phone
===================================
5 NULL
4 NULL
3 '333-3333'
2 '222-2222'
1 '111-1111'
SELECT * FROM b_view;
id phone
===================================
1 '111-1111'
2 '222-2222'
3 '333-3333'
4 NULL
5 NULL
ALTER VIEW b_view ADD QUERY SELECT * FROM a_tbl WHERE id IN (1,2);
SELECT * FROM b_view;
id phone
===================================
1 '111-1111'
2 '222-2222'
3 '333-3333'
4 NULL
5 NULL
1 '111-1111'
2 '222-2222'
ALTER VIEW b_view AS SELECT * FROM a_tbl WHERE phone IS NOT NULL;
SELECT * FROM b_view;
id phone
===================================
1 '111-1111'
2 '222-2222'
3 '333-3333'
ALTER VIEW b_view ADD QUERY SELECT * FROM a_tbl WHERE id IN (1,2);
ALTER VIEW b_view ADD QUERY SELECT * FROM a_tbl WHERE id = 3;
SELECT * FROM b_view;
id phone
===================================
1 '111-1111'
2 '222-2222'
3 '333-3333'
4 NULL
5 NULL
1 '111-1111'
2 '222-2222'
3 '333-3333'
ALTER VIEW b_view CHANGE QUERY 2 SELECT * FROM a_tbl WHERE phone IS NULL;
SELECT * FROM b_view;
id phone
===================================
1 '111-1111'
2 '222-2222'
3 '333-3333'
4 NULL
5 NULL
4 NULL
5 NULL
3 '333-3333'
DROP VIEW b_view;
RENAME VIEW game_2004 AS info_2004;
참고링크