데이터베이스 설계
Lis Sulmont
Curriculum Manager
데이터베이스에서 뷰는 저장된 쿼리의 결과 집합으로, 사용자가 영구적인 데이터베이스 객체처럼 조회할 수 있습니다 (Wikipedia)
물리적 스키마에 속하지 않는 가상 테이블
CREATE VIEW view_name AS
SELECT col1, col2
FROM table_name
WHERE condition;

$$
목표: science fiction 장르의 제목과 저자 반환
CREATE VIEW scifi_books AS
SELECT title, author, genre
FROM dim_book_sf
JOIN dim_genre_sf ON dim_genre_sf.genre_id = dim_book_sf.genre_id
JOIN dim_author_sf ON dim_author_sf.author_id = dim_book_sf.author_id
WHERE dim_genre_sf.genre = 'science fiction';
SELECT * FROM scifi_books
| title | author | genre |
|-------------------------------|-------------------|-----------------|
| The Naked Sun | Isaac Asimov | science fiction |
| The Robots of Dawn | Isaac Asimov | science fiction |
| The Time Machine | H.G. Wells | science fiction |
| The Invisible Man | H.G. Wells | science fiction |
| The War of the Worlds | H.G. Wells | science fiction |
| Wild Seed (Patternmaster, #1) | Octavia E. Butler | science fiction |
| ... | ... | ... |
SELECT * FROM scifi_books
=
SELECT * FROM
(SELECT title, author, genre
FROM dim_book_sf
JOIN dim_genre_sf ON dim_genre_sf.genre_id = dim_book_sf.genre_id
JOIN dim_author_sf ON dim_author_sf.author_id = dim_book_sf.author_id
WHERE dim_genre_sf.genre = 'science fiction');
$$
SELECT * FROM INFORMATION_SCHEMA.views;
시스템 뷰 포함
SELECT * FROM information_schema.views
WHERE table_schema NOT IN ('pg_catalog', 'information_schema');
시스템 뷰 제외
$$

데이터베이스 설계