SlideShare a Scribd company logo
Creating Views
Objectives After completing this lesson, you should be able  to do the following: Describe a view  Create, alter the definition of, and drop a view Retrieve data through a view Insert, update, and delete data through a view Create and use an inline view Perform “Top-N” analysis
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
What is a View? EMPLOYEES  Table:
Why Use Views? To restrict data access To make complex queries easy To provide data independence To present different views of the same data
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 operations through  a view Yes Not always
Creating a View You embed a subquery within the  CREATE VIEW  statement. The subquery can contain complex  SELECT  syntax. CREATE [OR REPLACE] [FORCE| NOFORCE ] VIEW  view [( alias [,  alias ]...)] AS  subquery [WITH CHECK OPTION [CONSTRAINT  constraint ]] [WITH READ ONLY [CONSTRAINT  constraint ]];
Creating a View Create a view,  EMPVU80 , that contains details of employees in department 80. Describe the structure of the view by using the  i SQL*Plus  DESCRIBE  command. DESCRIBE empvu80 CREATE VIEW  empvu80 AS SELECT  employee_id, last_name, salary FROM  employees WHERE  department_id = 80; View created.
Creating a View Create a view by using column aliases in the subquery. Select the columns from this view by the given alias names. CREATE VIEW  salvu50 AS SELECT  employee_id ID_NUMBER, last_name NAME, salary*12 ANN_SALARY FROM  employees WHERE  department_id = 50; View created.
Retrieving Data from a View SELECT * FROM salvu50;
Querying a View i SQL*Plus SELECT  * FROM  empvu80 ; Oracle Server USER_VIEWS   EMPVU80 SELECT employee_id,  last_name, salary FROM  employees WHERE  department_id=80; EMPLOYEES
Modifying a View Modify the  EMPVU80  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. CREATE OR REPLACE VIEW empvu80 (id_number, name, sal, department_id) AS SELECT  employee_id, first_name || ' ' || last_name,  salary, department_id FROM  employees WHERE  department_id = 80; View created.
Creating a Complex View Create a complex view that contains group functions to display values from two tables. CREATE VIEW dept_sum_vu (name, minsal, maxsal, avgsal) AS SELECT  d.department_name, MIN(e.salary),  MAX(e.salary),AVG(e.salary) FROM  employees e, departments d WHERE  e.department_id = d.department_id  GROUP BY  d.department_name; View created.
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
Rules for Performing  DML Operations on a View You cannot modify data in a view if it contains: Group functions A  GROUP BY  clause The  DISTINCT  keyword The pseudocolumn  ROWNUM  keyword Columns defined by expressions
Rules for Performing  DML Operations on a View You cannot add data through a view if the view  includes: Group functions A  GROUP BY  clause The  DISTINCT  keyword The pseudocolumn  ROWNUM  keyword Columns defined by expressions NOT NULL  columns in the base tables that are not selected by the view
You can ensure that DML operations performed on the view stay 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 fails because it violates the  WITH CHECK OPTION  constraint. Using the  WITH   CHECK   OPTION  Clause CREATE OR REPLACE VIEW empvu20 AS SELECT * FROM  employees WHERE  department_id = 20 WITH CHECK OPTION CONSTRAINT empvu20_ck ; View created.
Denying DML Operations You can ensure that no DML operations occur by adding the  WITH READ ONLY  option to your view definition. Any attempt to perform a DML on any row in the view results in an Oracle server error.
Denying DML Operations CREATE OR REPLACE VIEW empvu10 (employee_number, employee_name, job_title) AS SELECT employee_id, last_name, job_id FROM  employees WHERE  department_id = 10 WITH READ ONLY; View created.
Removing a View You can remove a view without losing data because a view is based on underlying tables in the database. DROP VIEW empvu80; View dropped. DROP VIEW  view ;
Inline Views An inline view is a subquery with an alias (or correlation name) that you can use within a SQL statement.  A named subquery in the  FROM  clause of the main query is an example of an inline view. An inline view is not a schema object.
Top-N Analysis Top-N queries ask for the  n  largest or smallest values of a column. For example: 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.
Performing Top-N Analysis The high-level structure of a Top-N analysis  query is:  SELECT [ column_list ], ROWNUM  FROM  (SELECT [ column_list ]  FROM table ORDER  BY Top-N_column) WHERE  ROWNUM <=  N;
Example of Top-N Analysis To display the top three earner names and salaries from the  EMPLOYEES  table: SELECT ROWNUM as RANK, last_name, salary  FROM  (SELECT last_name,salary FROM employees ORDER BY salary DESC) WHERE ROWNUM <= 3; 3 1 2 1 2 3
Summary In this lesson, you should have learned that a view is  derived from data in other tables or views and provides the following advantages: Restricts database access Simplifies queries Provides data independence Provides multiple views of the same data Can be dropped without removing the underlying data An inline view is a subquery with an alias name. Top-N analysis can be done using subqueries and outer queries.
Practice 11 Overview This practice covers the following topics: 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
Les01
Sudharsan S
 
PPT
Creating other schema objects
Syed Zaid Irshad
 
PPT
Les18
Vijay Kumar
 
PPT
Aggregate Functions,Final
mukesh24pandey
 
PPT
Les02 (restricting and sorting data)
Achmad Solichin
 
PPTX
Oracle: Basic SQL
DataminingTools Inc
 
Les01
Sudharsan S
 
Creating other schema objects
Syed Zaid Irshad
 
Les18
Vijay Kumar
 
Aggregate Functions,Final
mukesh24pandey
 
Les02 (restricting and sorting data)
Achmad Solichin
 
Oracle: Basic SQL
DataminingTools Inc
 

What's hot (18)

PPT
Les09 (using ddl statements to create and manage tables)
Achmad Solichin
 
PPT
Les17
Vijay Kumar
 
PPTX
Oracle: Functions
DataminingTools Inc
 
PPT
Excell vba
Heru Cahyo
 
PDF
Introduction to oracle functions
Nitesh Singh
 
PDF
Database Systems - SQL - DDL Statements (Chapter 3/3)
Vidyasagar Mundroy
 
PPTX
View
Pooja Dixit
 
PPT
Les10
Vijay Kumar
 
PPT
SQL select statement and functions
Vikas Gupta
 
PPT
Les01 (retrieving data using the sql select statement)
Achmad Solichin
 
PDF
Nested Queries Lecture
Felipe Costa
 
PDF
Updat Dir
guest319770
 
PPTX
1. dml select statement reterive data
Amrit Kaur
 
PPT
Les07
Vijay Kumar
 
PPTX
Lab1 select statement
Balqees Al.Mubarak
 
PPT
Creating Views - oracle database
Salman Memon
 
PPT
Les02
Sudharsan S
 
PPT
Retrieving data using the sql select statement
Syed Zaid Irshad
 
Les09 (using ddl statements to create and manage tables)
Achmad Solichin
 
Les17
Vijay Kumar
 
Oracle: Functions
DataminingTools Inc
 
Excell vba
Heru Cahyo
 
Introduction to oracle functions
Nitesh Singh
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Vidyasagar Mundroy
 
View
Pooja Dixit
 
Les10
Vijay Kumar
 
SQL select statement and functions
Vikas Gupta
 
Les01 (retrieving data using the sql select statement)
Achmad Solichin
 
Nested Queries Lecture
Felipe Costa
 
Updat Dir
guest319770
 
1. dml select statement reterive data
Amrit Kaur
 
Les07
Vijay Kumar
 
Lab1 select statement
Balqees Al.Mubarak
 
Creating Views - oracle database
Salman Memon
 
Les02
Sudharsan S
 
Retrieving data using the sql select statement
Syed Zaid Irshad
 
Ad

Similar to Les11 (20)

PPT
Les12[1]Creating Views
siavosh kaviani
 
PPT
e computer notes - Creating views
ecomputernotes
 
PPT
Sql views
arshid045
 
PPT
Les10
Sudharsan S
 
PPT
Les12
arnold 7490
 
PPT
Les11.ppt
AlhassanFederated
 
PDF
Les10
Abrianto Nugraha
 
PPT
SQL WORKSHOP::Lecture 12
Umair Amjad
 
PPTX
Oracle views
Balqees Al.Mubarak
 
PDF
Lesson10
renguzi
 
PDF
Database Oracle Basic
Kamlesh Singh
 
PPT
chap 9 dbms.ppt
arjun431527
 
PDF
Sql ch 13 - sql-views
Mukesh Tekwani
 
PPT
Views
Rahul Gupta
 
PDF
6232 b 04
Portal_do_Estudante_SQL
 
PPTX
Designing and Creating Views, Inline Functions, and Synonyms
Tayba Farooqui
 
PPT
Oracle view
Madhavendra Dutt
 
PPT
Chapter 8 Views in SQL-Introduction .ppt
sivamathi12
 
PPT
Oracle training institute in hyderabad
appaji intelhunt
 
Les12[1]Creating Views
siavosh kaviani
 
e computer notes - Creating views
ecomputernotes
 
Sql views
arshid045
 
Les10
Sudharsan S
 
Les12
arnold 7490
 
Les11.ppt
AlhassanFederated
 
SQL WORKSHOP::Lecture 12
Umair Amjad
 
Oracle views
Balqees Al.Mubarak
 
Lesson10
renguzi
 
Database Oracle Basic
Kamlesh Singh
 
chap 9 dbms.ppt
arjun431527
 
Sql ch 13 - sql-views
Mukesh Tekwani
 
Views
Rahul Gupta
 
Designing and Creating Views, Inline Functions, and Synonyms
Tayba Farooqui
 
Oracle view
Madhavendra Dutt
 
Chapter 8 Views in SQL-Introduction .ppt
sivamathi12
 
Oracle training institute in hyderabad
appaji intelhunt
 
Ad

More from Vijay Kumar (14)

PPT
Les20
Vijay Kumar
 
PPT
Les19
Vijay Kumar
 
PPT
Les15
Vijay Kumar
 
PPT
Les16
Vijay Kumar
 
PPT
Les14
Vijay Kumar
 
PPT
Les13
Vijay Kumar
 
PPT
Les12
Vijay Kumar
 
PPT
Les09
Vijay Kumar
 
PPT
Les08
Vijay Kumar
 
PPT
Les06
Vijay Kumar
 
PPT
Les05
Vijay Kumar
 
PPT
Les04
Vijay Kumar
 
PPT
Les03
Vijay Kumar
 
PPT
Les02
Vijay Kumar
 
Les20
Vijay Kumar
 
Les19
Vijay Kumar
 
Les15
Vijay Kumar
 
Les16
Vijay Kumar
 
Les14
Vijay Kumar
 
Les13
Vijay Kumar
 
Les12
Vijay Kumar
 
Les09
Vijay Kumar
 
Les08
Vijay Kumar
 
Les06
Vijay Kumar
 
Les05
Vijay Kumar
 
Les04
Vijay Kumar
 
Les03
Vijay Kumar
 
Les02
Vijay Kumar
 

Recently uploaded (20)

PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Doc9.....................................
SofiaCollazos
 
Software Development Methodologies in 2025
KodekX
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 

Les11

  • 2. Objectives After completing this lesson, you should be able to do the following: Describe a view Create, alter the definition of, and drop a view Retrieve data through a view Insert, update, and delete data through a view Create and use an inline view Perform “Top-N” analysis
  • 3. 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. What is a View? EMPLOYEES Table:
  • 5. Why Use Views? To restrict data access To make complex queries easy To provide data independence To present different views of the same data
  • 6. 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 operations through a view Yes Not always
  • 7. Creating a View You embed a subquery within the CREATE VIEW statement. The subquery can contain complex SELECT syntax. CREATE [OR REPLACE] [FORCE| NOFORCE ] VIEW view [( alias [, alias ]...)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint ]] [WITH READ ONLY [CONSTRAINT constraint ]];
  • 8. Creating a View Create a view, EMPVU80 , that contains details of employees in department 80. Describe the structure of the view by using the i SQL*Plus DESCRIBE command. DESCRIBE empvu80 CREATE VIEW empvu80 AS SELECT employee_id, last_name, salary FROM employees WHERE department_id = 80; View created.
  • 9. Creating a View Create a view by using column aliases in the subquery. Select the columns from this view by the given alias names. CREATE VIEW salvu50 AS SELECT employee_id ID_NUMBER, last_name NAME, salary*12 ANN_SALARY FROM employees WHERE department_id = 50; View created.
  • 10. Retrieving Data from a View SELECT * FROM salvu50;
  • 11. Querying a View i SQL*Plus SELECT * FROM empvu80 ; Oracle Server USER_VIEWS EMPVU80 SELECT employee_id, last_name, salary FROM employees WHERE department_id=80; EMPLOYEES
  • 12. Modifying a View Modify the EMPVU80 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. CREATE OR REPLACE VIEW empvu80 (id_number, name, sal, department_id) AS SELECT employee_id, first_name || ' ' || last_name, salary, department_id FROM employees WHERE department_id = 80; View created.
  • 13. Creating a Complex View Create a complex view that contains group functions to display values from two tables. CREATE VIEW dept_sum_vu (name, minsal, maxsal, avgsal) AS SELECT d.department_name, MIN(e.salary), MAX(e.salary),AVG(e.salary) FROM employees e, departments d WHERE e.department_id = d.department_id GROUP BY d.department_name; View created.
  • 14. 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
  • 15. Rules for Performing DML Operations on a View You cannot modify data in a view if it contains: Group functions A GROUP BY clause The DISTINCT keyword The pseudocolumn ROWNUM keyword Columns defined by expressions
  • 16. Rules for Performing DML Operations on a View You cannot add data through a view if the view includes: Group functions A GROUP BY clause The DISTINCT keyword The pseudocolumn ROWNUM keyword Columns defined by expressions NOT NULL columns in the base tables that are not selected by the view
  • 17. You can ensure that DML operations performed on the view stay 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 fails because it violates the WITH CHECK OPTION constraint. Using the WITH CHECK OPTION Clause CREATE OR REPLACE VIEW empvu20 AS SELECT * FROM employees WHERE department_id = 20 WITH CHECK OPTION CONSTRAINT empvu20_ck ; View created.
  • 18. Denying DML Operations You can ensure that no DML operations occur by adding the WITH READ ONLY option to your view definition. Any attempt to perform a DML on any row in the view results in an Oracle server error.
  • 19. Denying DML Operations CREATE OR REPLACE VIEW empvu10 (employee_number, employee_name, job_title) AS SELECT employee_id, last_name, job_id FROM employees WHERE department_id = 10 WITH READ ONLY; View created.
  • 20. Removing a View You can remove a view without losing data because a view is based on underlying tables in the database. DROP VIEW empvu80; View dropped. DROP VIEW view ;
  • 21. Inline Views An inline view is a subquery with an alias (or correlation name) that you can use within a SQL statement. A named subquery in the FROM clause of the main query is an example of an inline view. An inline view is not a schema object.
  • 22. Top-N Analysis Top-N queries ask for the n largest or smallest values of a column. For example: 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.
  • 23. Performing Top-N Analysis The high-level structure of a Top-N analysis query is: SELECT [ column_list ], ROWNUM FROM (SELECT [ column_list ] FROM table ORDER BY Top-N_column) WHERE ROWNUM <= N;
  • 24. Example of Top-N Analysis To display the top three earner names and salaries from the EMPLOYEES table: SELECT ROWNUM as RANK, last_name, salary FROM (SELECT last_name,salary FROM employees ORDER BY salary DESC) WHERE ROWNUM <= 3; 3 1 2 1 2 3
  • 25. Summary In this lesson, you should have learned that a view is derived from data in other tables or views and provides the following advantages: Restricts database access Simplifies queries Provides data independence Provides multiple views of the same data Can be dropped without removing the underlying data An inline view is a subquery with an alias name. Top-N analysis can be done using subqueries and outer queries.
  • 26. Practice 11 Overview This practice covers the following topics: 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
  • 27.  
  • 28.  

Editor's Notes

  • #2: Schedule: Timing Topic 20 minutes Lecture 20 minutes Practice 40 minutes Total