Proiectarea bazelor de date
Lis Sulmont
Curriculum Manager
Într-o bază de date, o vizualizare este setul de rezultate al unei interogări stocate asupra datelor, pe care utilizatorii o pot interoga ca pe un obiect persistent al bazei de date (Wikipedia)
Tabel virtual care nu face parte din schema fizică
CREATE VIEW view_name AS
SELECT col1, col2
FROM table_name
WHERE condition;

$$
Obiectiv: Returnarea titlurilor și autorilor din genul 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;
Includes system views
SELECT * FROM information_schema.views
WHERE table_schema NOT IN ('pg_catalog', 'information_schema');
Excludes system views
$$

Proiectarea bazelor de date