Like tables, views have CREATE and DROP commands. (DROP VIEW applies only to the view, not to the underlying base table.) You access the data with SELECT statements.
Here's the simplified syntax of a view definition statement:
CREATE VIEW view_name [(column_name [, column_name]...)] AS SELECT_statement
Follow your system's rules for identifiers when you name the view.
Specify view column names if the column is calculated or if multiple columns have the same name. Otherwise, columns inherit names from the SELECT clause.
Define the columns and rows of the view in the SELECT statement.
The following example creates a view that displays the names of authors who live in Oakland, California, and their books. The view is named oaklanders. Its SELECT statement pulls data from three tables.
create view oaklanders (FirstName, LastName, Book) as select au_fname, au_lname, title from authors, titles, titleauthors where authors.au_id = titleauthors.au_id and titles.title_id = titleauthors.title_id and city = 'Oakland'
As you've seen, the SELECT statement doesn't need to be a simple selection of the rows and columns of one particular table. You can create a view using any of these objects or combinations of objects:
- A single table
- More than one table
- Another view
- Multiple views
- Combinations of views and tables
The SELECT statement can have almost any complexity, using projection and selection to define the columns and rows you want to include and including GROUP BY and HAVING clauses. In multiple-object views, you can use joins, subqueries, or combinations of the two to construct connections in the tables and views underlying the view.
In spite of the many kinds of views that can be created, there are always limits, varying from SQL to SQL. Basically, restrictions have two sources:
Elements not allowed in CREATE VIEW statements (ORDER BY and sometimes UNION). Check your system manuals for specifics.
Elements permitted in CREATE VIEW statements (computed columns, aggregates) that can limit the data modification permitted through the view because of the problem of interpreting data modification statements.
The view-updating problem is explained and illustrated later in this chapter. If you want users to be able to perform most functions through a view, you may decide to modify a perfectly legal CREATE VIEW statement to avoid these limitations.
Displaying Data Through Views
Creating a view doesn't produce a display. When SQL receives a CREATE VIEW command, it does not actually execute the SELECT statement that follows the keyword AS. Instead, it stores the SELECT statement in the system catalogs.
In order to see data through the view, query the view, just as you would a table.
select * from oaklanders FirstName LastName Book ========= ========== =============================================== Marjorie Green The Busy Executive's Database Guide Stearns MacFeather Cooking with Computers: Surreptitious Balance Sheets Marjorie Green You Can Combat Computer Stress! Dick Straight Straight Talk About Computers Livia Karsen Computer Phobic and Non-Phobic Individuals: Behavior Variations Stearns MacFeather Computer Phobic and Non-Phobic Individuals: [6 rows] Behavior Variations
A view in a SELECT statement works just like a table. You can
- Change the order of the columns in the display;
- Add display labels;
- Limit the rows with WHERE conditions;
- Group rows;
- Limit group results with HAVING;
- Sort the results.
For example, you could write a query of oaklanders like this, changing the position of columns, adding display labels, and sorting the results:
select Book as Title, LastName as Surname from oaklanders order by Title Title Surname ========================================================== ========== Computer Phobic and Non-Phobic Individuals: Behavior Variations Karsen Computer Phobic and Non-Phobic Individuals: Behavior Variations MacFeather Cooking with Computers: Surreptitious Balance Sheets MacFeather Straight Talk About Computers Straight The Busy Executive's Database Guide Green You Can Combat Computer Stress! Green [6 rows]
Here's a query of the oaklanders view with aggregates, GROUP BY, and HAVING:
select LastName, count(Book) from oaklanders group by LastName having count(Book) > 1 LastName count(titles.title) ======================================== ==================== Green 2 MacFeather 2 [2 rows]
The command for removing views is:
DROP VIEW view_name
If a view depends on a table (or on another view) that has been dropped, you won't be able to use the view. However, if you create a new table (or view) with the same name to replace the dropped one, you may be able to use the view again, as long as the columns referenced in the view definition still exist. Check your system's reference manuals for details.