A view is a database object that is a stored query. A view is a virtual table you can create dynamically using a saved query acting as a virtual table.
You can join a view to another table or view
You can query a view
Regular views don't store any data, but materialised view does
Syntax
CREATE OR REPLACEVIEWview_nameAS query
Example 1
CREATE OR REPLACEVIEWv_movie_quickASSELECT movie_name, movie_length, release_datefrom movies mv;-- use just like a normal tableselect*from v_movie_quick limit5; movie_name | movie_length | release_date --------------------+--------------+-------------- A Clockwork Orange | 112 | 1972-02-02 Apocalypse Now | 168 | 1979-08-15 Battle Royale | 111 | 2001-01-04 Blade Runner | 121 | 1982-06-25 Chungking Express | 113 | 1996-08-03
Example with JOIN
CREATE OR REPLACEVIEWv_movie_d_nameasSELECT movie_name, movie_length, release_date, d.first_name ||' '|| d.last_name as"full name"from movies mvinner join directors d on mv.director_id = d.director_id;select*from v_movie_d_name limit5; movie_name | movie_length | release_date | full name------------------------+--------------+--------------+----------------- Let the Right One In | 128 | 2008-10-24 | Tomas Alfredson There Will Be Blood | 168 | 2007-12-26 | Paul Anderson The Darjeeling Limited | 119 | 2007-09-29 | Wes Anderson Rushmore | 104 | 1998-11-12 | Wes Anderson Grand Budapest Hotel | 117 | 2014-07-03 | Wes Anderson
Managing VIEW
ALTERVIEW v_movie_d_name RENAME TO v_movie_with_names;-- ALTER VIEWselect*from v_movie_with_names limit5;-- SAME OUTPUT AS ABOVE-- dropping viewDROPVIEWifexists v_movie_quick;
View containing condition
CREATE OR REPLACEVIEWv_movie_after_1997asselect*from movieswhere release_date >='1997-12-31'and movie_lang ='English'order by release_date desclimit5; movie_id | movie_name | movie_length | movie_lang | release_date | age_certificate | director_id ----------+--------------------------------------------+--------------+------------+--------------+-----------------+-------------47 | Three Billboards Outside Ebbing, Missouri | 134 | English | 2017-11-10 | 15 | 1815 | Grand Budapest Hotel | 117 | English | 2014-07-03 | PG | 322 | Life of Pi | 129 | English | 2012-11-21 | PG | 1538 | Submarine | 115 | English | 2011-06-03 | 15 | 424 | Never Let Me Go | 117 | English | 2010-09-15 | 15 | 25
You cannot add, update, delete columns from a view once created for that create a new view, drop the old view and rename the new view back to original
Deleting from view also deletes from the main table
CREATE OR REPLACEVIEWv_movieasselect*from movies;select*from v_movie limit5;select*from movies limit5;-- deleting from v_movie also deletes from main tabledeletefrom v_moviewhere movie_id =4;
Materialized View
Allows you to
store result of a query
update data periodically :: manual
used to cache result of heavy data
syntax
CREATE MATERIALIZED VIEW IFNOTEXISTS view_name AS query WITH [ NO ] DATA;
If you want to load data into the materialised view at the creation time, you will use WITH DATA
create materialized view ifnotexists mv_dir asselect first_name, last_namefrom directorswithnodata;-- this will give error becuz no data-- is loaded at time of creationselect*from mv_dir limit5;-- refreshing view to load data in viewrefresh materialized view mv_dir;select*from mv_dir limit5;-- dropping materialized viewdrop materialized view mv_dir;
cannot change data in materialised view : insert, update and delete
advantage of usingmaterialised view : access and update materialised view without locking everyone else out
disadvantage of using materialised view if you alter the base table ,
materialised view must also me alter : delete the old materialised view and create a new one