Database Reference
In-Depth Information
Note that the number of columns returned in the result depends on the number of columns
in the view, not on the number of columns in the underlying table. In this example, the SELECT
clause produces just two columns because CustomerNameView itself has just two columns.
Also notice that the columns LastName and FirstName in the CUSTOMER table have
been renamed to CustomerLastName and CustomerFirstName in the view. Because of this, the
ORDER BY phrase in the SELECT statement uses CustomerLastName and CustomerFirstName,
not LastName and FirstName. Also, the DBMS uses the labels CustomerLastName and
CustomerFirstName when producing results.
By ThE WAy If you need to change an SQL view after you have created it, use the
SQL ALTER VIEW statement. For example, if you wanted to reverse the
order of LastName and FirstName in the CustomerNameView, you would use the SQL
statement:
/* *** EXAMPLE CODE - DO NOT RUN *** */
/* *** SQL-ALTER-VIEW-CH07-01 *** */
ALTER VIEW CustomerNameView AS
SELECT
FirstName AS CustomerFirstName,
LastName AS CustomerLastName,
FROM
CUSTOMER;
If you using Oracle Database 11 g Release 2 or MySQL 5.6, you can also use the SQL
CREATE OR REPLACE VIEW syntax in place of the SQL CREATE VIEW syntax. This
allows you to modify the stored view without using the SQL ALTER VIEW syntax.
Figure 7-17 lists the uses for SQL views. SQL views can hide columns or rows. They also
can be used to display the results of computed columns, to hide complicated SQL syntax,
and to layer the use of built-in functions to create results that are not possible with a single
SQL statement. Additionally, SQL views can provide an alias for table names and thus hide
the true table names from applications and users. SQL views also are used to assign different
processing permissions and different triggers to different views of the same table. We will show
examples for each of these.
Using SQL Views to hide Columns and Rows
SQL views can be used to hide columns to simplify results or to prevent the display of sensi-
tive data. For example, suppose the users at the View Ridge Gallery want a simplified list of
Figure 7-17
Uses of SQL Views
Uses of SQL Views
Hide columns or rows.
Display results of computations.
Hide complicated SQL syntax.
Layer built-in functions.
Provide level of isolation between table data and users' view of data.
Assign different processing permissions to different views of the same table.
Assign different triggers to different views of the same table.
 
Search WWH ::




Custom Search