You are on page 1of 27

12

Creating Views

Copyright

Oracle Corporation, 1998. All rights reserved.

Objectives
After completing this lesson, you should
be able to do the following:
Describe a view
Create a view
Retrieve data through a view
Alter the definition of a view
Insert, update, and delete data through
a view
Drop a view
12-2

Copyright

Oracle Corporation, 1998. All rights reserved.

Database Objects

12-3

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

Copyright

Oracle Corporation, 1998. All rights reserved.

What Is a View?
EMP Table
EMPNO
JOB
MGR
SAL
SAL
COMM
DEPTNO
EMPNO ENAME
ENAME JOB
JOB
JOB
MGR HIREDATE
HIREDATE
SAL
SAL COMM
COMM
COMM DEPTNO
DEPTNO
DEPTNO
---------------------------------------------------------------------------------- -------------- ------------------------- --------- ---------- ---------------------7839
7839
7839
7839 KING
KING
7782
7698
BLAKE
7782
7698 CLARK
BLAKE
CLARK

PRESIDENT
PRESIDENT
17-NOV-81
5000
PRESIDENT
PRESIDENT
17-NOV-81 5000
5000
5000
MANAGER
MANAGER
01-MAY-81
1500
MANAGER
MANAGER 7839
7839 09-JUN-81
01-MAY-81 2850
09-JUN-81
2850
1500
7934
7782
CLARK
MILLER
MANAGER
CLERK
7839
7782
09-JUN-81
23-JAN-82
2450
1300
7934
7782 CLARK
MILLER MANAGER
CLERK
7839 23-JAN-82
7782
09-JUN-81 2450
1300
7566
7566
MANAGER
2975
7566
7566 JONES
JONES MANAGER
MANAGER
MANAGER 7839
7839 02-APR-81
02-APR-81 2975
2975
2975

EMPVU10
ViewSALESMAN
7788
7654
MARTIN
ANALYST
7698
28-SEP-81
7788
7654 SCOTT
MARTIN
SCOTT
SALESMAN
ANALYST 7566
7698 09-DEC-82
7566
28-SEP-81
09-DEC-82
7499
ALLEN
CLERK
7698
7788
20-FEB-81
7876
7499 ADAMS
ALLEN SALESMAN
ADAMS
SALESMAN
CLERK
7698 12-JAN-83
7788
20-FEB-81
12-JAN-83
EMPNO 7876
ENAME
JOB
7369
7844
TURNER
SMITH
SALESMAN
CLERK
7698
7902
08-SEP-81
17-DEC-80
7844 SMITH
TURNER SALESMAN
CLERK
7698 17-DEC-80
7902
08-SEP-81
------ 7369
-----------------7902
7900
JAMES
ANALYST
7698
7902
7900 FORD
JAMES CLERK
FORD
CLERK
ANALYST 7566
7698 03-DEC-81
7566
03-DEC-81
7839 7698
KING
PRESIDENT
7521
WARD
SALESMAN
MANAGER
7698
22-FEB-81
7698
7521 BLAKE
WARD
BLAKE
SALESMAN
MANAGER 7839
7698 01-MAY-81
7839
22-FEB-81
01-MAY-81
7902
FORD
ANALYST
SALESMAN
7566
7698
03-DEC-81
7782 7654
CLARK
MANAGER
7654
7902 MARTIN
FORD
MARTIN
ANALYST
SALESMAN
7566 28-SEP-81
7698
03-DEC-81
28-SEP-81
7499
7369
SMITH
ALLEN
CLERK
SALESMAN
7902
7698
17-DEC-80
20-FEB-81
7369 ALLEN
SMITH
CLERK
SALESMAN
7902 20-FEB-81
17-DEC-80
7934 7499
MILLER
CLERK7698

Copyright

10
10
10
10
20
20
20
20

1250
3000
1250
3000 1400
1400
1600
1100
1600
1100 300
300

30
20
30
20
30
20
30
20

1500
800
1500
800
950
3000
950
3000

30
20
30
20
30
20
30
20

00

1250
2850
1250
2850 500
500
3000
1250
3000
1250 1400
1400
800
1600
300
800
1600
300

7844
7788
SCOTT
SALESMAN
7698
09-DEC-82
1500
7844
7788 TURNER
SCOTT ANALYST
TURNER
ANALYST
SALESMAN 7566
7566 08-SEP-81
7698
08-SEP-81 3000
09-DEC-82
3000
1500
7900
7876
ADAMS
CLERK
7788
7698
12-JAN-83
950
7900
7876 JAMES
ADAMS CLERK
JAMES
CLERK
CLERK
7788 03-DEC-81
7698
12-JAN-83 1100
03-DEC-81
1100
950
7521
7934
MILLER
SALESMAN
7782
23-JAN-82
1250
7521
7934 WARD
MILLER CLERK
WARD
CLERK
SALESMAN 7698
7782 22-FEB-81
7698
23-JAN-82 1300
22-FEB-81
1300
1250

12-4

300
300

10
10
10
10
30
10
30
10

Oracle Corporation, 1998. All rights reserved.

30
30
30
30
20
30
20
30

00

20
30
20
30
20
30
20
30

500
500

20
30
20
30
10
30
10
30

Why Use Views?


To restrict database access
To make complex queries easy
To allow data independence
To present different views of the same
data

12-5

Copyright

Oracle Corporation, 1998. All rights reserved.

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 through view

Yes

Not always

12-6

Copyright

Oracle Corporation, 1998. All rights reserved.

Creating a View
You embed a subquery within the CREATE VIEW statement.

The subquery can contain complex SELECT syntax.

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]

12-7

Copyright

Oracle Corporation, 1998. All rights reserved.

Creating a View
Create a view, EMPVU10, that contains
details of employees in department 10.
SQL>
2
3
4
View

CREATE VIEW
AS SELECT
FROM
WHERE
created.

empvu10
empno, ename, job
emp
deptno = 10;

Describe the structure of the view by


using the SQL*Plus DESCRIBE
command.
SQL>
SQL> DESCRIBE
DESCRIBE empvu10
empvu10
12-8

Copyright

Oracle Corporation, 1998. All rights reserved.

Creating a View
Create a view by using column aliases in
the subquery.
SQL>
2
3
4
5
View

CREATE VIEW
AS SELECT
FROM
WHERE
created.

salvu30
empno EMPLOYEE_NUMBER, ename NAME,
sal SALARY
emp
deptno = 30;

Select the columns from this view by the


given alias names.

12-9

Copyright

Oracle Corporation, 1998. All rights reserved.

Retrieving Data from a View


SQL>
2

SELECT *
FROM
salvu30;

EMPLOYEE_NUMBER
EMPLOYEE_NUMBER
----------------------------7698
7698
7654
7654
7499
7499
7844
7844
7900
7900
7521
7521

NAME
SALARY
NAME
SALARY
------------------- ----------------BLAKE
2850
BLAKE
2850
MARTIN
1250
MARTIN
1250
ALLEN
1600
ALLEN
1600
TURNER
1500
TURNER
1500
JAMES
950
JAMES
950
WARD
1250
WARD
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 *
FROM
empvu10;

7839
7782
7934

12-11

KING
PRESIDENT
CLARK MANAGER
MILLER CLERK

Copyright

SELECT
SELECT
FROM
FROM
WHERE
WHERE

Oracle Corporation, 1998. All rights reserved.

EMPVU10
EMPVU10

empno,
empno, ename,
ename, job
job
emp
emp
deptno
deptno == 10;
10;

EMP

Modifying a View
Modify the EMPVU10 view by using
CREATE OR REPLACE VIEW clause. Add
an alias for each column name.
SQL>
2
3
4
5
View

CREATE OR REPLACE VIEW empvu10


(employee_number, employee_name, job_title)
AS SELECT
empno, ename, job
FROM
emp
WHERE
deptno = 10;
created.

Column aliases in the CREATE VIEW


clause are listed in the same order as the
columns in the subquery.
12-12

Copyright

Oracle Corporation, 1998. All rights reserved.

Creating a Complex View


Create a complex view that contains group
functions to display values from two tables.
SQL>
2
3
4
5
6
7
View

12-13

CREATE VIEW
AS SELECT
FROM
WHERE
GROUP BY
created.

Copyright

dept_sum_vu
(name, minsal, maxsal, avgsal)
d.dname, MIN(e.sal), MAX(e.sal),
AVG(e.sal)
emp e, dept d
e.deptno = d.deptno
d.dname;

Oracle Corporation, 1998. All rights reserved.

Rules for Performing


DML Operations on a View
You can 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

12-14

Copyright

Oracle Corporation, 1998. All rights reserved.

Rules for Performing


DML Operations on a View
You cannot modify data in a view if it contains:
Any of the conditions mentioned in the
previous slide
Columns defined by expressions
The ROWNUM pseudocolumn
You cannot add data if:
The view contains any of the conditions

mentioned above or in the previous slide


There are NOT NULL columns in the base
tables that are not selected by the view
12-15

Copyright

Oracle Corporation, 1998. All rights reserved.

Using the WITH CHECK OPTION


Clause

You can ensure that DML on the view stays


within the domain of the view by using the
WITH CHECK OPTION clause.
SQL>
2
3
4
5
View

CREATE OR REPLACE VIEW empvu20


AS SELECT
*
FROM
emp
WHERE
deptno = 20
WITH CHECK OPTION CONSTRAINT empvu20_ck;
created.

Any attempt to change the department


number for any row in the view will fail
because it violates the WITH CHECK OPTION
constraint.
12-16

Copyright

Oracle Corporation, 1998. All rights reserved.

Denying DML Operations


You can ensure that no DML operations
occur by adding the WITH READ ONLY
option to your view definition.
SQL>
2
3
4
5
6
View

CREATE OR REPLACE VIEW empvu10


(employee_number, employee_name, job_title)
AS SELECT
empno, ename, job
FROM
emp
WHERE
deptno = 10
WITH READ ONLY;
created.

Any attempt to perform a DML on any


row in the view will result in Oracle
Server error.
12-17

Copyright

Oracle Corporation, 1998. All rights reserved.

Removing a View
Remove a view without losing data
because a view is based on underlying
tables in the database.
DROP
DROP VIEW
VIEW view;
view;

SQL> DROP VIEW empvu10;


View dropped.

12-18

Copyright

Oracle Corporation, 1998. All rights reserved.

Example
Select a.ename,a.sal,a.deptno,
b.max_sal from emp a, (select deptno,max(sal) max_sal from emp
Group by deptno) b
Where a.deptno=b.deptno
and a.sal<b.max_sal;
select e.deptno,dname,loc
from emp e,(select deptno,dname,loc from dept) d
where e.deptno=d.deptno;

12-21

Copyright

Oracle Corporation, 1998. All rights reserved.

Example
Finding department details of highest paid
employee:
select ename,e.deptno,dname,empno,sal
from dept e,(select
ename,empno,sal,deptno from emp where
sal=(select max(sal) max_sal from emp
where e.deptno=d.deptno

12-22

Copyright

Oracle Corporation, 1998. All rights reserved.

Performing Top-N Analysis


The high-level structure of a Top-N
analysis query is:
SQL> SELECT [column_list], ROWNUM
2 FROM
table
3

ORDER BY Top-N_column)

4 WHERE
12-23

(SELECT [column_list] FROM

Copyright

ROWNUM <= N
Oracle Corporation, 1998. All rights reserved.

Example of Top-N Analysis


select rownum as Rank,ename,sal
from (select ename,sal from emp order by sal
desc)
where rownum<=3;
select rownum,ename,sal from emp order by sal
desc;
The difference between the above 2 examples are
rownum is obtained after and before the order by
clause .

12-24

Copyright

Oracle Corporation, 1998. All rights reserved.

Example of Top-N Analysis


1.To fetch the first nth row
select * from emp where rownum<=5;
2. To fetch a specific row
select * from (select ename,sal,empno,rownum rn
from emp) where rn=2;
3. To fetch the ranges of rows and specific rows
select * from (select ename,sal,empno,rownum rn
from emp) where rn between 2 and 4;
select * from (select ename,sal,empno,rownum rn
from emp) where rn in (1,3,5);

12-25

Copyright

Oracle Corporation, 1998. All rights reserved.

Example of Top-N Analysis


To view every nth rows
Select * from (select rownum
rn,empno,ename from emp)
Where mod(rn,4)=0;

12-26

Copyright

Oracle Corporation, 1998. All rights reserved.

Summary
A view is derived from data in other tables
or other views.
A view provides the following advantages:
Restricts database access
Simplifies queries
Provides data independence
Allows multiple views of the same data
Can be dropped without removing the

underlying data

12-27

Copyright

Oracle Corporation, 1998. All rights reserved.

Practice Overview
Creating a simple view
Creating a complex view
Creating a view with a check constraint
Attempting to modify data in the view
Displaying view definitions
Removing views

12-28

Copyright

Oracle Corporation, 1998. All rights reserved.

Example of Top-N Analysis


To display the top three earners names and
salaries from the EMP table.
SQL> SELECT ROWNUM as RANK, ename, sal
2FROM
3

(SELECT ename, sal FROM emp

ORDER BY sal DESC)

4WHERE ROWNUM <= 3;


RANK

ENAME

KING

SCOTT

FORD

12-29

Copyright

SAL
5000

3000
3000
Oracle Corporation, 1998. All rights reserved.

You might also like