07-PGView
PGView
将一些复杂的查询操作封装起来,同时隐藏敏感的信息。但是不推荐视图进行写操作。
简而言之,视图就是一些sql语句。
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 ]
构建简单的视图
postgres=# create view vw_score as (select student_id from student);
CREATE VIEW
使用视图
postgres=# select * from vw_score;
student_id
------------
2
3
(2 rows)
对于单表简单视图可以修改,同时修改的就是原表。但如果是多表联查,就不可修改。
update vw_score set id = 5 where student_id = 2;
update vw_student_score set id = 5 where student_id = 2;
ERROR:错误:无法更新视图"ww student score"
DETAIL: 不来自单表或单视图的视图不能自动更新...

浙公网安备 33010602011771号