Copyright س Oracle Corporation, 1998. All rights reserved. 12 Creating Views.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
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
Restricting and sorting data 16 May May May Created By Pantharee Sawasdimongkol.
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
1Eyad Alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
SQL: Part 4 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
Copyright  Oracle Corporation, All rights reserved. 6 Subqueries.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
10 Copyright © 2004, Oracle. All rights reserved. Creating Other Schema Objects.
Copyright  Oracle Corporation, All rights reserved. 9 Manipulating Data: INSERT, UPDATE, DELETE.
Session 3: SQL (B): Parts 3 & 4 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
o At the end of this lesson, you will be able to:  Describe the life-cycle development phases  Discuss the theoretical and physical aspects of a relational.
Copyright  Oracle Corporation, All rights reserved. I Introduction.
4-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language (DML)
Copyright  Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Dr. Philip Cannata 1 Programming Languages Prolog Part 3 SQL & Prolog.
Copyright © 2004, Oracle. All rights reserved. Lecture 3: Creating Other Schema Objects Lecture 3: Creating Other Schema Objects ORACLE.
11 Copyright © 2007, Oracle. All rights reserved. Creating Other Schema Objects.
Displaying Data from Multiple Tables (Join). EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Relational Databases &
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
Subqueries.
Subqueries.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
7 Multiple-Column Subqueries. 7-2 Objectives At the end of this lesson, you should be able to: Write a multiple-column subquery Describe and explain the.
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.
11 Copyright © Oracle Corporation, All rights reserved. Creating Views.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
Copyright  Oracle Corporation, All rights reserved. Introduction.
Copyright  Oracle Corporation, All rights reserved. 4 Introduction.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
Manipulating Data. Objectives After completing this lesson, you should be able to do the following: Describe each DML statement Insert rows into a table.
9 Manipulating Data. 9-2 Objectives At the end of this lesson, you should be able to: Describe each DML statement Insert rows into a table Update rows.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
Copyright  Oracle Corporation, All rights reserved. 8 Producing Readable Output with SQL*Plus.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
2-1 Limiting Rows Using a Selection “…retrieve all employees in department 10” EMP EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER.
6 Subqueries. 6-2 Objectives At the end of this lesson, you should be able to: Describe the types of problems that subqueries can solve Define subqueries.
Creating and Managing Tables 14. ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson,
Creating Views Database Systems Objectives Explain the concept of a view. Create simple and complex views. Retrieve data through a view. Alter the.
1-1 Copyright  Oracle Corporation, All rights reserved. Logging In to SQL*Plus From Windows environment:From Windows environment: From command line:From.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join Displaying Data from.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
Communicating with a RDBMS Using SQL Database SQL> SELECT loc 2 FROM dept; SQL> SELECT loc 2 FROM dept; SQL statement is entered Statement is sent to database.
Relational Normalization Theory
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Creating Views Schedule: Timing Topic 20 minutes Lecture
Creating Other Schema Objects
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
Creating Other Schema Objects
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Chapter 2 Views.
Chapter 2 Views.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Restricting and Sorting Data
IST 318 Database Administration
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

Copyright س Oracle Corporation, All rights reserved. 12 Creating Views

12-2 Copyright س Oracle Corporation, 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 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, 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, All rights reserved. What Is a View? EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO KING PRESIDENT 17-NOV BLAKE MANAGER MAY CLARK MANAGER JUN JONES MANAGER APR MARTIN SALESMAN SEP ALLEN SALESMAN FEB TURNER SALESMAN SEP JAMES CLERK DEC WARD SALESMAN FEB FORD ANALYST DEC SMITH CLERK DEC SCOTT ANALYST DEC ADAMS CLERK JAN MILLER CLERK JAN EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO KING PRESIDENT 17-NOV BLAKE MANAGER MAY CLARK MANAGER JUN JONES MANAGER APR MARTIN SALESMAN SEP ALLEN SALESMAN FEB TURNER SALESMAN SEP JAMES CLERK DEC WARD SALESMAN FEB FORD ANALYST DEC SMITH CLERK DEC SCOTT ANALYST DEC ADAMS CLERK JAN MILLER CLERK JAN EMP Table EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO KING PRESIDENT 17-NOV CLARK MANAGER JUN MILLER CLERK JAN JONES MANAGER APR SCOTT ANALYST DEC ADAMS CLERK JAN SMITH CLERK DEC FORD ANALYST DEC BLAKE MANAGER MAY MARTIN SALESMAN SEP ALLEN SALESMAN FEB TURNER SALESMAN SEP JAMES CLERK DEC WARD SALESMAN FEB EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO KING PRESIDENT 17-NOV CLARK MANAGER JUN MILLER CLERK JAN JONES MANAGER APR SCOTT ANALYST DEC ADAMS CLERK JAN SMITH CLERK DEC FORD ANALYST DEC BLAKE MANAGER MAY MARTIN SALESMAN SEP ALLEN SALESMAN FEB TURNER SALESMAN SEP JAMES CLERK DEC WARD SALESMAN FEB EMPNO ENAME JOB KING PRESIDENT 7782 CLARK MANAGER 7934 MILLER CLERK EMPVU10 View EMPVU10 View

12-5 Copyright س Oracle Corporation, 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 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, All rights reserved. Simple Views and Complex Views FeatureSimple ViewsComplex Views Number of tablesOneOne or more Contain functionsNoYes Contain groups of dataNoYes DML through viewYesNot always FeatureSimple ViewsComplex Views Number of tablesOneOne or more Contain functionsNoYes Contain groups of dataNoYes DML through viewYesNot always

12-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. The subquery cannot contain an ORDER BY clause. 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, 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 SELECTempno, ename, job 3 FROMemp 4 WHEREdeptno = 10; View created.

12-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 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 SELECTempno EMPLOYEE_NUMBER, ename NAME, 3sal SALARY 4 FROMemp 5 WHEREdeptno = 30; View created.

12-10 Copyright س Oracle Corporation, All rights reserved. Retrieving Data from a View EMPLOYEE_NUMBER NAME SALARY BLAKE MARTIN ALLEN TURNER JAMES WARD rows selected. EMPLOYEE_NUMBER NAME SALARY BLAKE MARTIN ALLEN TURNER JAMES WARD rows selected. SQL> SELECT * 2 FROMsalvu30;

12-11 Copyright س Oracle Corporation, All rights reserved. Querying a View USER_VIEWS USER_VIEWS EMPVU10 SELECTempno, ename, job FROMemp WHEREdeptno = 10; USER_VIEWS USER_VIEWS EMPVU10 SELECTempno, ename, job FROMemp WHEREdeptno = 10; SQL*Plus SELECT * FROM empvu10; EMP 7839KINGPRESIDENT 7782CLARKMANAGER 7934MILLERCLERK

12-12 Copyright س Oracle Corporation, 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. 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 FROMemp 5 WHEREdeptno = 10; View created.

12-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. SQL> CREATE VIEWdept_sum_vu 2 (name, minsal, maxsal, avgsal) 3 AS SELECTd.dname, MIN(e.sal), MAX(e.sal), 4AVG(e.sal) 5 FROMemp e, dept d 6 WHEREe.deptno = d.deptno 7 GROUP BY d.dname; View created.

12-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 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, 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 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, 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-17 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. SQL> CREATE OR REPLACE VIEW empvu10 2 (employee_number, employee_name, job_title) 3 AS SELECTempno, ename, job 4 FROMemp 5 WHEREdeptno = 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-18 Copyright س Oracle Corporation, 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-19 Copyright س Oracle Corporation, 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 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, 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 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

12-21 Copyright س Oracle Corporation, All rights reserved. GO TO LECTURE #13

12-22 Copyright س Oracle Corporation, All rights reserved.