PostgreSQL Tutorial: Views

It is no different from MySQL. It encapsulates some complex operations and can also hide some sensitive data.

For users, a view is a real table, and the information of one or more tables can be directly queried based on the view.

For development, a view is just a SQL statement.

image.png

In PGSQL, simple (single table) views allow write operations.

However, writing to views is strongly not recommended, although PGSQL allows it by default (simple views).

When writing, what is actually modified is the table itself.

-- 构建一个简单视图
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;

Multiple table view

-- 复杂视图(两张表关联)
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