warehouse_db=# create table tab_view(emp_id int not null,emp_name varchar(10),emp_city varchar(10));
CREATE TABLE
warehouse_db=# insert into tab_view values (1,'Adam','Chicago');
INSERT 0 1
warehouse_db=# insert into tab_view values (2,'John','Miami'),(3,'Smith','Dallas');
INSERT 0 2
warehouse_db=# create view view_select as select * from tab_view ;
CREATE VIEW
warehouse_db=# create rule view_select_insert as on insert to view_select
do instead (insert into tab_view values (new.emp_id,new.emp_name,new.emp_city));
CREATE RULE
warehouse_db=# select rulename from pg_rewrite where rulename='view_select_insert';
rulename
--------------------
view_select_insert
(1 row)
warehouse_db=# insert into view_select values (4,'Gary','Houston');
INSERT 0 1
warehouse_db=# select * from tab_view ;
emp_id | emp_name | emp_city
--------+----------+----------
1 | Adam | Chicago
2 | John | Miami
3 | Smith | Dallas
4 | Gary | Houston
(4 rows)
warehouse_db=# select * from view_select ;
emp_id | emp_name | emp_city
--------+----------+----------
1 | Adam | Chicago
2 | John | Miami
3 | Smith | Dallas
4 | Gary | Houston
(4 rows)
warehouse_db=# select * from warehouse_tb1 ;
warehouse_id | warehouse_name | year_created | street_address | city | state | zip
--------------+----------------+--------------+----------------+------+-------+-----
(0 rows)
warehouse_db=# create view view_warehouse_tb1 as select * from warehouse_tb1 ;
CREATE VIEW
warehouse_db=# select * from view_warehouse_tb1 ;
warehouse_id | warehouse_name | year_created | street_address | city | state | zip
--------------+----------------+--------------+----------------+------+-------+-----
(0 rows)
warehouse_db=# select * from history ;
history_id | date | amount | data | customer_id | warehouse_id
------------+------+--------+------+-------------+--------------
(0 rows)
warehouse_db=# create view view_multiple_tables as select warehouse_name,year_created,city,amount,date
from warehouse_tb1 ,history where warehouse_tb1.warehouse_id = history.warehouse_id;
CREATE VIEW
warehouse_db=# select * from view_multiple_tables ;
warehouse_name | year_created | city | amount | date
----------------+--------------+------+--------+------
(0 rows)
warehouse_db=# insert into warehouse_tb1 (warehouse_id ,warehouse_name,year_created,street_address,
city,state,zip)values (6,'Jackson & Co',2010,'lincoln Road','Buffalo','NY',4331);
INSERT 0 1
warehouse_db=# insert into warehouse_tb1 (warehouse_id ,warehouse_name,year_created,street_address,
city,state,zip)values (2,'Nicon & Co',2010,'Bush Road','Washington','NY',4331);
INSERT 0 1
warehouse_db=# insert into history (history_id ,date,amount,data,customer_id,warehouse_id)
warehouse_db-# values (1,'Jul--10-14',1234,'thedata',1,6);
INSERT 0 1
warehouse_db=# insert into history (history_id ,date,amount,data,customer_id,warehouse_id)
values (2,'Jul-10-15',2345,'thedatasecond',2,2);
INSERT 0 1
warehouse_db=# select * from view_multiple_tables ;
warehouse_name | year_created | city | amount | date
----------------+--------------+------------+--------+---------------------
Jackson & Co | 2010 | Buffalo | 1234 | 2014-07-10 00:00:00
Nicon & Co | 2010 | Washington | 2345 | 2015-07-10 00:00:00
(2 rows)
warehouse_db=# dv
List of relations
Schema | Name | Type | Owner
--------+----------------------+------+----------
public | view_multiple_tables | view | postgres
public | view_select | view | postgres
public | view_warehouse_tb1 | view | postgres
(3 rows)
warehouse_db=# drop view view_warehouse_tb1 ;
DROP VIEW
warehouse_db=# dv
List of relations
Schema | Name | Type | Owner
--------+----------------------+------+----------
public | view_multiple_tables | view | postgres
public | view_select | view | postgres
(2 rows)
warehouse_db=# h create view
Command: CREATE VIEW
Description: define a new view
Syntax:
CREATE [ OR REPLACE ] [ TEMP | TEMPORARY ] [ RECURSIVE ] VIEW name [ ( column_name [, ...] ) ]
[ WITH ( view_option_name [= view_option_value] [, ... ] ) ]
AS query
[ WITH [ CASCADED | LOCAL ] CHECK OPTION ]
warehouse_db=# h create materialized view
Command: CREATE MATERIALIZED VIEW
Description: define a new materialized view
Syntax:
CREATE MATERIALIZED VIEW table_name
[ (column_name [, ...] ) ]
[ WITH ( storage_parameter [= value] [, ... ] ) ]
[ TABLESPACE tablespace_name ]
AS query
[ WITH [ NO ] DATA ]
warehouse_db=# create materialized view mat_view as select * from warehouse_tb1 with no data;
SELECT 0
warehouse_db=# select * from mat_view ;
ERROR: materialized view "mat_view" has not been populated
HINT: Use the REFRESH MATERIALIZED VIEW command.
warehouse_db=# refresh materialized view mat_view ;
REFRESH MATERIALIZED VIEW
warehouse_db=# select * from mat_view ;
warehouse_id | warehouse_name | year_created | street_address | city | state | zip
--------------+----------------+--------------+----------------+------------+-------+------
6 | Jackson & Co | 2010 | lincoln Road | Buffalo | NY | 4331
2 | Nicon & Co | 2010 | Bush Road | Washington | NY | 4331
7 | GMC | 2011 | getsby Road | Fulton | NY | 4332
8 | Ford | 2012 | fisa Road | Elmira | NY | 4333
(4 rows)
warehouse_db=# drop materialized view mat_view ;
DROP MATERIALIZED VIEW