11 Copyright © Oracle Corporation, 2001. All rights reserved. Creating Views.

Slides:



Advertisements
Similar presentations
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
11-1 Copyright © Oracle Corporation, All rights reserved. Different type of keys.
12-1 Copyright  Oracle Corporation, All rights reserved. What Is a View? EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
SQL: Part 4 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
Database Programming Sections 13. Marge Hohly  1. Which statements are True about the following sequence? The sequence was used to generate numbers.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
18 Copyright © Oracle Corporation, All rights reserved. Advanced Subqueries.
1 Lecture 3: Subqueries DCO11310 Database Systems and Design By Rose Chang.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
10 Copyright © 2004, Oracle. All rights reserved. Creating Other Schema Objects.
Introduction to Structured Query Language (SQL)
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Copyright س Oracle Corporation, All rights reserved. 13 Other Database Objects.
View and Materialized view. What is a view? Logically represents subset of data from one or more table. In sql, a view is a virtual relation based on.
Introduction to Oracle9i: SQL1 Views. Introduction to Oracle9i: SQL2 Chapter Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE.
Chapter 6 Additional Database Objects
Objectives After completing this lesson, you should be able to do the following: Categorize the main database objects Review the table structure List.
Copyright © 2004, Oracle. All rights reserved. Lecture 3: Creating Other Schema Objects Lecture 3: Creating Other Schema Objects ORACLE.
12 Copyright © Oracle Corporation, All rights reserved. Other Database Objects.
11 Copyright © 2007, Oracle. All rights reserved. Creating Other Schema Objects.
Chapter 6 Additional Database Objects Oracle 10g: SQL.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
10-1 Copyright  Oracle Corporation, All rights reserved. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns.
Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
Chapter 4 Indexes. Indexes Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage; composed.
DDL and Views. Database Objects Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage;
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Creating Views Database Systems Objectives Explain the concept of a view. Create simple and complex views. Retrieve data through a view. Alter the.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
 CONACT UC:  Magnific training   
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
SQL Creating and Managing Tables
Creating Views Schedule: Timing Topic 20 minutes Lecture
Creating Other Schema Objects
SQL Creating and Managing Tables
Creating Other Schema Objects
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
SQL Creating and Managing Tables
Chapter 4 Indexes.
CH 4 Indexes.
Chapter 2 Views.
Database Programming Sections 11 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
CH 4 Indexes.
Chapter 2 Views.
Contents Preface I Introduction Lesson Objectives I-2
IST 318 Database Administration
Presentation transcript:

11 Copyright © Oracle Corporation, All rights reserved. Creating Views

11-2 Copyright © Oracle Corporation, All rights reserved. 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 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

11-3 Copyright © Oracle Corporation, 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

11-4 Copyright © Oracle Corporation, All rights reserved. What is a View? EMPLOYEES Table:

11-5 Copyright © Oracle Corporation, All rights reserved. Why Use Views? To restrict data access To make complex queries easy To provide data independence To present different views of the same data To restrict data access To make complex queries easy To provide data independence To present different views of the same data

11-6 Copyright © Oracle Corporation, All rights reserved. Simple Views and Complex Views FeatureSimple ViewsComplex Views Number of tablesOneOne or more Contain functionsNoYes Contain groups of dataNoYes DML operations through a viewYesNot always

11-7 Copyright © Oracle Corporation, All rights reserved. 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]]; CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [(alias[, alias]...)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READ ONLY [CONSTRAINT constraint]];

11-8 Copyright © Oracle Corporation, All rights reserved. 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.

11-9 Copyright © Oracle Corporation, 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. CREATE VIEW salvu50 AS SELECT employee_id ID_NUMBER, last_name NAME, salary*12 ANN_SALARY FROM employees WHERE department_id = 50; View created.

11-10 Copyright © Oracle Corporation, All rights reserved. Retrieving Data from a View SELECT * FROMsalvu50;

11-11 Copyright © Oracle Corporation, All rights reserved. Querying a View USER_VIEWS USER_VIEWS EMPVU80 SELECT employee_id, last_name, salary FROM employees WHERE department_id=80; i SQL*Plus SELECT * FROM empvu80 ; EMPLOYEES Oracle Server

11-12 Copyright © Oracle Corporation, All rights reserved. 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.

11-13 Copyright © Oracle Corporation, All rights reserved. Creating a Complex View Create a complex view that contains group functions to display values from two tables. CREATE VIEWdept_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.

11-14 Copyright © Oracle Corporation, 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

11-15 Copyright © Oracle Corporation, All rights reserved. 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

11-16 Copyright © Oracle Corporation, All rights reserved. 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

11-17 Copyright © Oracle Corporation, All rights reserved. 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. CREATE OR REPLACE VIEW empvu20 AS SELECT* FROM employees WHERE department_id = 20 WITH CHECK OPTION CONSTRAINT empvu20_ck ; View created. Using the WITH CHECK OPTION Clause

11-18 Copyright © Oracle Corporation, 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. Any attempt to perform a DML on any row in the view results in an Oracle server error.

11-19 Copyright © Oracle Corporation, All rights reserved. Denying DML Operations CREATE OR REPLACE VIEW empvu10 (employee_number, employee_name, job_title) AS SELECTemployee_id, last_name, job_id FROM employees WHERE department_id = 10 WITH READ ONLY; View created.

11-20 Copyright © Oracle Corporation, All rights reserved. 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;

11-21 Copyright © Oracle Corporation, All rights reserved. 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. 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.

11-22 Copyright © Oracle Corporation, All rights reserved. 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. 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.

11-23 Copyright © Oracle Corporation, All rights reserved. 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; SELECT [column_list], ROWNUM FROM (SELECT [column_list] FROM table ORDER BY Top-N_column) WHERE ROWNUM <= N;

11-24 Copyright © Oracle Corporation, All rights reserved. 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;

11-25 Copyright © Oracle Corporation, All rights reserved. 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.

11-26 Copyright © Oracle Corporation, All rights reserved. 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

11-27 Copyright © Oracle Corporation, All rights reserved.

11-28 Copyright © Oracle Corporation, All rights reserved.