Documentation
 
 
 

2.2. Views

Refer back to the queries in Section 1.6. Suppose the annual salary of all the employees in department 20 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 emp_view AS
    SELECT ename, sal*12 annual_salary
    FROM emp
    WHERE deptno = 20;

SELECT * FROM emp_view;

 ename | annual_salary
-------+---------------
 SMITH |       9600.00
 JONES |      35700.00
 SCOTT |      36000.00
 ADAMS |      13200.00
 FORD  |      36000.00
(5 rows)

Making liberal use of views is a key aspect of good SQL database design. Views provide a consistent interface that encapsulate details of the structure of your tables which may change as your application evolves.

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

 
 ©2004-2007 EnterpriseDB All Rights Reserved