
- •Objectivesj i
- •Database Objectsj
- •What IsIs a View?i
- •Why Use Views?i
- •Simplei le Viewsi and Complexl Viewsi
- •Creatingi a Viewi
- •Creatingi a Viewi
- •Creatingi a Viewi
- •Retrievingi i Data from a Viewi
- •Queryingi a Viewi
- •Modifyingi i a Viewi
- •Creatingi a Complexl Viewi
- •Rulesl for Performingi DML Operationsi on a Viewi
- •Rulesl for Performingi DML Operationsi on a Viewi
- •Usingi the WITHI CHECK OPTIONI
- •Denyingi DML Operationsi
- •Removingi a Viewi
- •Summary
- •Practicei Overviewi

12
Creating Views

Objectivesj i
After completing this lesson, you should be able to do the following:
•• Describeri a viewi
•• Creater te a viewi
•• Retrievetri datata throught r a viewi
•• Alterlt r thet definitionfi iti off a viewi
•• Insert,I rt, update,t , and deletel te datata throught r a viewi
•• Dropr a viewi
12-2

Database Objectsj
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

What IsIs a View?i
EMP Table
EMPNO |
ENAME |
JOB |
MGR |
HIREDATE |
SAL |
COMM |
DEPTNO |
----- |
-------- |
--------- ---- |
--------- ------ |
----- |
------- |
EMPVU10 View |
|
|
|
|
|
7876 |
DAMS |
CLERK |
7788 12-JAN-83 |
|
|
EMPNO ENAME |
JOB |
|
|
|
|
------ -------- ----------- |
|
|
|||
7369 |
SMITH |
CLERK |
7902 17 DEC 80 |
|
|
7902 |
FORD |
ANALYST |
7566 03-DEC-81 |
|
|
7839 KING |
PRESIDENT |
|
|
||
7698 |
BLAKE |
MANAGER |
7839 01-MAY-81 |
|
|
7782 CLARK |
MANAGER |
|
|
||
7654 |
MA TIN |
SALESMAN |
7698 28-SEP-81 |
|
|
7934 MILLER |
CLERK |
|
|
||
7499 |
A N |
SALESMAN |
7698 20-FEB-81 |
|
|
7844 |
TURNER |
SALESMAN |
7698 08-SEP-81 |
|
|
7900 |
JAMES |
CLERK |
7698 03-DEC-81 |
950 |
30 |
7521 |
WARD |
SALESMAN |
7698 22-FEB-81 |
1250 500 |
30 |
12-4

Why Use Views?i
•• To restrictr tri t databaset access
•• To make complexl queriesri easy
•• To allowll datata independencei
•• To presentr t differentiff r t viewsi off thet same datata
12-5

Simplei le Viewsi and Complexl Viewsi
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

Creatingi a Viewi
•You embed a subquery within the CREATE VIEW statement.
•You embed a subquery within the CREATE VIEW statement.
•The subquery can contain complex SELECT syntax.
•The subquery can contain complex SELECT syntax.
•The subquery cannot contain an ORDER BY clause.
•The subquery cannot contain an ORDER BY clause.
CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view
[(alias[, alias]...)] AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY]
12-7

Creatingi a Viewi
•• Creater te a view,i , EMPVU10,, thatt t containst i detailst ils off employeesl inin departmentrt t 10..
|
|
|
|
|
|
SQL> |
CREATE VIEW |
empvu10 |
|
|
2 |
AS SELECT |
empno, ename, job |
|
|
3 |
FROM |
emp |
|
|
4 |
WHERE |
deptno = 10; |
|
|
View |
created. |
|
|
|
|
|
|
|
•• Describeri thet structuretr t re off thet viewi by usingi thet SQL*Plus* l DESCRIBEI command..
SQL> DESCRIBE empvu10
12-8

Creatingi a Viewi
•• Creater te a viewi by usingi columnl aliasesli inin thet subqueryry..
|
|
|
|
|
|
|
SQL> |
CREATE VIEW |
salvu30 |
|
|
|
2 |
AS SELECT |
empno EMPLOYEE_NUMBER, ename |
|
NAME, |
|
3 |
|
sal SALARY |
||
|
4 |
FROM |
emp |
||
|
5 |
WHERE |
deptno = 30; |
||
|
View |
created. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
•• Selectl t thet columnsl fromfr thist is viewi by thet giveni aliasli names..
12-9

Retrievingi i Data from a Viewi
|
|
|
|
|
|
|
|
SQL> |
SELECT * |
|
|
|
|
2 |
FROM |
salvu30; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EMPLOYEE_NUMBER |
NAME |
SALARY |
|
|
--------------- |
---------- --------- |
|
||
|
7698 |
BLAKE |
2850 |
|
|
|
7654 |
MARTIN |
1250 |
|
|
|
7499 |
ALLEN |
1600 |
|
|
|
7844 |
TURNER |
1500 |
|
|
|
7900 |
JAMES |
950 |
|
|
|
7521 |
WARD |
1250 |
|
|
|
|
6 rows selected. |
|
|
|
|
|
|
|
|
|
12-10

Queryingi a Viewi
SQL*Plus
SELECT *
FROM empvu10;
7839 |
KING PRESIDENT |
7782 |
CLARK MANAGER |
7934 |
MILLER CLERK |
|
USER_VIEWS |
|
I |
|
EMPVU10 |
SELECT |
em |
FROM |
em |
WHERE |
de |
|
EMP |
12-11