SlideShare a Scribd company logo
Copyright ำำ Oracle Corporation, 1998. All rights reserved.
1212
Creating Views
12-2 Copyright ำำ Oracle Corporation, 1998. All rights reserved.
Objectives
After completing this lesson, you shouldAfter completing this lesson, you should
be able to do the followingbe 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-3 Copyright ำำ Oracle Corporation, 1998. All rights reserved.
Database Objects
Description
Basic unit of storage; composed of rows
and columns
Logically represents subsets of data from
one or more tables
Generates primary key values
Improves the performance of some queries
Alternative name for an object
Object
Table
View
Sequence
Index
Synonym
12-4 Copyright ำำ Oracle Corporation, 1998. All rights reserved.
What Is a View?
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ------- --------- ----- --------- ----- ----- -------
7839 KING PRESIDENT 17-NOV-81 5000 10
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7900 JAMES CLERK 7698 03-DEC-81 950 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ------- --------- ----- --------- ----- ----- -------
7839 KING PRESIDENT 17-NOV-81 5000 10
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7900 JAMES CLERK 7698 03-DEC-81 950 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
EMP TableEMP Table
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- -------- --------- ---- --------- ------ ----- -------
7839 KING PRESIDENT 17-NOV-81 5000 10
7782 CLARK MANAGER 7839 09-JUN-81 1500 300 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7900 JAMES CLERK 7698 03-DEC-81 950 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- -------- --------- ---- --------- ------ ----- -------
7839 KING PRESIDENT 17-NOV-81 5000 10
7782 CLARK MANAGER 7839 09-JUN-81 1500 300 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 09-DEC-82 3000 20
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7900 JAMES CLERK 7698 03-DEC-81 950 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
EMPNO ENAME JOB
------ -------- -----------
7839 KING PRESIDENT
7782 CLARK MANAGER
7934 MILLER CLERK
EMPVU10 ViewEMPVU10 View
12-5 Copyright ำำ Oracle Corporation, 1998. All rights reserved.
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-6 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-7 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.
• 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]
CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view
[(alias[, alias]...)]
AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY]
12-8 Copyright ำำ Oracle Corporation, 1998. All rights reserved.
Creating a View
• Create a view, EMPVU10, that contains
details of employees in department 10.
• Describe the structure of the view by
using the SQL*Plus DESCRIBE
command.
SQL> DESCRIBE empvu10SQL> DESCRIBE empvu10
SQL> CREATE VIEW empvu10
2 AS SELECT empno, ename, job
3 FROM emp
4 WHERE deptno = 10;
View createdView created..
12-9 Copyright ำำ Oracle Corporation, 1998. All rights reserved.
Creating a View
• Create a view by using column aliases
in the subquery.
• Select the columns from this view by
the given alias names.
SQL> CREATE VIEW salvu30
2 AS SELECT empno EMPLOYEE_NUMBER, ename NAME,
3 sal SALARY
4 FROM emp
5 WHERE deptno = 30;
View createdView created..
12-10 Copyright ำำ Oracle Corporation, 1998. All rights reserved.
Retrieving Data from a View
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.
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.
SQL> SELECT *
2 FROM salvu30;
12-11 Copyright ำำ Oracle Corporation, 1998. All rights reserved.
Querying a View
USER_VIEWSUSER_VIEWS
EMPVU10EMPVU10
SELECT empno, ename, job
FROM emp
WHERE deptno = 10;
USER_VIEWSUSER_VIEWS
EMPVU10EMPVU10
SELECT empno, ename, job
FROM emp
WHERE deptno = 10;
SQLSQL**PlusPlus
SELECT *
FROM empvu10;
EMP
7839 KING PRESIDENT
7782 CLARK MANAGER
7934 MILLER CLERK
12-12 Copyright ำำ Oracle Corporation, 1998. All rights reserved.
Modifying a View
• Modify the EMPVU10 view by using
CREATE OR REPLACE VIEW clause. Add
an alias for each column name.
• Column aliases in the CREATE VIEW
clause are listed in the same order as the
columns in the subquery.
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 createdView created..
12-13 Copyright ำำ Oracle Corporation, 1998. All rights reserved.
Creating a Complex View
Create a complex view that contains groupCreate a complex view that contains group
functions to display values from two tablesfunctions to display values from two 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 createdView created..
12-14 Copyright ำำ 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-15 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-16 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.
• Any attempt to change the department
number for any row in the view will fail
because it violates the WITH CHECK OPTION
constraint.
SQL> CREATE OR REPLACE VIEW empvu20
2 AS SELECT *
3 FROM emp
4 WHERE deptno = 20
5 WITH CHECK OPTION CONSTRAINT empvu20_ck;
View createdView created..
12-17 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> 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 createdView created..
• Any attempt to perform a DML on any
row in the view will result in Oracle
Server error.
12-18 Copyright ำำ Oracle Corporation, 1998. All rights reserved.
Removing a View
Remove a view without losing dataRemove a view without losing data
because a view is based on underlyingbecause a view is based on underlying
tables in the databasetables in the database..
SQL> DROP VIEW empvu10;
View droppedView dropped..
DROP VIEW view;DROP VIEW view;
12-19 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-20 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

More Related Content

What's hot (20)

PPT
Les01
arnold 7490
 
PPT
Les12
arnold 7490
 
PPT
Les01
Yousif Misbah
 
PPT
SQL WORKSHOP::Lecture 12
Umair Amjad
 
PPT
Les10
arnold 7490
 
PPT
Les09
arnold 7490
 
PPT
Les11
arnold 7490
 
PPT
Les02
jsgcruz
 
PDF
MERGE SQL Statement: Lesser Known Facets
Andrej Pashchenko
 
PPT
Les05[1]Aggregating Data Using Group Functions
siavosh kaviani
 
PPT
Les08-Oracle
suman1248
 
PPT
SQL WORKSHOP::Lecture 2
Umair Amjad
 
PPT
Les06[1]Subqueries
siavosh kaviani
 
PPT
Les02[1]Restricting and Sorting Data
siavosh kaviani
 
PPT
Les03[1] Single-Row Functions
siavosh kaviani
 
PPT
Single row functions
Balqees Al.Mubarak
 
DOC
ORACLE NOTES
Sachin Shukla
 
PDF
Database Management System
Hitesh Mohapatra
 
SQL WORKSHOP::Lecture 12
Umair Amjad
 
Les02
jsgcruz
 
MERGE SQL Statement: Lesser Known Facets
Andrej Pashchenko
 
Les05[1]Aggregating Data Using Group Functions
siavosh kaviani
 
Les08-Oracle
suman1248
 
SQL WORKSHOP::Lecture 2
Umair Amjad
 
Les06[1]Subqueries
siavosh kaviani
 
Les02[1]Restricting and Sorting Data
siavosh kaviani
 
Les03[1] Single-Row Functions
siavosh kaviani
 
Single row functions
Balqees Al.Mubarak
 
ORACLE NOTES
Sachin Shukla
 
Database Management System
Hitesh Mohapatra
 

Similar to Les12 creating views (20)

PPTX
Oracle views
Balqees Al.Mubarak
 
PPT
Les12[1]Creating Views
siavosh kaviani
 
PPT
Creating Views - oracle database
Salman Memon
 
PPT
Sql views
arshid045
 
PPT
Les11
Vijay Kumar
 
PPT
Les11.ppt
AlhassanFederated
 
PPT
Creating other schema objects
Syed Zaid Irshad
 
PPT
Les10
Sudharsan S
 
PPT
e computer notes - Creating views
ecomputernotes
 
PDF
Lesson10
renguzi
 
PDF
Database Oracle Basic
Kamlesh Singh
 
PPT
Views
Rahul Gupta
 
PPT
Oracle training institute in hyderabad
appaji intelhunt
 
PPTX
DBMS weSDFDDDFDFDFDSSek 4,5 BSCS 6th.pptx
universalcomputer1
 
PPTX
SQL(NEW).pptx
PoojaChawan2
 
PDF
Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole College
dezyneecole
 
DOCX
It6312 dbms lab-ex2
MNM Jain Engineering College
 
PPT
Oracle view
Madhavendra Dutt
 
PDF
Sql tutorial
amitabros
 
Oracle views
Balqees Al.Mubarak
 
Les12[1]Creating Views
siavosh kaviani
 
Creating Views - oracle database
Salman Memon
 
Sql views
arshid045
 
Creating other schema objects
Syed Zaid Irshad
 
e computer notes - Creating views
ecomputernotes
 
Lesson10
renguzi
 
Database Oracle Basic
Kamlesh Singh
 
Oracle training institute in hyderabad
appaji intelhunt
 
DBMS weSDFDDDFDFDFDSSek 4,5 BSCS 6th.pptx
universalcomputer1
 
SQL(NEW).pptx
PoojaChawan2
 
Divyansh Mehta,BCA Final Year 2015 ,Dezyne E'cole College
dezyneecole
 
It6312 dbms lab-ex2
MNM Jain Engineering College
 
Oracle view
Madhavendra Dutt
 
Sql tutorial
amitabros
 
Ad

Recently uploaded (20)

PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Dimensions of Societal Planning in Commonism
StefanMz
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Ad

Les12 creating views

  • 1. Copyright ำำ Oracle Corporation, 1998. All rights reserved. 1212 Creating Views
  • 2. 12-2 Copyright ำำ Oracle Corporation, 1998. All rights reserved. Objectives After completing this lesson, you shouldAfter completing this lesson, you should be able to do the followingbe 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
  • 3. 12-3 Copyright ำำ Oracle Corporation, 1998. All rights reserved. Database Objects Description Basic unit of storage; composed of rows and columns Logically represents subsets of data from one or more tables Generates primary key values Improves the performance of some queries Alternative name for an object Object Table View Sequence Index Synonym
  • 4. 12-4 Copyright ำำ Oracle Corporation, 1998. All rights reserved. What Is a View? EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------- --------- ----- --------- ----- ----- ------- 7839 KING PRESIDENT 17-NOV-81 5000 10 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7900 JAMES CLERK 7698 03-DEC-81 950 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7369 SMITH CLERK 7902 17-DEC-80 800 20 7788 SCOTT ANALYST 7566 09-DEC-82 3000 20 7876 ADAMS CLERK 7788 12-JAN-83 1100 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------- --------- ----- --------- ----- ----- ------- 7839 KING PRESIDENT 17-NOV-81 5000 10 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7900 JAMES CLERK 7698 03-DEC-81 950 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7369 SMITH CLERK 7902 17-DEC-80 800 20 7788 SCOTT ANALYST 7566 09-DEC-82 3000 20 7876 ADAMS CLERK 7788 12-JAN-83 1100 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10 EMP TableEMP Table EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- -------- --------- ---- --------- ------ ----- ------- 7839 KING PRESIDENT 17-NOV-81 5000 10 7782 CLARK MANAGER 7839 09-JUN-81 1500 300 10 7934 MILLER CLERK 7782 23-JAN-82 1300 10 7566 JONES MANAGER 7839 02-APR-81 2975 20 7788 SCOTT ANALYST 7566 09-DEC-82 3000 20 7876 ADAMS CLERK 7788 12-JAN-83 1100 20 7369 SMITH CLERK 7902 17-DEC-80 800 20 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7900 JAMES CLERK 7698 03-DEC-81 950 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- -------- --------- ---- --------- ------ ----- ------- 7839 KING PRESIDENT 17-NOV-81 5000 10 7782 CLARK MANAGER 7839 09-JUN-81 1500 300 10 7934 MILLER CLERK 7782 23-JAN-82 1300 10 7566 JONES MANAGER 7839 02-APR-81 2975 20 7788 SCOTT ANALYST 7566 09-DEC-82 3000 20 7876 ADAMS CLERK 7788 12-JAN-83 1100 20 7369 SMITH CLERK 7902 17-DEC-80 800 20 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7900 JAMES CLERK 7698 03-DEC-81 950 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 EMPNO ENAME JOB ------ -------- ----------- 7839 KING PRESIDENT 7782 CLARK MANAGER 7934 MILLER CLERK EMPVU10 ViewEMPVU10 View
  • 5. 12-5 Copyright ำำ Oracle Corporation, 1998. All rights reserved. Why Use Views? • To restrict database access • To make complex queries easy • To allow data independence • To present different views of the same data
  • 6. 12-6 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
  • 7. 12-7 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. • 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] CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [(alias[, alias]...)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READ ONLY]
  • 8. 12-8 Copyright ำำ Oracle Corporation, 1998. All rights reserved. Creating a View • Create a view, EMPVU10, that contains details of employees in department 10. • Describe the structure of the view by using the SQL*Plus DESCRIBE command. SQL> DESCRIBE empvu10SQL> DESCRIBE empvu10 SQL> CREATE VIEW empvu10 2 AS SELECT empno, ename, job 3 FROM emp 4 WHERE deptno = 10; View createdView created..
  • 9. 12-9 Copyright ำำ Oracle Corporation, 1998. All rights reserved. Creating a View • Create a view by using column aliases in the subquery. • Select the columns from this view by the given alias names. SQL> CREATE VIEW salvu30 2 AS SELECT empno EMPLOYEE_NUMBER, ename NAME, 3 sal SALARY 4 FROM emp 5 WHERE deptno = 30; View createdView created..
  • 10. 12-10 Copyright ำำ Oracle Corporation, 1998. All rights reserved. Retrieving Data from a View 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. 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. SQL> SELECT * 2 FROM salvu30;
  • 11. 12-11 Copyright ำำ Oracle Corporation, 1998. All rights reserved. Querying a View USER_VIEWSUSER_VIEWS EMPVU10EMPVU10 SELECT empno, ename, job FROM emp WHERE deptno = 10; USER_VIEWSUSER_VIEWS EMPVU10EMPVU10 SELECT empno, ename, job FROM emp WHERE deptno = 10; SQLSQL**PlusPlus SELECT * FROM empvu10; EMP 7839 KING PRESIDENT 7782 CLARK MANAGER 7934 MILLER CLERK
  • 12. 12-12 Copyright ำำ Oracle Corporation, 1998. All rights reserved. Modifying a View • Modify the EMPVU10 view by using CREATE OR REPLACE VIEW clause. Add an alias for each column name. • Column aliases in the CREATE VIEW clause are listed in the same order as the columns in the subquery. 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 createdView created..
  • 13. 12-13 Copyright ำำ Oracle Corporation, 1998. All rights reserved. Creating a Complex View Create a complex view that contains groupCreate a complex view that contains group functions to display values from two tablesfunctions to display values from two 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 createdView created..
  • 14. 12-14 Copyright ำำ 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
  • 15. 12-15 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
  • 16. 12-16 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. • Any attempt to change the department number for any row in the view will fail because it violates the WITH CHECK OPTION constraint. SQL> CREATE OR REPLACE VIEW empvu20 2 AS SELECT * 3 FROM emp 4 WHERE deptno = 20 5 WITH CHECK OPTION CONSTRAINT empvu20_ck; View createdView created..
  • 17. 12-17 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> 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 createdView created.. • Any attempt to perform a DML on any row in the view will result in Oracle Server error.
  • 18. 12-18 Copyright ำำ Oracle Corporation, 1998. All rights reserved. Removing a View Remove a view without losing dataRemove a view without losing data because a view is based on underlyingbecause a view is based on underlying tables in the databasetables in the database.. SQL> DROP VIEW empvu10; View droppedView dropped.. DROP VIEW view;DROP VIEW view;
  • 19. 12-19 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
  • 20. 12-20 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

Editor's Notes

  • #2: Schedule:TimingTopic 20 minutesLecture 20 minutesPractice 40 minutesTotal
  • #3: Lesson Aim In this lesson, you will learn to create and use views. You will also learn to query the relevant data dictionary object to retrieve information about views.
  • #5: What Is a View? You can present logical subsets or combinations of data by creating views of tables. A view is a logical table based on a table or another view. A view contains no data of its own but is like a window through which data from tables can be viewed or changed. The tables on which a view is based are called base tables. The view is stored as a SELECT statement in the data dictionary.
  • #6: Advantages of Views Views restrict access to the database because the view can display a selective portion of the database. Views allow users to make simple queries to retrieve the results from complicated queries. For example, views allow users to query information from multiple tables without knowing how to write a join statement. Views provide data independence for ad hoc users and application programs. One view can be used to retrieve data from several tables. Views provide groups of users access to data according to their particular criteria. For more information, seeOracle Server SQL Reference, Release 8, “CREATE VIEW.”
  • #7: Simple Views Versus Complex Views There are two classifications for views: simple and complex. The basic difference is related to the DML (insert, update, and delete) operations. A simple view is one that: Derives data from only one table Contains no functions or groups of data Can perform DML through the view A complex view is the one that: Derives data from many tables Contains functions or groups of data Does not always allow DML through the view
  • #8: Creating a View You can create a view by embedding a subquery within the CREATE VIEW statement. In the syntax: OR REPLACEre-creates the view if it already exists FORCEcreates the view regardless of whether or not the base tables exist NOFORCEcreates the view only if the base tables exist (This is the default.) viewis the name of the view aliasspecifies names for the expressions selected by the view’s query (The number of aliases must match the number of expressions selected by the view.) subqueryis a complete SELECT statement (You can use aliases for the columns in the SELECT list.) WITH CHECK OPTIONspecifies that only rows accessible to the view can be inserted or updated constraintis the name assigned to the CHECK OPTION constraint WITH READ ONLYensures that no DML operations can be performed on this view
  • #9: Creating a View (continued) The example on the slide creates a view that contains the employee number, name, and job title for all the employees in department 10. You can display the structure of the view by using the SQL*Plus DESCRIBE command. Name Null? Type ------------------------------- -------- ------------ EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) Guidelines for creating a view: The subquery that defines a view can contain complex SELECT syntax, including joins, groups, and subqueries. The subquery that defines the view cannot contain an ORDER BY clause. The ORDER BY clause is specified when you retrieve data from the view. If you do not specify a constraint name for a view created with the CHECK OPTION, the system will assign a default name in the format SYS_Cn. You can use the OR REPLACE option to change the definition of the view without dropping and re-creating it or regranting object privileges previously granted on it.
  • #10: Creating a View (continued) You can control the column names by including column aliases within the subquery. The example on the slide creates a view containing the employee number (empno) with the alias EMPLOYEE_NUMBER, name (ename) with the alias NAME, and salary (sal) with the alias SALARY for department 30. Alternatively, you can control the column names by including column aliases in the CREATE VIEW clause.
  • #11: Retrieving Data from a View You can retrieve data from a view as you would from any table. You can either display the contents of the entire view or just view specific rows and columns.
  • #12: Views in the Data Dictionary
  • #13: Modifying a View The OR REPLACE option allows a view to be created even if one exists with this name already, thus replacing the old version of the view for its owner. This means that the view can be altered without dropping, re-creating, and regranting object privileges. Note: When assigning column aliases in the CREATE VIEW clause, remember that the aliases are listed in the same order as the columns in the subquery. Class Management Note The OR REPLACE option started with Oracle7. With earlier versions of Oracle, if the view needed to be changed, it had to be dropped and re-created. Demo: l12emp.sql Purpose: To illustrate creating a view using aliases.
  • #14: Creating a Complex View 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. You can view the structure of the view by using the SQL*Plus DESCRIBE command. Display the contents of the view by issuing a SELECT statement.
  • #15: Performing DML Operations on a View You can perform DML operations on data through a view if those operations follow certain rules. You can remove a row from a view unless it contains any of the following: Group functions A GROUP BY clause The DISTINCT keyword
  • #16: Performing DML Operations on a View (continued) You can modify data in a view unless it contains any of the conditions mentioned in the previous slide and any of the following: Columns defined by expressions—for example, SALARY * 12 The ROWNUM pseudocolumn You can add data through a view unless it contains any of the above and there are NOT NULL columns, without a default value, in the base table that are not selected by the view. All required values must be present in the view. Remember that you are adding values directly into the underlying table through the view. For more information, see 0racle8 Server SQL Reference, Release 8, “CREATE VIEW.” Class Management Note Oracle7.3 and Oracle8 allow you, with some restrictions, to modify views that involve joins. The restrictions for DML operations described above also apply to join views. Any UPDATE, INSERT, or DELETE statement on a join view can modify only one underlying base table. If at least one column in the subquery join has a unique index, then it may be possible to modify one base table in a join view. You can query USER_UPDATABLE_COLUMNS to see whether the columns in a join view are updatable. (For Page 12-16) If the user does not supply a constraint name, the system assigns a name in the form SYS_Cn, where n is an integer that makes the constraint name unique within the system. Demo: l12vu1.sql, l12vu2.sql Purpose: To illustrate creating views with and without the CHECK OPTION.
  • #17: Using the WITH CHECK OPTION Clause It is possible to perform referential integrity checks through views. You can also enforce constraints at the database level. The view can be used to protect data integrity, but the use is very limited. The WITH CHECK OPTION clause specifies that INSERTS and UPDATES performed through the view are not allowed to create rows that the view cannot select, and therefore it allows integrity constraints and data validation checks to be enforced on data being inserted or updated. If there is an attempt to perform DML operations on rows that the view has not selected, an error is displayed, with the constraint name if that has been specified. SQL> UPDATE empvu20 2 SET deptno = 10 3 WHERE empno = 7788; update empvu20 * ERROR at line 1: ORA-01402: view WITH CHECK OPTION where-clause violation Note: No rows are updated because if the department number were to change to 10, the view would no longer be able to see that employee. Therefore, with the WITH CHECK OPTION clause, the view can see only department 20 employees and does not allow the department number for those employees to be changed through the view.
  • #18: Denying DML Operations You can ensure that no DML operations occur on your view by creating it with the WITH READ ONLY option. The example on the slide modifies the EMPVU10 view to prevent any DML operations on the view. Any attempts to remove a row from the view will result in an error. Any attempts to insert a row or modify a row using the view will result in Oracle Server error -01733: virtual column not allowed here.
  • #19: Removing a View You use the DROP VIEW statement to remove a view. The statement removes the view definition from the database. Dropping views has no effect on the tables on which the view was based. Views or other applications based on deleted views become invalid. Only the creator or a user with the DROP ANY VIEW privilege can remove a view. In the syntax: viewis the name of the view
  • #20: What Is a View? A view is based on a table or another view and acts as a window through which data on tables can be viewed or changed. A view does not contain data. The definition of the view is stored in the data dictionary. You can see the definition of the view in the USER_VIEWS data dictionary table. Advantages of Views Restrict database access Simplify queries Provide data independence Allow multiple views of the same data Remove views without affecting the underlying data View Options Can be a simple view based on one table Can be a complex view based on more than one table or can contain groups or functions Can be replaced if one of the same name exists Contain a check constraint Can be read-only
  • #21: Practice Overview In this practice, you will create simple and complex views and attempt to perform DML statements on the views.
  • #22: Practice 12 1.Create a view called EMP_VU based on the employee number, employee name, anddepartment number from the EMP table. Change the heading for the employee name toEMPLOYEE. 2.Display the contents of the EMP_VU view. EMPNO EMPLOYEE DEPTNO ----- -------- ------ 7839 KING 10 7698 BLAKE 30 7782 CLARK 10 7566 JONES 20 7654 MARTIN 30 7499 ALLEN 30 7844 TURNER 30 7900 JAMES 30 7521 WARD 30 7902 FORD 20 7369 SMITH 20 7788 SCOTT 20 7876 ADAMS 20 7934 MILLER 10 14 rows selected. 3.Select the view name and text from the data dictionary USER_VIEWS. VIEW_NAME TEXT ----------- ---------------------------------------- EMP_VU SELECT empno, ename employee, deptno FROM emp 4.Using your view EMP_VU, enter a query to display all employee names and department numbers. EMPLOYEE DEPTNO ---------- --------- KING 10 BLAKE 30 CLARK 10 JONES 20 MARTIN 30 ... 14 rows selected.
  • #23: Practice 12 (continued) 5.Create a view named DEPT20 that contains the employee number, employee name, anddepartment number for all employees in department 20. Label the view columnEMPLOYEE_ID, EMPLOYEE, and DEPARTMENT_ID. Do not allow an employee to be reassigned to another department through the view. 6.Display the structure and contents of the DEPT20 view. Name Null? Type --------------- -------- ------ EMPLOYEE_ID NOT NULL NUMBER(4) EMPLOYEE VARCHAR2(10) DEPARTMENT_ID NOT NULL NUMBER(2) EMPLOYEE_ID EMPLOYEE DEPARTMENT_ID ----------- -------- ------------- 7566 JONES20 7902 FORD20 7369 SMITH20 7788 SCOTT20 7876 ADAMS20 7.Attempt to reassign Smith to department 30. If you have time, complete the following exercise: 8.Create a view called SALARY_VU based on the employee name, department name, salary, and salary grade for all employees. Label the columns Employee, Department, Salary, andGrade, respectively.