SQL - VIEWS A view is a database object that is of a stored query. A view can be accessed as a virtual table in PostgreSQL.
In other words, a PostgreSQL view is a logical table that represents data of one or more underlying tables through
a SELECT statement. These are session based views Consider we have two table as below create table company(id int, name varchar, place varchar); postgres=# Insert into company values(1,'babu','chennai'); postgres=# Insert into company values(2,'pappu','avadi'); postgres=# Insert into company values(3,'rosh','chepauk'); postgres=# Insert into company values(4,'div','chepauk'); postgres=# Insert into company values(5,'divya','chetpet'); postgres=# select * from company; id | name | place ----+-------+---------- 1 | babu | chennai 1 | pappu | mylapore 2 | pappu | avadi 3 | rosh | chepauk 4 | div | chepauk 5 | divya | chetpet (6 rows) postgres=# create table emp(id int, name varchar,sal int); postgres=# Insert into emp values(1,'babu',2000) postgres=# Insert into emp values(2,'pappu',4000); postgres=# Insert into emp values(3,'rosh',5000); postgres=# Insert into emp values(4,'div',6000); postgres=# Insert into emp values(5,'divya',8000); postgres=# select * from emp; id | name | sal ----+-------+------ 1 | babu | 2000 2 | pappu | 4000 3 | rosh | 5000 4 | div | 6000 5 | vidya | 8000Creating temporary view : postgres=# create temporary view v1_tmp as select a.id, a.name, b.place from emp a inner join company b on a.id=b.id; CREATE VIEW postgres=# select * from v1_tmp; id | name | place ----+-------+---------- 1 | babu | chennai 1 | babu | mylapore 2 | pappu | avadi 3 | rosh | chepauk 4 | div | chepauk 5 | vidya | chetpet (6 rows)List of views postgres=# \dv List of relations Schema | Name | Type | Owner -----------+--------+------+---------- pg_temp_3 | v1_tmp | view | postgresUpdatable Views : Simple views are automatically updatable: the system will allow INSERT, UPDATE and DELETE statements to be used on the view
in the same way as on a regular table.
postgres=# create view v1 as select a.id, a.name, b.place from emp a inner join company b on a.id=b.id;CREATE VIEW postgres=# \dv List of relations Schema | Name | Type | Owner -----------+--------+------+---------- public | v1 | view | postgres (2 rows) postgres=# select * from v1; id | name | place ----+-------+---------- 1 | babu | chennai 1 | babu | mylapore 2 | pappu | avadi 3 | rosh | chepauk 4 | div | chepauk 5 | vidya | chetpet (6 rows) postgres=# select * from pg_views where schemaname='public'; schemaname | viewname | viewowner | definition ------------+----------+-----------+------------------------------------------ public | v1 | postgres | SELECT a.id, a.name, b.place FROM (emp a JOIN company b ON ((a.id = b.id)));Update the table will automatically update view : postgres=# update company set place ='madurai' where id=1; UPDATE 2 postgres=# select * from company; id | name | place ----+-------+--------- 2 | pappu | avadi 3 | rosh | chepauk 4 | div | chepauk 5 | divya | chetpet 1 | babu | madurai 1 | pappu | madurai (6 rows) postgres=# select * from v1; id | name | place ----+-------+--------- 1 | babu | madurai 1 | babu | madurai 2 | pappu | avadi 3 | rosh | chepauk 4 | div | chepauk 5 | vidya | chetpet (6 rows)Drop view : postgres=# drop view v1; DROP VIEW postgres=# select * from pg_views where schemaname='public'; schemaname | viewname | viewowner | definition ------------+----------+-----------+------------ (0 rows)Materialized Views : postgres=# create materialized view v1 as select a.id, a.name, b.place from emp a inner join company b on a.id=b.id; SELECT 6 postgres=# select * from v1; id | name | place ----+-------+--------- 1 | babu | madurai 1 | babu | madurai 2 | pappu | avadi 3 | rosh | chepauk 4 | div | chepauk 5 | vidya | chetpet (6 rows) postgres=# postgres=# select * from pg_matviews; schemaname | matviewname | matviewowner | tablespace | hasindexes | ispopulated | definition ------------+-------------+--------------+------------+------------+-------------+------------------------------------------ public | v1 | postgres | | f | t | SELECT a.id, + | | | | | | a.name, + | | | | | | b.place + | | | | | | FROM (emp a + | | | | | | JOIN company b ON ((a.id = b.id))); postgres=# update company set place ='kollam' where id=5;Update : postgres=# select * from company; id | name | place ----+-------+--------- 2 | pappu | avadi 3 | rosh | chepauk 4 | div | chepauk 1 | babu | madurai 1 | pappu | madurai 5 | divya | kollam (6 rows) postgres=# select * from v1; id | name | place ----+-------+--------- 1 | babu | madurai 1 | babu | madurai 2 | pappu | avadi 3 | rosh | chepauk 4 | div | chepauk 5 | vidya | chetpet (6 rows) postgres=# refresh materialized view v1; REFRESH MATERIALIZED VIEW postgres=# select * from v1; id | name | place ----+-------+--------- 1 | babu | madurai 1 | babu | madurai 2 | pappu | avadi 3 | rosh | chepauk 4 | div | chepauk 5 | vidya | kollam (6 rows) postgres=# vacuum analyze v1;Vacuum : postgres=# drop materialized view v1; DROP MATERIALIZED VIEW postgres=# \dm+ Did not find any relations. postgres=# select * from pg_matviews; schemaname | matviewname | matviewowner | tablespace | hasindexes | ispopulated | definition ------------+-------------+--------------+------------+------------+-------------+------------ (0 rows) ☛ Join to Learn from Experts: PostgreSQL SQL Course in Chennai by TesDBAcademy
« Previous
Next Topic »
(SQL - Sequence)
|