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

最新文章

  1. UOJ #149 [NOIP 2015] 子串
  2. stty命令使用
  3. vijos p1002 dp ***
  4. TT付款方式、前TT和后TT、LC信用证+TT付款方式
  5. 运维人愿意听到的话 vs 不愿意听到的话
  6. 对CLR异常和状态管理的一点理解
  7. [置顶] Java启动命令大观
  8. Install and configure sql server 2008 express
  9. WPF/MVVM 快速开发
  10. delphi cmd(4个例子都是通过管道取得)
  11. VBoxGuestAdditions.iso下载地址
  12. MVC — 初步理解IIS工作流程
  13. MJRefresh 使用注意点
  14. vue jquery js 获取当前时间本周的第一天 和 本月的第一天
  15. CLOUD配置审批流发消息
  16. Nancy 寄宿OWin
  17. 编译opencv python
  18. SpringBoot阿里巴巴Fastjson的一些常用配置
  19. 发布网站的时候发现360极速浏览器ie7内核不兼容样式的问题
  20. HashMap集合特点

热门文章

  1. Bootstrap页面布局1 - 下载BS(bootstrap简称)
  2. sql语句 当前时间查找重复 时间戳转换
  3. mysqli_query($link,'SET group_concat_max_len=8192');
  4. Spring配置文件的读取
  5. C++内嵌汇编代码,简单文件加密
  6. 1058 N的阶乘的长度
  7. PMP--可能会涉及到的计算题
  8. Essential Documents to Manage Your Projects
  9. json和string 之间的相互转换
  10. LED的压降