postgresql-view

View overview

A view is essentially a query statement stored in the database. Views themselves do not contain data and are also called
virtual tables. We give the view a name when we create it, and then we can query it like a table
Insert image description here

Benefits of using views

Insert image description here

Create view

PostgreSQL uses the CREATE VIEW statement to create a view:

CREATE VIEW view_name AS query;

Among them, view_name is the name of the view; AS is followed by the query statement of the view, which can be a simple query or a complex
query. The following statement creates a view containing employee details:

create view emp_details_view
as select
 e.employee_id,
 e.job_id,
 e.manager_id,
 e.department_id,
 d.location_id,
 e.first_name,
 e.last_name,
 e.salary,
 e.commission_pct,
 d.department_name,
 j.job_title
from employees e
join departments d on (e.department_id = d.department_id)
join jobs j on (j.job_id = e.job_id);
-- 使用视图查询数据
select * from emp_details_view
where department_name = 'IT';

Insert image description here

Modify view

If you need to modify the query in the view definition, you can use the CREATE OR REPLACE statement:

CREATE OR REPLACE VIEW 视图名称
AS
查询语句;
--PostgreSQL 目前只支持追加视图定义中的字段,不支持减少字段或者修改字段的名称或顺
--序。例如,我们可以为视图 emp_details_view 增加一个字段 hire_date:
create or replace view emp_details_view
as select
 e.employee_id,
 e.job_id,
 e.manager_id,
 e.department_id,
 d.location_id,
 e.first_name,
 e.last_name,
 e.salary,
 e.commission_pct,
 d.department_name,
 j.job_title,
 e.hire_date
from employees e
join departments d on (e.department_id = d.department_id)
join jobs j on (j.job_id = e.job_id);
--另外,PostgreSQL 还提供了 ALTER VIEW 语句修改视图的属性。例如以下语句用于修改视图的名称
--该语句将视图 emp_details_view 重命名为 emp_info_view。
ALTER VIEW emp_details_view RENAME TO emp_info_view;

The ALTER VIEW statement also provides other modification functions, such as setting the default value of a field, modifying the mode to which the view belongs,
etc. For details, please refer to the official documentation.

Delete view

Use the DROP VIEW statement to delete an existing view:

DROP VIEW [ IF EXISTS ] name [ CASCADE | RESTRICT ];

Among them, IF EXISTS can avoid errors when deleting a non-existent view; CASCADE means cascading deletion
of objects that depend on the view; RESTRICT means prompting an error message if dependent objects exist, which is the default value

recursive view

--视图的定义中也可以使用 recursive来创建递归视图
-- column_names:字段名称
create recursive view 视图名 (column_names) as 查询语句;
-- 递归视图需要指定字段的名称 column_names。以上语句实际上等价于以下sql
CREATE VIEW view_name AS
WITH RECURSIVE cte_name (column_names) AS (query)
SELECT column_names FROM cte_name;
-- 递归视图的创建
CREATE RECURSIVE VIEW employee_path(employee_id, employee_name, path) AS
 SELECT employee_id, CONCAT(first_name, ',', last_name), CONCAT(first_name,
',', last_name) AS path
 FROM employees
 WHERE manager_id IS NULL
 UNION ALL
 SELECT e.employee_id, CONCAT(e.first_name, ',', e.last_name),
CONCAT(ep.path, '->', e.first_name, ',', e.last_name)
 FROM employee_path ep
 JOIN employees e ON ep.employee_id = e.manager_id;
-- 查询视图
select * from employee_path ep ;

Insert image description here

Updateable view

If a view meets the following conditions:
• The FROM clause of the view definition only contains 一个表or can update views;
• The top-level query statement of the view definition does not contain the following clauses: GROUP BY, HAVING, LIMIT, OFFSET,
DISTINCT, WITH, UNION, INTERSECT and EXCEPT;
• The SELECT list does not contain window functions, set functions or aggregate functions (such as SUM, COUNT, AVG,
etc.).
Then the view is called an updateable view (updatable view), which means that we can execute INSERT,
UPDATE, and DELETE statements on it, and PostgreSQL will convert these operations into operations on the underlying table.

-- 创建视图
create view employees_it as
select employee_id,
 first_name,
 last_name,
 email,
 phone_number,
 hire_date,
 job_id,
 manager_id,
 department_id
from employees
where department_id = 60;
-- 查询视图信息
select employee_id,first_name, last_name from employees_it;

Insert image description here

-- 通过视图 employees_it 为 employees 表增加一个员工:
insert into employees_it
(employee_id, first_name, last_name, email, phone_number, hire_date, job_id,
manager_id, department_id)
VALUES(209, 'Tony', 'Dong', 'DONG', '590.423.5568', '2020-05-06', 'IT_PROG',
103, 60);

select * from employees_it;

Insert image description here
Insert image description here

WITH CHECK OPTION

To prevent inserting or modifying data via a view that is not visible to the view, you can use WITH CHECK OPTIONthe option:

create or replace view employees_it as
select employee_id,
 first_name,
 last_name,
 email,
 phone_number,
 hire_date,
 job_id,
 manager_id,
 department_id
from employees
where department_id = 60
with check option;

Insert image description here
The execution result shows that the check option is violated and data cannot be inserted.
WITH CASCADED CHECK OPTIONOptions perform cascading checks on the view and other views it depends on;
WITH LOCAL CHECK OPTIONoptions check only on the current view. Default is CASCADED.

Guess you like

Origin blog.csdn.net/Java_Fly1/article/details/132866874