SlideShare a Scribd company logo
Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
12
Creating Views
12-2 Copyright ‫س‬ Oracle Corporation, 1999. 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-3 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Objectives
After completing this lesson, you should
be able to do the following:
• Describe an inline view
• Perform “Top-N” Analysis
12-4 Copyright ‫س‬ Oracle Corporation, 1999. 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-5 Copyright ‫س‬ Oracle Corporation, 1999. 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
EMP 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
EMPNO ENAME JOB
------ -------- -----------
7839 KING PRESIDENT
7782 CLARK MANAGER
7934 MILLER CLERK
EMPVU10 View
12-6 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Why Use Views?
• To restrict data access
• To make complex queries easy
• To allow data independence
• To present different views of the same
data
12-7 Copyright ‫س‬ Oracle Corporation, 1999. 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-8 Copyright ‫س‬ Oracle Corporation, 1999. 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];
12-9 Copyright ‫س‬ Oracle Corporation, 1999. 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 empvu10
SQL> CREATE VIEW empvu10
2 AS SELECT empno, ename, job
3 FROM emp
4 WHERE deptno = 10;
View created.
12-10 Copyright ‫س‬ Oracle Corporation, 1999. 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 created.
12-11 Copyright ‫س‬ Oracle Corporation, 1999. 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.
SQL> SELECT *
2 FROM salvu30;
12-12 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Querying a View
USER_VIEWS
EMPVU10
SELECT empno, ename, job
FROM emp
WHERE deptno = 10;
SQL*Plus
SELECT *
FROM empvu10;
EMP
7839 KING PRESIDENT
7782 CLARK MANAGER
7934 MILLER CLERK
12-13 Copyright ‫س‬ Oracle Corporation, 1999. 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 created.
12-14 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Creating a Complex View
Create a complex view that contains group
functions 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 created.
12-15 Copyright ‫س‬ Oracle Corporation, 1999. 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
– The pseudocolumn ROWNUM keyword
12-16 Copyright ‫س‬ Oracle Corporation, 1999. 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-17 Copyright ‫س‬ Oracle Corporation, 1999. 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 created.
12-18 Copyright ‫س‬ Oracle Corporation, 1999. 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 created.
• Any attempt to perform a DML on any
row in the view will result in Oracle
Server error.
12-19 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Removing a View
Remove a view without losing data
because a view is based on underlying
tables in the database.
SQL> DROP VIEW empvu10;
View dropped.
DROP VIEW view;
12-20 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Inline Views
• An inline view is a subquery with an
alias (correlation name) that you can
use within a SQL statement.
• An inline view is similar to using a
named subquery in the FROM clause of
the main query.
• An inline view is not a schema object.
12-21 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
“Top-N” Analysis
• Top-N queries ask for the n largest or
smallest values of a column.
– What are the ten best selling
products?
– What are the ten worst selling
products ?
• Both largest values and smallest values
sets are considered Top-N queries.
12-22 Copyright ‫س‬ Oracle Corporation, 1999. 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 (SELECT [column_list] FROM table
3 ORDER BY Top-N_column)
4 WHERE ROWNUM <= N
12-23 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Example of “Top-N” Analysis
To display the top three earners names
and salaries from the EMP table.
SQL> SELECT
2 FROM (SELECT ename,sal FROM emp
3 ORDER BY sal DESC)
4 WHERE ROWNUM <= 3;
ROWNUM as RANK, sal
ename,
RANK
---------
1
2
3
ENAME
----------
KING
SCOTT
FORD
SAL
---------
5000
3000
3000
12-24 Copyright ‫س‬ Oracle Corporation, 1999. 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-25 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved.
Summary
• An inline view is a subquery with an alias
name.
• “Top-N” analysis can be done using:
– Subquery
– Outer query
12-26 Copyright ‫س‬ Oracle Corporation, 1999. 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

PPT
Les10[1]Creating and Managing Tables
PPT
Les01[1]Writing Basic SQL Statements
PPT
Les05[1]Aggregating Data Using Group Functions
PPT
Les03[1] Single-Row Functions
PPT
Les09[1]Manipulating Data
PPT
Les13[1]Other Database Objects
PPT
Les22[1]Advanced Explicit Cursor Concepts
PPT
Les06[1]Subqueries
Les10[1]Creating and Managing Tables
Les01[1]Writing Basic SQL Statements
Les05[1]Aggregating Data Using Group Functions
Les03[1] Single-Row Functions
Les09[1]Manipulating Data
Les13[1]Other Database Objects
Les22[1]Advanced Explicit Cursor Concepts
Les06[1]Subqueries

What's hot (20)

PPT
Les08[1] Producing Readable Output with SQL*Plus
PPT
Les02[1]Restricting and Sorting Data
PPT
Les16[1]Declaring Variables
PPT
Les11[1]Including Constraints
PPT
Les07[1]Multiple-Column Subqueries
PPT
Les17[1] Writing Executable Statements
PPT
Les18[1]Interacting with the Oracle Server
PPT
Les21[1]Writing Explicit Cursors
PPT
Single-Row Functions in orcale Data base
PPT
Les23[1]Handling Exceptions
PPT
Producing Readable Output with iSQL*Plus - Oracle Data Base
PPT
Sequences and indexes
PPT
Les19[1]Writing Control Structures
PPT
Including Constraints -Oracle Data base
PPT
Aggregating Data Using Group Functions
PPT
Displaying Data from Multiple Tables - Oracle Data Base
PPT
Writing Basic SQL SELECT Statements
PPT
Creating Views - oracle database
PPT
2 sql - single-row functions
PPT
Creating and Managing Tables -Oracle Data base
Les08[1] Producing Readable Output with SQL*Plus
Les02[1]Restricting and Sorting Data
Les16[1]Declaring Variables
Les11[1]Including Constraints
Les07[1]Multiple-Column Subqueries
Les17[1] Writing Executable Statements
Les18[1]Interacting with the Oracle Server
Les21[1]Writing Explicit Cursors
Single-Row Functions in orcale Data base
Les23[1]Handling Exceptions
Producing Readable Output with iSQL*Plus - Oracle Data Base
Sequences and indexes
Les19[1]Writing Control Structures
Including Constraints -Oracle Data base
Aggregating Data Using Group Functions
Displaying Data from Multiple Tables - Oracle Data Base
Writing Basic SQL SELECT Statements
Creating Views - oracle database
2 sql - single-row functions
Creating and Managing Tables -Oracle Data base
Ad

Similar to Les12[1]Creating Views (20)

PPTX
Oracle views
PPT
PPT
Sql views
PPT
PPT
SQL WORKSHOP::Lecture 12
PPT
e computer notes - Creating views
PPT
PDF
Lesson10
PPT
Creating other schema objects
PDF
Database Oracle Basic
PPT
PPT
Oracle view
PPT
chap 9 dbms.ppt
PPT
PPTX
Oracle Database View
PPTX
Designing and Creating Views, Inline Functions, and Synonyms
PPT
Chapter 8 Views in SQL-Introduction .ppt
Oracle views
Sql views
SQL WORKSHOP::Lecture 12
e computer notes - Creating views
Lesson10
Creating other schema objects
Database Oracle Basic
Oracle view
chap 9 dbms.ppt
Oracle Database View
Designing and Creating Views, Inline Functions, and Synonyms
Chapter 8 Views in SQL-Introduction .ppt
Ad

More from siavosh kaviani (14)

PDF
sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
PDF
Introduction-to-the-Lean-Canvas.pdf
PPTX
Attaque chimique contre les écolières en Iran version 2.pptx
PDF
Short CV BA.pdf
PDF
Faegh Omidi Resume.pdf
PDF
Short CV CTO version 2.pdf
PDF
Short CV Marketing version 2.pdf
PDF
Short CV prof version 2.pdf
PDF
Siavosh Kaviani cv francais 2022 version 2.pdf
PDF
SiavoshKaviani-CV[2021] francais.pdf
PPSX
apex security demo.ppsx
PPT
Les14[1]Controlling User Access
PPT
Les20[1]Working with Composite Datatypes
PPT
Les15[1]SQL Workshop
sara-shortCV SARA GHIASI TABRIZI Computer Science PhD Application
Introduction-to-the-Lean-Canvas.pdf
Attaque chimique contre les écolières en Iran version 2.pptx
Short CV BA.pdf
Faegh Omidi Resume.pdf
Short CV CTO version 2.pdf
Short CV Marketing version 2.pdf
Short CV prof version 2.pdf
Siavosh Kaviani cv francais 2022 version 2.pdf
SiavoshKaviani-CV[2021] francais.pdf
apex security demo.ppsx
Les14[1]Controlling User Access
Les20[1]Working with Composite Datatypes
Les15[1]SQL Workshop

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
01-Introduction-to-Information-Management.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Institutional Correction lecture only . . .
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Cell Types and Its function , kingdom of life
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
master seminar digital applications in india
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Insiders guide to clinical Medicine.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Cell Structure & Organelles in detailed.
Renaissance Architecture: A Journey from Faith to Humanism
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Module 4: Burden of Disease Tutorial Slides S2 2025
01-Introduction-to-Information-Management.pdf
VCE English Exam - Section C Student Revision Booklet
Institutional Correction lecture only . . .
Supply Chain Operations Speaking Notes -ICLT Program
PPH.pptx obstetrics and gynecology in nursing
Cell Types and Its function , kingdom of life
Anesthesia in Laparoscopic Surgery in India
O7-L3 Supply Chain Operations - ICLT Program
master seminar digital applications in india
102 student loan defaulters named and shamed – Is someone you know on the list?
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Insiders guide to clinical Medicine.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

Les12[1]Creating Views

  • 1. Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. 12 Creating Views
  • 2. 12-2 Copyright ‫س‬ Oracle Corporation, 1999. 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
  • 3. 12-3 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Objectives After completing this lesson, you should be able to do the following: • Describe an inline view • Perform “Top-N” Analysis
  • 4. 12-4 Copyright ‫س‬ Oracle Corporation, 1999. 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
  • 5. 12-5 Copyright ‫س‬ Oracle Corporation, 1999. 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 EMP 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 EMPNO ENAME JOB ------ -------- ----------- 7839 KING PRESIDENT 7782 CLARK MANAGER 7934 MILLER CLERK EMPVU10 View
  • 6. 12-6 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Why Use Views? • To restrict data access • To make complex queries easy • To allow data independence • To present different views of the same data
  • 7. 12-7 Copyright ‫س‬ Oracle Corporation, 1999. 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
  • 8. 12-8 Copyright ‫س‬ Oracle Corporation, 1999. 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];
  • 9. 12-9 Copyright ‫س‬ Oracle Corporation, 1999. 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 empvu10 SQL> CREATE VIEW empvu10 2 AS SELECT empno, ename, job 3 FROM emp 4 WHERE deptno = 10; View created.
  • 10. 12-10 Copyright ‫س‬ Oracle Corporation, 1999. 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 created.
  • 11. 12-11 Copyright ‫س‬ Oracle Corporation, 1999. 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. SQL> SELECT * 2 FROM salvu30;
  • 12. 12-12 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Querying a View USER_VIEWS EMPVU10 SELECT empno, ename, job FROM emp WHERE deptno = 10; SQL*Plus SELECT * FROM empvu10; EMP 7839 KING PRESIDENT 7782 CLARK MANAGER 7934 MILLER CLERK
  • 13. 12-13 Copyright ‫س‬ Oracle Corporation, 1999. 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 created.
  • 14. 12-14 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Creating a Complex View Create a complex view that contains group functions 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 created.
  • 15. 12-15 Copyright ‫س‬ Oracle Corporation, 1999. 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 – The pseudocolumn ROWNUM keyword
  • 16. 12-16 Copyright ‫س‬ Oracle Corporation, 1999. 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
  • 17. 12-17 Copyright ‫س‬ Oracle Corporation, 1999. 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 created.
  • 18. 12-18 Copyright ‫س‬ Oracle Corporation, 1999. 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 created. • Any attempt to perform a DML on any row in the view will result in Oracle Server error.
  • 19. 12-19 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Removing a View Remove a view without losing data because a view is based on underlying tables in the database. SQL> DROP VIEW empvu10; View dropped. DROP VIEW view;
  • 20. 12-20 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Inline Views • An inline view is a subquery with an alias (correlation name) that you can use within a SQL statement. • An inline view is similar to using a named subquery in the FROM clause of the main query. • An inline view is not a schema object.
  • 21. 12-21 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. “Top-N” Analysis • Top-N queries ask for the n largest or smallest values of a column. – What are the ten best selling products? – What are the ten worst selling products ? • Both largest values and smallest values sets are considered Top-N queries.
  • 22. 12-22 Copyright ‫س‬ Oracle Corporation, 1999. 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 (SELECT [column_list] FROM table 3 ORDER BY Top-N_column) 4 WHERE ROWNUM <= N
  • 23. 12-23 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Example of “Top-N” Analysis To display the top three earners names and salaries from the EMP table. SQL> SELECT 2 FROM (SELECT ename,sal FROM emp 3 ORDER BY sal DESC) 4 WHERE ROWNUM <= 3; ROWNUM as RANK, sal ename, RANK --------- 1 2 3 ENAME ---------- KING SCOTT FORD SAL --------- 5000 3000 3000
  • 24. 12-24 Copyright ‫س‬ Oracle Corporation, 1999. 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
  • 25. 12-25 Copyright ‫س‬ Oracle Corporation, 1999. All rights reserved. Summary • An inline view is a subquery with an alias name. • “Top-N” analysis can be done using: – Subquery – Outer query
  • 26. 12-26 Copyright ‫س‬ Oracle Corporation, 1999. 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