Postgresql 简明教程

PostgreSQL - VIEWS

视图是伪表。也就是说,它们不是实际表;不过,它们对 SELECT 来说表现为普通表。视图可以表示实际表的一个子集,从普通表中选择某些列或某些行。视图甚至可以表示连接的表。由于视图被分配了单独的权限,所以您可以使用它们来限制表访问,以便用户只能看到表的特定行或列。

Views are pseudo-tables. That is, they are not real tables; nevertheless appear as ordinary tables to SELECT. A view can represent a subset of a real table, selecting certain columns or certain rows from an ordinary table. A view can even represent joined tables. Because views are assigned separate permissions, you can use them to restrict table access so that the users see only specific rows or columns of a table.

视图可以包含表的全部行或一个或多个表的选定行。视图可以从一个或多个表创建,这取决于用于创建视图的书面 PostgreSQL 查询。

A view can contain all rows of a table or selected rows from one or more tables. A view can be created from one or many tables, which depends on the written PostgreSQL query to create a view.

视图(它是一种虚拟表)允许用户执行以下操作 -

Views, which are kind of virtual tables, allow users to do the following −

  1. Structure data in a way that users or classes of users find natural or intuitive.

  2. Restrict access to the data such that a user can only see limited data instead of complete table.

  3. Summarize data from various tables, which can be used to generate reports.

由于视图不是普通表,因此您可能无法对视图执行 DELETE、INSERT 或 UPDATE 语句。但是,您可以创建一个 RULE 来纠正此问题(对视图使用 DELETE、INSERT 或 UPDATE)。

Since views are not ordinary tables, you may not be able to execute a DELETE, INSERT, or UPDATE statement on a view. However, you can create a RULE to correct this problem of using DELETE, INSERT or UPDATE on a view.

Creating Views

PostgreSQL 视图使用 CREATE VIEW 语句创建。PostgreSQL 视图可以从单表、多表或另一个视图创建。

The PostgreSQL views are created using the CREATE VIEW statement. The PostgreSQL views can be created from a single table, multiple tables, or another view.

基本的 CREATE VIEW 语法如下 -

The basic CREATE VIEW syntax is as follows −

CREATE [TEMP | TEMPORARY] VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE [condition];

您可以以与在普通 PostgreSQL SELECT 查询中使用的方式非常相似的方式在 SELECT 语句中包含多张表。如果存在可选的 TEMP 或 TEMPORARY 关键字,则将视图创建在临时空间中。临时视图会在当前会话结束时自动删除。

You can include multiple tables in your SELECT statement in very similar way as you use them in normal PostgreSQL SELECT query. If the optional TEMP or TEMPORARY keyword is present, the view will be created in the temporary space. Temporary views are automatically dropped at the end of the current session.

Example

考虑 COMPANY 表具有以下记录 -

Consider, the COMPANY table is having the following records −

 id | name  | age | address    | salary
----+-------+-----+------------+--------
  1 | Paul  |  32 | California |  20000
  2 | Allen |  25 | Texas      |  15000
  3 | Teddy |  23 | Norway     |  20000
  4 | Mark  |  25 | Rich-Mond  |  65000
  5 | David |  27 | Texas      |  85000
  6 | Kim   |  22 | South-Hall |  45000
  7 | James |  24 | Houston    |  10000

现在,以下是如何根据 COMPANY 表创建视图的示例。此视图将仅用于拥有 COMPANY 表中的几列 -

Now, following is an example to create a view from COMPANY table. This view would be used to have only few columns from COMPANY table −

testdb=# CREATE VIEW COMPANY_VIEW AS
SELECT ID, NAME, AGE
FROM  COMPANY;

现在,您可以以与查询实际表类似的方式查询 COMPANY_VIEW。以下是如何操作 -

Now, you can query COMPANY_VIEW in a similar way as you query an actual table. Following is the example −

testdb=# SELECT * FROM COMPANY_VIEW;

这将产生以下结果 -

This would produce the following result −

 id | name  | age
----+-------+-----
  1 | Paul  |  32
  2 | Allen |  25
  3 | Teddy |  23
  4 | Mark  |  25
  5 | David |  27
  6 | Kim   |  22
  7 | James |  24
(7 rows)

Dropping Views

要删除视图,只需使用带有 view_name 的 DROP VIEW 语句。基本的 DROP VIEW 语法如下 −

To drop a view, simply use the DROP VIEW statement with the view_name. The basic DROP VIEW syntax is as follows −

testdb=# DROP VIEW view_name;

以下命令将删除我们在最后一节中创建的 COMPANY_VIEW 视图 -

The following command will delete COMPANY_VIEW view, which we created in the last section −

testdb=# DROP VIEW COMPANY_VIEW;