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

PostgreSQL rule view materialized view examples的更多相关文章

  1. PostgreSQL物化视图(materialized view)

    1.创建视图 CREATE MATERIALIZED VIEW [ IF NOT EXISTS ] table_name [ (column_name [, ...] ) ] [ WITH ( sto ...

  2. Postgresql - MATERIALIZED VIEW

    MATERIALIZED VIEWPG 9.3 版本之后开始支持物化视图.View 视图:虚拟,不存在实际的数据,在查询视图的时候其实是对视图内的表进行查询操作. 物化视图:实际存在,将数据存成一张表 ...

  3. [terry笔记]物化视图 materialized view基础学习

    一.物化视图定义摘录:     物化视图是包括一个查询结果的数据库对像(由系统实现定期刷新数据),物化视图不是在使用时才读取,而是预先计算并保存表连接或聚集等耗时较多的操作结果,这样在查询时大大提高了 ...

  4. MATERIALIZED VIEW

    Oracle的实体化视图提供了强大的功能,可以用在不同的环境中,实体化视图和表一样可以直接进行查询.实体化视图可以基于分区表,实体化视图本身也可以分区. 主要用于预先计算并保存表连接或聚集等耗时较多的 ...

  5. Advanced Replication同步复制实验(基于Trigger&基于Materialized View)

    1. 高级复制和流复制介绍 1.1 高级复制(Advanced Replication) 高级复制也称为对称复制,分为多主体站点复制(Multiple Master Rplication).物化视图站 ...

  6. Materialized View in Oracle - Concepts and Architecture

    List all of MV inoracle: select owner, query, query_len from dba_mviews See content of aMV: select * ...

  7. 物化视图(materialized view) 实现数据迁移、数据定时同步

    近日公司有一个9i 的Oracle数据库,运行效率低下.想要将其升级到11G. 但是升级之前 要将数据进行同步,好在表不是很多.只有三张表.业务压力也不大,就想到了使用物 化视图的方式将数据同步过来. ...

  8. Materialized View模式

    Materialized-View模式是在要求数据格式不利于查询操作的情况下,根据多个数据仓库的数据生成预生成的视图的一种模式.这种模式可以帮助支持高效的查询和数据提取,提高应用程序的性能. 问题 在 ...

  9. ora-904 rowid create materialized view

    create materialized view t_v asselect t1.*,1 as marker,rowid from t1 t1union allselect t2.*,2 as mar ...

随机推荐

  1. Bootstrap页面布局12 - BS表单元素的排版

    首先看看这行代码: <label for='account'>帐 号</label> <input id='account' name='account' type='t ...

  2. 搭建C语言开发环境

    大学的时候有数据结构这门课,但...终究还是得学.电脑是win8的,根据网上的教程倒是能安装成功vc6.0并且能够打开新建工程,但是一编译运行就提示兼容性问题. 首先安装C语言编译器.下载MinGw ...

  3. jquery autocomplete插件结合ajax使用

    <%@ page isELIgnored="false"%> <%@ page contentType="text/html; charset=UTF- ...

  4. myeclipse 8.5 常用快捷键【转】

    eclipse 里查找行号的方法 今天开发的时候为了方便查找报错行的位置,特意在网上找了一下快捷键是什么,现做记录. 在Eclipse里的show the line number 后,使用" ...

  5. Day09

    Servlet概述 生命周期方法: void init(ServletConfig):出生之后(1次): void service(ServletRequest request, ServletRes ...

  6. Python基本数据类型之list列表

    列表是python中用的非常频繁的数据结构,它是有序序列.之前学的字符串就是一种有序序列.不过列表是可变的. 创建列表 li = list( )                          #构 ...

  7. win环境下,用虚拟化工具打包Qt动态编译exe的过程(使用Enigma Virtual Box)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://goldlion.blog.51cto.com/4127613/834075 引子 ...

  8. Cocos2d-JS引入其他场景小实例

    创建新项目,目标是把LogoNode.js场景引入app.js 新建LogoNode.js var LogoLayer = cc.Layer.extend({ ctor:function () { t ...

  9. There has been an error processing your request magento

    如果使用magento的过程中,出现以下页面: 说明出现了错误,但是亲,不用紧张,请根据"Error record number:xxxxxxxxx"的数字在网站根目录下的var/ ...

  10. Android调用WCF

    http://www.cnblogs.com/davidgu/archive/2012/05/16/2504182.html