- •Objectivesj i
- •Queryingi a Viewi
- •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
- •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
Modifyingi i a Viewi
•• Modifyify thet EMPVU10 viewi by usingi
CREATE OR REPLACE VIEWI clausel .. Add an aliasli forf r each columnl name..
|
|
|
|
|
|
|
SQL> |
CREATE OR REPLACE VIEW empvu10 |
|
|
|
|
2 |
|
(employee_number, employee_name, |
jo |
|
|
3 |
AS SELECT |
empno, ename, job |
|
|
|
4 |
FROM |
emp |
|
|
|
5 |
WHERE |
deptno = |
10; |
|
|
View |
created. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
•• Columnl aliasesli inin thet CREATE VIEWI clausel arere listedli t inin thet same orderr r as thet columnsl inin thet subqueryry..
12-12
Creatingi a Complexl Viewi
Create a complex view that contains group functions to display values from two tables.
|
|
|
|
|
|
|
SQL> |
CREATE VIEW |
dept_sum_vu |
|
|
|
2 |
|
(name, |
|
minsa |
|
3 |
AS SELECT |
d.dname, MIN(e.sal), MAX(e.sal), |
||
|
4 |
|
AVG(e.sal) |
||
|
5 |
FROM |
emp e, dept |
||
|
6 |
WHERE |
e.deptno = d |
||
|
7 |
GROUP BY |
d.dname; |
||
|
View |
created. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12-13
Rulesl for Performingi DML Operationsi on a Viewi
•• You can performrf rm DML operationsr ti on simplei le viewsi ..
•• You cannott remover a rowr ifif thet viewi containst i thet following:f ll i :
–Groupr functionsf ti
–A GROUP BY clausel
–The DISTINCTI I keywordrd
12-14
Rulesl for Performingi DML Operationsi on a Viewi
•• You cannott modifyify datata inin a viewi ifif itit contains:t i :
–Any off thet conditionsiti mentionedti inin thet previousr i slideli
–Columnsl definedfi by expressionsr i
–The ROWNUM pseudocolumnl
•• You cannott add datata if:if:
–The viewi containst i any off thet conditionsiti mentionedti above orr inin thet previousr i slideli
–Therere arere NOT NULL columnsl inin thet base tablest l thatt t arere nott selectedl t by thet viewi
12-15
Usingi the WITHI CHECK OPTIONI
Clausel
•• You can ensurere thatt t DML on thet viewi stayst withinit in thet domainin off thet viewi by usingi thet WITHI CHECK OPTIONI ..
|
|
|
|
|
|
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 attempttt t toto change thet departmentrt t numberr forf r any rowr inin thet viewi willill failf il because itit violatesi l t thet WITHI CHECK OPTIONI constrainttr i t..
12-16
Denyingi DML Operationsi
•• You can ensurere thatt t no DML operationsr ti occurr by addingi thet WITHI READ ONLY optionti toto yourr viewi definitionfi iti ..
|
|
|
|
|
|
|
|
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 attempttt t toto performrf rm a DML on any rowr inin thet viewi willill resultr lt inin Oracler le Serverr r errorrr r ORA--01752..
12-17
Removingi a Viewi
Remove a view without losing data because a view is based on underlying tables in the database.

DROP VIEW view;
SQL> DROP VIEW empvu10;
View dropped.
12-18
Summary
•• A viewi isis derivedri fromfr datata inin othert r tablest l orr othert r viewsi ..
•• A viewi providesr i thet followingf ll i advantages:t :
–Restrictstri ts databaset access
–Simplifiesi lifi queriesri
–Providesr i datata independencei
–Allowsll multiplelti le viewsi off thet same datata
–Can be droppedr withoutit t removingr i thet underlyingrl i datata
12-19
Practicei Overviewi
•Creating a simple view
•Creating a simple view
•Creating a complex view
•Creating a complex view
•Creating a view with a check constraint
•Creating a view with a check constraint
•Attempting to modify data in the view
•Attempting to modify data in the view
•Displaying view definitions
•Displaying view definitions
•Removing views
•Removing views
12-20
