PostgreSQL教程:视图

跟MySQL的没啥区别,把一些复杂的操作封装起来,还可以隐藏一些敏感数据。

视图对于用户来说,就是一张真实的表,可以直接基于视图查询一张或者多张表的信息。

视图对于开发来说,就是一条SQL语句。

image.png

在PGSQL中,简单(单表)的视图是允许写操作的。

但是强烈不推荐对视图进行写操作,虽然PGSQL默认允许(简单的视图)。

写入的时候,其实修改的是表本身

-- 构建一个简单视图
create view vw_score as 
(select id,math_score from score);

select * from vw_score;
update vw_score set math_score = 99 where id = 2;

多表视图

-- 复杂视图(两张表关联)
create view vw_student_score as 
(select stu.id as id ,stu.name as name ,score.math_score from student stu,score score where stu.id = score.student_id);

select * from vw_student_score;

update vw_student_score set math_score =999 where id = 2;

image.png

猜你喜欢

转载自blog.csdn.net/a772304419/article/details/132928544