Sunteți pe pagina 1din 18

View

View
 A view is a virtual table – no data is actually
maintained for a view
 A view is defined based on existing tables by
using a SELECT statement:
CREATE VIEW viewname
AS SELECT …
 When a view is referenced the view's defining
query is executed and data is retrieved from
tables referenced by the defining query
 The defining query for a view is stored in the
data dictionary
Advantages of Views
To restrict To make complex
data access queries easy

To provide To present
data different views
independence of the same data
View Example
CREATE VIEW management
AS SELECT Emp_no, ename
FROM employees
WHERE emp_no IN
(SELECT mgr
FROM employees
WHERE mgr IS NOT NULL);
 Using a view in a query causes the SELECT
statement that defines the view to be executed
and data values retrieved
eg SELECT ename
FROM management;
View Example 2
 Views are often used to simplify writing of
complex queries which involve multiple tables
or group functions
Eg: CREATE VIEW deptsummary
AS SELECT d.deptno, dept_name, sum(sal) Salaries
FROM departments d, employees e
WHERE d.dept_no = e.dept_no(+)
GROUP BY d.dept_no, dept_name;
SELECT * FROM deptsummary
WHERE Salaries > 5000;
View Example 3
 Views are also used to permit access to only
certain rows and/or columns in a table to some
user(s) and not permit access to the remaining
columns and/or rows
Eg: CREATE VIEW emplist
AS SELECT emp_no, ename, fname, job
FROM employees
WHERE LOWER(job)!='president'
ORDER BY ename, fname;
SELECT * FROM emplist;
Simple and Complex Views
 A simple view is based upon a query that
involves only a single table and uses no
grouping; any other view is referred to a
complex view
 Data can be modified using INSERT, UPDATE
and DELETE statements applied to a simple
view but not to a complex view
eg: UPDATE emplist
SET job = 'Assistant'
WHERE job = 'Clerk';
Simple Views and Complex
Views
Feature Simple Views Complex Views
Number of tables One One or more
Contain functions No Yes
Contain groups of data No Yes
DML operations Yes Not always
through a view
Creating a View
– You embed a subquery in the CREATE
VIEW
CREATE [ORstatement:
REPLACE] [FORCE|NOFORCE] VIEW view
[(alias[, alias]...)]
AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY [CONSTRAINT constraint]];

– The subquery can contain complex


SELECT syntax.
Creating a View
– Create the EMPVU80 view, which
contains details of employees in
CREATE VIEW
department empvu80
80:
AS SELECT employee_id, last_name, salary
FROM employees
WHERE department_id = 80;
View created.

DESCRIBE empvu80
– Describe the structure of the view by
using the iSQL*Plus DESCRIBE
command:
Creating a View
– Create a view by using column aliases in
the subquery:
CREATE VIEW salvu50
AS SELECT employee_id ID_NUMBER,
last_name NAME,
salary*12 ANN_SALARY
FROM employees
WHERE department_id = 50;
View created.
– Select the columns from this view by the
given alias names:
Retrieving Data from a View
SELECT *
FROM salvu50;
Modifying a View
– Modify the EMPVU80 view by using a
CREATE OR REPLACE VIEW clause.
Add an alias for each column name:
CREATE OR REPLACE VIEW empvu80
(id_number, name, sal, department_id)
AS SELECT employee_id, first_name || ' '
|| last_name, salary, department_i
FROM employees
WHERE department_id = 80;
View created.
– Column aliases in the CREATE OR
REPLACE VIEW clause are listed in the
same order as the columns in the
subquery.
Change View
 Change definition of a view by using the OR
REPLACE clause in the CREATE VIEW
statement
eg CREATE OR REPLACE VIEW emplist
AS SELECT emp_no, ename, fname, job
FROM employees
ORDER BY ename, fname;
Rules for Performing
DML Operations on a View
– You can usually perform DML
operations
on simple views.
– You cannot remove a row if the view
contains the following:
• Group functions
• A GROUP BY clause
• The DISTINCT keyword
• The pseudocolumn ROWNUM keyword
Rules for Performing
DML Operations on a View
 You cannot modify data in a view if it
contains:
– Group functions
– A GROUP BY clause
– The DISTINCT keyword
– The pseudocolumn ROWNUM keyword
– Columns defined by expressions
Rules for Performing
DML Operations on a View
 You cannot add data through a view if
the view includes:
– Group functions
– A GROUP BY clause
– The DISTINCT keyword
– The pseudocolumn ROWNUM keyword
– Columns defined by expressions
– NOT NULL columns in the base tables
that are not selected by the view
Removing a View
 You can remove a view without losing
data because a view is based on
underlying tables in the database.
DROP VIEW view;

DROP VIEW empvu80;


View dropped.

S-ar putea să vă placă și