Week8 - Views
Week8 - Views
Objective
The objective of this lab is to get familiar with views in databases. After completing this
lab students will be able to create, view and update the views.
Introduction
View
● A view is a logical table based on a table or another view.
● A view contains no data of its own but is like a window through which data from
tables can be viewed or changed.
● The tables on which a view is based are called base tables.
● The view is stored as a SELECT statement in the data dictionary.
Advantages of Views
● Views restrict access to the data because the view can display selective columns
from the table.
● Views can be used to make simple queries to retrieve the results of complicated
queries. Views provide data independence for ad hoc users and application
programs.
● One view can be used to retrieve data from several tables.
● Views provide groups of users access to data according to their particular
criteria.
Creating a View
You can create a view by embedding a subquery within the CREATE VIEW statement.
Syntax
Keyword Description
The example creates a view that contains the employee number, last name, and salary
for each employee in department 80.
You can display the structure of the view by using the iSQL*Plus DESCRIBE command.
● You can control the column names by including column aliases within the
subquery.
Modifying a View
With the OR REPLACE option, a view can be created even if one exists with this name
already, thus replacing the old version of the view for its owner.
The following command modifies the EMPVU80 view by using CREATE OR REPLACE
VIEW clause.
Note:
● Add an alias for each column name.
● Column aliases in the CREATE VIEW clause are listed in the same order as the
columns in the subquery.
You can view the structure of the view by using the iSQL*Plus DESCRIBE command.
Display the contents of the view by issuing a SELECT statement.
SELECT *
FROM dept_sum_vu;
The WITH CHECK OPTION clause specifies that INSERTs and UPDATEs performed
through the view cannot create rows which the view cannot select, and therefore it
allows integrity constraints and data validation checks to be enforced on data being
inserted or updated.
If there is an attempt to perform DML operations on rows that the view has not selected,
an error is displayed, with the constraint name if that has been specified.
Any attempts to remove a row from a view with a read-only constraint results in an error.
Removing a View
You use the DROP VIEW statement to remove a view. The statement removes the view
definition from the database. Dropping views has no effect on the tables on which the
view was based. Views or other applications based on deleted views become invalid.
Only the creator or a user with the DROP ANY VIEW privilege can remove a view.
Syntax:
-- PRACTICE --
3. Select the view name and text from the USER_VIEWS data dictionary view.
4. Using your EMPLOYEES_VU view, enter a query to display all employee names
and department numbers.
5. Create a view named DEPT50 that contains the employee numbers, employee
last names, and department numbers for all employees in department 50. Label
the view columns EMPNO, EMPLOYEE, and DEPTNO. Do not allow an
employee to be reassigned to another department through the view using WITH
CHECK OPTION.