Professional Documents
Culture Documents
Creating Views
Object Description
EMPNO 7876
7499
7499 ADAMS
ENAME
7876 ALLEN
ALLEN SALESMAN
ADAMS CLERK
JOB
SALESMAN
CLERK 7788
7698
7698 12-JAN-83
7788 20-FEB-81
12-JAN-83
20-FEB-81 1600
1100
1100 300
1600 300 30
20
30
20
------ 7369
--------
7369
7844 SMITH
TURNER
7844 SMITH -----------
SALESMAN
CLERK
TURNER SALESMAN
CLERK 7902
7698
7902 17-DEC-80
08-SEP-81
7698 17-DEC-80
08-SEP-81 1500
800
1500
800 00 30
20
30
20
7902
7900
7900 FORD
JAMES
JAMES CLERK
ANALYST
ANALYST 7566
7698
7698 03-DEC-81 3000
950 30
20
7839 7698
KING
7902
7521
FORD
PRESIDENT
CLERK 7566 03-DEC-81 3000
950 30
20
7521 BLAKE
7698 WARD
BLAKE
WARD SALESMAN
MANAGER
MANAGER 7839
SALESMAN 7698
7698 01-MAY-81
7839 22-FEB-81
01-MAY-81
22-FEB-81 1250
2850
2850 500
1250 500 30
30
30
30
7782 7654
CLARK
7902
7902 MARTIN
7654 FORD
MARTIN
FORD MANAGER
ANALYST
SALESMAN
ANALYST
SALESMAN 7698
7566
7566 28-SEP-81
7698 03-DEC-81
28-SEP-81
03-DEC-81 3000
1250
1250 1400
3000 1400 20
30
20
30
7934 7499
MILLER
7499
7369 ALLEN
SMITH
7369 ALLEN
SMITH CLERK7698
CLERK
SALESMAN
CLERK
SALESMAN 7698
7902 20-FEB-81
17-DEC-80
7902 20-FEB-81
17-DEC-80 1600
800
1600
800 300
300 20
30
20
30
7844
7788
7788 TURNER
7844 SCOTT
SCOTT ANALYST
TURNER SALESMAN
SALESMAN 7698
ANALYST 7566
7566 08-SEP-81
7698 09-DEC-82
09-DEC-82 3000
08-SEP-81 1500
3000
1500 00 20
30
20
30
7900
7876
7876 JAMES
7900 ADAMS
ADAMS CLERK
JAMES CLERK
CLERK
CLERK 7698
7788
7788 03-DEC-81
7698 12-JAN-83
12-JAN-83 1100
03-DEC-81 950
1100
950 20
30
20
30
7521
7934
7934 WARD
7521 MILLER
MILLER CLERK
WARD SALESMAN
SALESMAN 7698
CLERK 7782
7782 22-FEB-81
7698 23-JAN-82
23-JAN-82 1300
22-FEB-81 1250
1300
1250 500
500 10
30
10
30
•• To
To restrict
restrict database
database access
access
•• To
To make
make complex
complex queries
queries easy
easy
•• To
To allow
allow data
data independence
independence
•• To
To present
present different
different views
views of
of the
the same
same
data
data
•• The
The subquery
subquery can
can contain
contain complex
complex
SELECT
SELECT syntax.
syntax.
•• The
The subquery
subquery cannot
cannot contain
contain an
an ORDER
ORDER
BY
BY clause.
clause.
•• Describe
Describe the
the structure
structure of
of the
the view
view by
by
using
using the
the SQL*Plus
SQL*Plus DESCRIBE
DESCRIBE
command.
command.
SQL>
SQL> DESCRIBE
DESCRIBE empvu10
empvu10
•• Select
Select the
the columns
columns from
from this
this view
view by
by
the
the given
given alias
alias names.
names.
SQL> SELECT *
2 FROM salvu30;
EMPLOYEE_NUMBER
EMPLOYEE_NUMBER NAME
NAME SALARY
SALARY
---------------
--------------- ----------
---------- ---------
---------
7698
7698 BLAKE
BLAKE 2850
2850
7654
7654 MARTIN
MARTIN 1250
1250
7499
7499 ALLEN
ALLEN 1600
1600
7844
7844 TURNER
TURNER 1500
1500
7900
7900 JAMES
JAMES 950
950
7521
7521 WARD
WARD 1250
1250
66 rows
rows selected.
selected.
SQL*Plus
USER_VIEWS
USER_VIEWS
SELECT *
EMPVU10
EMPVU10
FROM empvu10;
SELECT
SELECT empno,
empno, ename,
ename, job
job
FROM
FROM emp
emp
WHERE
WHERE deptno
deptno == 10;
10;
7839 KING PRESIDENT
7782 CLARK MANAGER EMP
7934 MILLER CLERK
•• Column
Column aliases
aliases inin the
the CREATE
CREATE VIEW
VIEW
clause
clause are
are listed
listed in
in the
the same
same order
order as
as the
the
columns
columns in
in the
the subquery.
subquery.
The example on the slide creates a complex view of the department names, minimum salary,
maximum salary, and average salary by the department. Note that alternative names have been
specified for the view. This is a requirement if any column of the view is derived from a function
or an expression.
12-13 Copyright Oracle Corporation, 1998. All rights reserved.
Rules for Performing
DML Operations on a View
•• You
You can
can perform
perform DML
DML operations
operations on
on
simple
simple views.
views.
•• You
You cannot
cannot remove
remove aa row
row if
if the
the view
view
contains
contains the
the following:
following:
–– Group
Group functions
functions
–– A
A GROUP
GROUP BY
BY clause
clause
–– The
The DISTINCT
DISTINCT keyword
keyword
•• Any
Any attempt
attempt to to change
change the
the department
department
number
number for
for any
any row
row in
in the
the view
view will
will fail
fail
because
because itit violates
violates the
the WITH
WITH CHECK
CHECK OPTION
OPTION
constraint.
constraint.
12-16 Copyright Oracle Corporation, 1998. All rights reserved.
Denying DML Operations
•• You
You can
can ensure
ensure that
that no
no DML
DML operations
operations
occur
occur by
by adding
adding the
the WITH
WITH READ
READ ONLY
ONLY
option
option to
to your
your view
view definition.
definition.
SQL> CREATE OR REPLACE VIEW empvu10
2 (employee_number, employee_name, job_title)
3 AS SELECT empno, ename, job
4 FROM emp
5 WHERE deptno = 10
6 WITH READ ONLY;
View created.
•• Any
Any attempt
attempt toto perform
perform aa DML
DML on
on any
any
row
row in
in the
the view
view will
will result
result in
in Oracle
Oracle
Server
Server error.
error.
12-17 Copyright Oracle Corporation, 1998. All rights reserved.
Removing a View
Remove
Remove aa view
view without
without losing
losing data
data
because
because aa view
view is
is based
based on
on underlying
underlying
tables
tables in
in the
the database.
database.
DROP
DROP VIEW
VIEW view;
view;
•• Creating
Creating aa simple
simple view
view
•• Creating
Creating aa complex
complex view
view
•• Creating
Creating aa view
view with
with aa check
check constraint
constraint
•• Attempting
Attempting toto modify
modify data
data in
in the
the view
view
•• Displaying
Displaying view
view definitions
definitions
•• Removing
Removing views
views