View
View
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.
•• 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.
•• 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;
Select
Select a.ename,a.sal,a.deptno,
a.ename,a.sal,a.deptno,
b.max_sal
b.max_sal from
from emp
emp a,
a, (select
(select deptno,max(sal)
deptno,max(sal) max_sal
max_sal from
from emp
emp
Group
Group by
by deptno)
deptno) bb
Where
Where a.deptno=b.deptno
a.deptno=b.deptno
and
and a.sal<b.max_sal;
a.sal<b.max_sal;
select
select e.deptno,dname,loc
e.deptno,dname,loc
from
from emp
emp e,(select
e,(select deptno,dname,loc
deptno,dname,loc from
from dept)
dept) dd
where
where e.deptno=d.deptno;
e.deptno=d.deptno;
Finding
Finding department
department details
details of
of highest
highest paid
paid
employee:
employee:
select
select ename,e.deptno,dname,empno,sal
ename,e.deptno,dname,empno,sal
from
from dept
dept e,(select
e,(select
ename,empno,sal,deptno
ename,empno,sal,deptno from
from emp
emp where
where
sal=(select
sal=(select max(sal)
max(sal) max_sal
max_sal from
from emp
emp
where
where e.deptno=d.deptno
e.deptno=d.deptno
The
The high-level
high-level structure
structure of
of aa Top-N
Top-N
analysis
analysis query
query is:
is:
SQL>
SQL> SELECT
SELECT [column_list],
[column_list], ROWNUM
ROWNUM
22 FROM
FROM (SELECT
(SELECT [column_list]
[column_list] FROM
FROM
table
table
33 BY
BY Top-N_column)
Top-N_column)
44 WHERE
WHERE ORDER
ORDER ROWNUM
ROWNUM <=
<= N
N
12-23 Copyright Oracle Corporation, 1998. All rights reserved.
Example of Top-N Analysis
select
select rownum
rownum as as Rank,ename,sal
Rank,ename,sal
from
from (select
(select ename,sal
ename,sal from
from emp
emp order
order by
by sal
sal
desc)
desc)
where
where rownum<=3;
rownum<=3;
select
select rownum,ename,sal
rownum,ename,sal from from emp
emp order
order by
by sal
sal
desc;
desc;
The
The difference
difference between
between the
the above
above 22 examples
examples are
are
rownum
rownum is is obtained
obtained after
after and
and before
before the
the order
order by
by
clause
clause ..
To
To view
view every
every nth
nth rows
rows
Select
Select ** from
from (select
(select rownum
rownum
rn,empno,ename
rn,empno,ename fromfrom emp)
emp)
Where
Where mod(rn,4)=0;
mod(rn,4)=0;
•• 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