Views

3.2. Views

Refer back to the queries in Section 2.6. Suppose the combined listing of weather records and city location is of particular interest to your application, but you do not want to type the query each time you need it. You can create a view over the query, which gives a name to the query that you can refer   to like an ordinary table:

CREATE VIEW myview AS

SELECT city, temp_lo, temp_hi, prcp, date, location

FROM weather, cities

WHERE city = name;

SELECT * FROM myview;

Making liberal use of views is a key aspect of good SQL database design. Views allow you to encapsulate the details of the structure of your tables, which might change as your application evolves,behind consistent interfaces.

 

Views can be used in almost any place a real table can be used. Building views upon other views is not uncommon.

 

3.2 视图

回到2.6节中的示例查询。假设你的应用程序中会经常用到天气信息和城市位置的组合列表,但你不希望每次查询的时候都重新敲那么一大段的代码。那么你可以基于查询创建一个视图。对于视图的查询可以像对表那样进行查询:

CREATE VIEW myview AS

SELECT city, temp_lo, temp_hi, prcp, date, location

FROM weather, cities

WHERE city = name;

SELECT * FROM myview;

灵活的使用视图是良好数据库设计的一个方面。视图可以使表结构对用户透明,这样可以很好的应对表结构变化对查询的影响。

 

视图几乎可以出现在表能够出现的任何地方。而且基于视图创建视图并不罕见。

你可能感兴趣的:(PostgreSQL,PostgreSQL,11.2,Documentation,Oracle,PostgreSQL,11.2,Documentation,Learning)