Professional Documents
Culture Documents
Orcl12 SQL IM ch13
Orcl12 SQL IM ch13
Orcl12 SQL IM ch13
Chapter 13
Views
At a Glance
Instructor’s Notes
♦ Chapter Overview
♦ Chapter Objectives
♦ Instructor Notes
♦ Troubleshooting Tips
♦ Quick Quizzes
♦ Discussion Questions
♦ Key Terms
Oracle 12c: SQL 13-2
Chapter Overview
Organizations use views as an added layer of security by restricting the data that users can
access. In addition, views can be used to simplify complex data navigation or searches for novice
users. A view is a pseudotable. Although it is a database object, it does not actually contain data.
The subquery used to generate the view is re-executed each time the view is referenced. The
view can be referenced in the FROM of a SELECT statement or in DML commands, just like a
table.
Chapter Objectives
After completing this chapter, you should be able to do the following:
Instructor Notes
Creating a View
A view can be created using the CREATE VIEW command. Because a view cannot be altered,
the CREATE OR REPLACE VIEW command is used to re-create an existing view. The
command also has the FORCE option available in the event the underlying table(s) is not
available when the view is created. A subquery is used to identify the underlying table(s) and
columns. The subquery is identified by the AS keyword. If the WITH CHECK OPTION is
specified, then modifications cannot be made to the data that would subsequently make it
inaccessible to the view. If the WITH READ ONLY option is included, no DML operations will
be permitted.
Oracle 12c: SQL 13-3
A simple view is based on only one table and cannot include any arithmetic expressions, grouped
data, or group functions. Any DML operation is permitted on a simple view unless it violates a
constraint on the view (WITH READ ONLY, WITH CHECK OPTION) or underlying table
(PRIMARY KEY, CHECK, etc).
Troubleshooting Tip Create a view and include the WITH CHECK OPTION. Then
demonstrate what occurs if the user attempts to violate the
constraint.
Quick Quiz
1. What clause cannot be included in the subquery that is used to create a simple view?
ANSWER: A GROUP BY clause
3. If you realized that you forgot to include a column in a view, how can you go back and
make the change?
ANSWER: That is not possible; the view will need to be replaced with a new view.
4. What DML operations are permitted on views created with the WITH READ ONLY
keywords?
ANSWER: No DML operations are permitted.
5. How can new names be assigned to the columns extracted from the underlying table,
without specifying a column list in the CREATE VIEW clause?
ANSWER: Assign column aliases in the subquery
Oracle 12c: SQL 13-4
Troubleshooting Tip Create a simple view and demonstrate basic DML operations. Re-
create the view and include group functions in the new view.
Demonstrate that certain DML options will not be allowed.
Quick Quiz
1. What distinguishes a complex view from a simple view?
ANSWER: A complex view may have more than one underlying table, grouped data, or
group functions.
3. Can rows be deleted through a complex view that contains grouped data?
ANSWER: No
4. Can rows be updated through a complex view that contains a group function?
ANSWER: No
Dropping a View
As with other database objects, a view is removed from a database using the DROP command.
Dropping a view does not affect the data contained in the underlying table.
Oracle 12c: SQL 13-5
Quick Quiz
1. What command is used to remove a view from the database?
ANSWER: DROP VIEW command
3. If a view is dropped, what happens to the data previously displayed by the view?
ANSWER: Nothing, the data will remain in the underlying tables.
An inline view is frequently used to perform “Top-N analysis.” Top-N analysis is performed by
including an ORDER BY clause in the subquery. Although an ORDER BY clause technically
cannot be used in a subquery, it is permitted in views. When the subquery is included in the
FROM clause of a SELECT statement, it is categorized as a view. The ROWNUM
pseudocolumn is then used to extract the desired rows. Oracle 12c introduced a new
row_limiting_clause which simplifies the TOP-N queries even further.
Quick Quiz
1. Why is an inline view different from a simple or complex view?
ANSWER: It is temporary and cannot be referenced at a later time.
2. When performing “Top-N analysis” to find the three highest values in a column, what is
the necessary sort sequence?
ANSWER: Descending
Oracle 12c: SQL 13-6
3. If you are looking for the three cheapest books in the BOOKS table, what order should
the data be sorted in to extract these books using “Top-N analysis”?
ANSWER: Ascending
Discussion Questions
1. Select an industry and describe how a view can be used to provide a degree of security.
2. Provide examples of some complex SQL statements that can be simplified through the
use of views.
Key Terms
inline view — A temporary view of underlying database tables that exists only while a command
is being executed. It is not a permanent database object and cannot be referenced again by a
subsequent query.
key-preserved table — A table that contains the primary key that a view uses to uniquely
identify each record displayed by the view.
“TOP-N” analysis — When an inline view and a pseudocolumn ROWNUM are merged
together to create a temporary list of records in a sorted order, and then the top “n,” or number of
records, are retrieved.
views — Display data in the underlying base tables. Views are used to provide a shortcut for
users not having SQL training or to restrict users’ access to sensitive data. Views are database
objects, but they do not store data.