Orcl12 SQL IM ch13

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Oracle 12c: SQL 13-1

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.

Students will need the privilege to create views.

Chapter Objectives
After completing this chapter, you should be able to do the following:

♦ Create a view, using the CREATE VIEW command or the CREATE OR


REPLACE VIEW command
♦ Know how to employ the FORCE and NO FORCE options
♦ State the purpose of the WITH CHECK OPTION
♦ Explain the effect of the WITH READ ONLY option
♦ Update records in a simple view
♦ Re-create a view
♦ Explain the implication of an expression in a view for DML operations
♦ Perform an update of a record in a complex view
♦ Identify problems associated with adding records to a complex view
♦ Identify the key-preserved table underlying a complex view
♦ Drop a view
♦ Explain inline views and the use of ROWNUM to perform a “Top-N” analysis
♦ Create a materialized view to replicate data

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

2. What is the purpose of the WITH CHECK OPTION?


ANSWER: Prevents DML operations that will make the data inaccessible after the
change

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

Creating a Complex View


A complex view is a view based on one or more underlying tables and may contain arithmetic
expressions, grouped data, or group functions. Depending on the structure of the view, certain
DML operations will not be allowed. When a complex view is based on more than one table, no
DML operations will be allowed on the non-key-preserved table.

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.

2. Why distinguish between simple and complex views?


ANSWER: There are restrictions on the DML operations that can be performed on
complex views.

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

5. What is a key-preserved table?


ANSWER: It is the underlying table containing the column(s) the view is using as its
primary key.

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

2. If a view is dropped, is the underlying table(s) dropped also?


ANSWER: No

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.

Creating an Inline View


Unlike simple and complex views, an inline view cannot be referenced at a later time. Instead, it
creates a temporary “table” that can only be referenced for the duration of the SELECT
statement in which it was created. Oracle 12c introduced the CROSS and OUTER APPLY
methods as additional options for performing join operations. These methods allow a column of
the joining table to be used to produce the result set of the inline view.

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.

Troubleshooting Tip Demonstrate that the ROWNUM is not changed if a table is


simply sorted. It is when the sorted data is passed to an outer query
that the ROWNUM will be in the necessary sequence to perform
Top-N analysis.

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

4. What command is used to remove an inline view from the database?


ANSWER: None, it is temporary and only exists during the execution of the SELECT
statement.

5. Why is an inline view required to perform “Top-N analysis”?


ANSWER: The ROWNUM must be re-sequenced, and an ORDER BY clause only is not
sufficient.

Creating a Materialized View


A materialized view allows you to store the data retrieved by the view query and lets you reuse
this data without executing the view query again. In other words, a materialized view allows the
replication of data. These are often referred to as snapshots, as they take a picture or capture a set
of data at a specific point in time.

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.

materialized view — Stores the data retrieved by the view query

non-key-preserved table — Does not uniquely identify the records in a view.

pseudotables — Created to present a particular “view” of a database’s contents. Does not


actually store data but is referenced like a table in SQL statements.
Oracle 12c: SQL 13-7

“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.

You might also like