You are on page 1of 20

12

Creating Views

Copyright  Oracle Corporation, 1998. All rights reserved.


Objectives
After
After completing
completing this
this lesson,
lesson, you
you should
should
be
be able
able to
to do
do the
the following:
following:
•• Describe
Describe aa view
view
•• Create
Create aa view
view
•• Retrieve
Retrieve data
data through
through aa view
view
•• Alter
Alter the
the definition
definition of
of aa view
view
•• Insert,
Insert, update,
update, and
and delete
delete data
data through
through
aa view
view
•• Drop
Drop aa view
view
12-2 Copyright  Oracle Corporation, 1998. All rights reserved.
Database Objects

Object Description

Table Basic unit of storage; composed of rows


and columns

View Logically represents subsets of data from


one or more tables

Sequence Generates primary key values

Index Improves the performance of some queries

Synonym Alternative name for an object

12-3 Copyright  Oracle Corporation, 1998. All rights reserved.


What Is a View?
EMP Table
EMPNO
EMPNO ENAME
ENAME JOBJOB
JOB
JOB MGR
MGR HIREDATE
HIREDATE SAL
SAL
SAL COMM
SAL COMM
COMM DEPTNO
COMM DEPTNO
DEPTNO
DEPTNO
-----
----- --------
-------
------- ---------
-------- ---------
--------------
--------- ----
---- ---------
----- --------- ------
-----
----- -----
------ -----
------------
----- -------
-------
-------
7839
7839
7839 KING
7839 KING PRESIDENT
PRESIDENT
PRESIDENT
PRESIDENT 17-NOV-81
17-NOV-81 5000
5000
5000
5000 10
10
10
10
7782
7698
7698 CLARK
7782 BLAKE
CLARK
BLAKE MANAGER
MANAGER
MANAGER 7839
MANAGER 7839 09-JUN-81
01-MAY-81
01-MAY-81 2850
09-JUN-81 1500
2850
1500 300
300 30
10
30
10
7934
7782
7934 MILLER
CLARK
7782 MILLER
CLARK MANAGER
CLERK
MANAGER
CLERK 7782
7839
7782 23-JAN-82
09-JUN-81
7839 23-JAN-82 2450
1300
09-JUN-81 2450
1300 10
10
10
10
7566
7566
7566 JONES
7566 JONES MANAGER
MANAGER
MANAGER 7839
MANAGER 7839 02-APR-81
02-APR-81 2975
2975
2975
2975 20
20
20
20
EMPVU10
7788
7654
7788 ViewSALESMAN
7654 SCOTT
MARTIN
SCOTT
MARTIN ANALYST
ANALYST 7566
SALESMAN 7698
7698 09-DEC-82
7566 28-SEP-81
09-DEC-82
28-SEP-81 1250
3000
3000 1400
1250 1400 30
20
30
20

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

12-4 Copyright  Oracle Corporation, 1998. All rights reserved.


Why Use Views?

•• 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

12-5 Copyright  Oracle Corporation, 1998. All rights reserved.


Simple Views
and Complex Views
Feature
Feature Simple
Simple Views
Views Complex
Complex Views
Views
Number
Number of
of tables
tables One
One One
One or
or more
more
Contain
Contain functions
functions No
No Yes
Yes
Contain
Contain groups
groups of
of data
data No
No Yes
Yes
DML
DML through
through view
view Yes
Yes Not
Not always
always

12-6 Copyright  Oracle Corporation, 1998. All rights reserved.


Creating a View
•• You
You embed
embed aa subquery
subquery within
within the
the
CREATE
CREATE VIEW
VIEW statement.
statement.
CREATE
CREATE [OR
[OR REPLACE]
REPLACE] [FORCE|NOFORCE]
[FORCE|NOFORCE] VIEW
VIEW view
view
[(alias[,
[(alias[, alias]...)]
alias]...)]
AS
AS subquery
subquery
[WITH
[WITH CHECK
CHECK OPTION
OPTION [CONSTRAINT
[CONSTRAINT constraint]]
constraint]]
[WITH
[WITH READ
READ ONLY]
ONLY]

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

12-7 Copyright  Oracle Corporation, 1998. All rights reserved.


Creating a View
•• Create
Create aa view,
view, EMPVU10,
EMPVU10, that
that contains
contains
details
details of
of employees
employees in
in department
department 10.
10.
SQL> CREATE VIEW empvu10
2 AS SELECT empno, ename, job
3 FROM emp
4 WHERE deptno = 10;
View created.

•• 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

12-8 Copyright  Oracle Corporation, 1998. All rights reserved.


Creating a View
•• Create
Create aa view
view by
by using
using column
column aliases
aliases
in
in the
the subquery.
subquery.
SQL> CREATE VIEW salvu30
2 AS SELECT empno EMPLOYEE_NUMBER, ename NAME,
3 sal SALARY
4 FROM emp
5 WHERE deptno = 30;
View created.

•• Select
Select the
the columns
columns from
from this
this view
view by
by
the
the given
given alias
alias names.
names.

12-9 Copyright  Oracle Corporation, 1998. All rights reserved.


Retrieving Data from a View

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.

12-10 Copyright  Oracle Corporation, 1998. All rights reserved.


Querying a View

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

12-11 Copyright  Oracle Corporation, 1998. All rights reserved.


Modifying a View
•• Modify
Modify the
the EMPVU10
EMPVU10 view
view by
by using
using
CREATE
CREATE OR OR REPLACE
REPLACE VIEW
VIEW clause.
clause. Add
Add
an
an alias
alias for
for each
each column
column name.
name.
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;
View created.

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

12-12 Copyright  Oracle Corporation, 1998. All rights reserved.


Creating a Complex View
Create
Create aa complex
complex view
view that
that contains
contains group
group
functions
functions to
to display
display values
values from
from two
two tables.
tables.
SQL> CREATE VIEW dept_sum_vu
2 (name, minsal, maxsal, avgsal)
3 AS SELECT d.dname, MIN(e.sal), MAX(e.sal),
4 AVG(e.sal)
5 FROM emp e, dept d
6 WHERE e.deptno = d.deptno
7 GROUP BY d.dname;
View created.

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

12-14 Copyright  Oracle Corporation, 1998. All rights reserved.


Rules for Performing
DML Operations on a View
•• You
You cannot
cannot modify
modify data
data in in aa view
view if
if it
it contains:
contains:
–– Any
Any of
of the
the conditions
conditions mentioned
mentioned in in the
the
previous
previous slide
slide
–– Columns
Columns defined
defined by
by expressions
expressions
–– The
The ROWNUM
ROWNUM pseudocolumn
pseudocolumn
•• You
You cannot
cannot add
add data
data if:
if:
–– The
The view
view contains
contains any
any of of the
the conditions
conditions
mentioned
mentioned above
above or
or in
in the
the previous
previous slideslide
–– There
There are
are NOT
NOT NULL
NULL columns
columns in in the
the base
base
tables
tables that
that are
are not
not selected
selected by by the
the view
view
12-15 Copyright  Oracle Corporation, 1998. All rights reserved.
Using the WITH CHECK OPTION
Clause
•• You
You can
can ensure
ensure that
that DML
DML on
on the
the view
view stays
stays
within
within the
the domain
domain of
of the
the view
view by
by using
using the
the
WITH
WITH CHECK
CHECK OPTION
OPTION clause.
clause.
SQL> CREATE OR REPLACE VIEW empvu20
2 AS SELECT *
3 FROM emp
4 WHERE deptno = 20
5 WITH CHECK OPTION CONSTRAINT empvu20_ck;
View created.

•• 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;

SQL> DROP VIEW empvu10;


View dropped.

12-18 Copyright  Oracle Corporation, 1998. All rights reserved.


Summary
•• A
A view
view is
is derived
derived from
from data
data in
in other
other
tables
tables or
or other
other views.
views.
•• A
A view
view provides
provides the
the following
following
advantages:
advantages:
–– Restricts
Restricts database
database access
access
–– Simplifies
Simplifies queries
queries
–– Provides
Provides data
data independence
independence
–– Allows
Allows multiple
multiple views
views of
of the
the same
same data
data
–– Can
Can be
be dropped
dropped without
without removing
removing the
the
underlying
underlying data
data
12-19 Copyright  Oracle Corporation, 1998. All rights reserved.
Practice Overview

•• 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

12-20 Copyright  Oracle Corporation, 1998. All rights reserved.

You might also like