Creating Views. 14-2 Database Systems Objectives Explain the concept of a view. Create simple and complex views. Retrieve data through a view. Alter the.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Advertisements

Manipulating Data Schedule: Timing Topic 60 minutes Lecture
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
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
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. 5 Aggregating Data Using Group Functions.
1Eyad alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
6 6 Subqueries Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published.
10 Copyright © 2004, Oracle. All rights reserved. Creating Other Schema Objects.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Copyright  Oracle Corporation, All rights reserved. 9 Manipulating Data: INSERT, UPDATE, DELETE.
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.
Session 3: SQL (B): Parts 3 & 4 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Introduction to Oracle9i: SQL1 Views. Introduction to Oracle9i: SQL2 Chapter Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE.
4-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language (DML)
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.
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.
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.
SQL (DDL & DML Commands)
Program with PL/SQL. Interacting with the Oracle Server.
Subqueries.
Subqueries.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
Copyright س Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
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.
Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
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. 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.
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.
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.
Creating Views. 2 home back first prev next last What Will I Learn? List three uses for views from the standpoint of a database administrator Explain,
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
DDL and Views. Database Objects Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage;
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.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: –Identify the available group.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Aggregating Data Using Group Functions
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Manipulating Data.
Creating Views Schedule: Timing Topic 20 minutes Lecture
Creating Other Schema Objects
Aggregating Data Using Group Functions
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
Interacting with the Oracle Server
Creating Other Schema Objects
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Chapter 2 Views.
“Manipulating Data” Lecture 6.
Database Programming Sections 11 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
(SQL) Manipulating Data
“Manipulating Data” Lecture 6.
Chapter 2 Views.
Subqueries Schedule: Timing Topic 25 minutes Lecture
IST 318 Database Administration
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

Creating Views

14-2 Database Systems Objectives Explain the concept of a view. Create simple and complex views. Retrieve data through a view. Alter the definition of a view. Insert, Update, Delete data through a view. Drop a view.

14-3 Database Systems ID LAST_NAME FIRST_NAME TITLE DEPT_ID Velasquez Carmen President 50 2 Ngao LaDoris VP, Operations 41 3 Nagayama Midori VP, Sales 31 4 Quick-To-See Mark VP, Finance 10 5 Ropeburn Audry VP, Administration 50 6 Urguhart Molly Warehouse Manager 41 7 Menchu Roberta Warehouse Manager 42 8 Biri Ben Warehouse Manager 43 9 Catchpole Antoinette Warehouse Manager Havel Marta Warehouse Manager Magee Colin Sales Representative Giljum Henry Sales Representative Sedeghi Yasmin Sales Representative Nguyen Mai Sales Representative Dumas Andre Sales Representative Maduro Elena Stock Clerk Smith George Stock Clerk Nozaki Akira Stock Clerk Patel Vikram Stock Clerk Newman Chad Stock Clerk Markarian Alexander Stock Clerk Chang Eddie Stock Clerk Patel Radha Stock Clerk Dancs Bela Stock Clerk Schwartz Sylvie Stock Clerk 45 What Is a View? ID LAST_NAME TITLE Havel Warehouse Manager 24 Dancs Stock Clerk 25 Schwartz Stock Clerk EMP Table EMPVU45 View

14-4 Database Systems Why Use Views? To restrict database access To make complex queries easy To allow data independence To present different views of the same data

14-5 Database Systems Simple Views and Complex Views Number of tables Contain functions Contain groups of data DML through view Complex Views One or more YesYes Not always Simple Views OneNoNoYes

14-6 Database Systems Creating a View: Syntax 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]...)] [(alias[, alias]...)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READ ONLY] CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [(alias[, alias]...)] [(alias[, alias]...)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READ ONLY]

14-7 Database Systems Creating a View: Syntax OR REPLACE – re-create the view if it already exists. FORCE – create the view regardless whether the base table exists or not. WITH CHECK OPTION – specifies that only rows accessible to the view can be inserted or updated. WITH READ ONLY – no DML can be performed. CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [(alias[, alias]...)] [(alias[, alias]...)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READ ONLY] CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [(alias[, alias]...)] [(alias[, alias]...)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READ ONLY]

14-8 Database Systems Creating a View: Example Create the EMPVU10 view, which contains the employee number, last name, and job title for employees in department 10. Describe the structure of the view by using the SQL*Plus DESCRIBE command. Display data from the view by entering a SELECT statement against the view. SQL> CREATE VIEW empvu10 2 AS SELECTempno, ename, job 2 AS SELECTempno, ename, job 3 FROMemp 3 FROMemp 4 WHEREdeptno = 10; 4 WHEREdeptno = 10; View created. SQL> CREATE VIEW empvu10 2 AS SELECTempno, ename, job 2 AS SELECTempno, ename, job 3 FROMemp 3 FROMemp 4 WHEREdeptno = 10; 4 WHEREdeptno = 10; View created.

14-9 Database Systems Creating a View: Example Create a view by using column aliases in the subquery. Select the columns from this view by the given alias name. SQL> CREATE VIEW salvu30 2 AS SELECTempno EMPLOYEE_NUMBER, ename FIRST_NAME, 2 AS SELECTempno EMPLOYEE_NUMBER, ename FIRST_NAME, 3 sal MONTHLY_SALARY 3 sal MONTHLY_SALARY 4 FROMemp 4 FROMemp 5 WHEREdeptno = 30; 5 WHEREdeptno = 30; View created. SQL> CREATE VIEW salvu30 2 AS SELECTempno EMPLOYEE_NUMBER, ename FIRST_NAME, 2 AS SELECTempno EMPLOYEE_NUMBER, ename FIRST_NAME, 3 sal MONTHLY_SALARY 3 sal MONTHLY_SALARY 4 FROMemp 4 FROMemp 5 WHEREdeptno = 30; 5 WHEREdeptno = 30; View created.

14-10 Database Systems Modifying a View: Example Modify the EMPVU10 view by using CREATE OR REPLACE. 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 (id_number, employee, title) 2 (id_number, employee, title) 3 AS SELECTempno, ename, job 3 AS SELECTempno, ename, job 4 FROMemp 4 FROMemp 5 WHEREdeptno = 10; 5 WHEREdeptno = 10; View created. SQL> CREATE OR REPLACE VIEW empvu10 2 (id_number, employee, title) 2 (id_number, employee, title) 3 AS SELECTempno, ename, job 3 AS SELECTempno, ename, job 4 FROMemp 4 FROMemp 5 WHEREdeptno = 10; 5 WHEREdeptno = 10; View created.

14-11 Database Systems Creating a Complex View: Example 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) 2 (name, minsal, maxsal, avgsal) 3 AS SELECTd.dname, MIN(e.sal), 3 AS SELECTd.dname, MIN(e.sal), 4 MAX(e.sal), AVG(e.sal) 4 MAX(e.sal), AVG(e.sal) 5 FROMemp e, dept d 5 FROMemp e, dept d 6 WHEREe.deptno = d.deptno 6 WHEREe.deptno = d.deptno 7 GROUP BY d.dname; 7 GROUP BY d.dname; View created. SQL> CREATE VIEW dept_sum_vu 2 (name, minsal, maxsal, avgsal) 2 (name, minsal, maxsal, avgsal) 3 AS SELECTd.dname, MIN(e.sal), 3 AS SELECTd.dname, MIN(e.sal), 4 MAX(e.sal), AVG(e.sal) 4 MAX(e.sal), AVG(e.sal) 5 FROMemp e, dept d 5 FROMemp e, dept d 6 WHEREe.deptno = d.deptno 6 WHEREe.deptno = d.deptno 7 GROUP BY d.dname; 7 GROUP BY d.dname; View created.

14-12 Database Systems 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 – Group functions. – A GROUP BY clause. – The DISTINCT keyword.

14-13 Database Systems Rules for Performing DML Operations on a View continued You cannot modify data in a view if it contains – Any of the above conditions. – Columns defined by expressions. – The ROWNUM pseudocolumn. You cannot add data if the view contains – Any of the above conditions. – Any NOT NULL columns in the base table not selected by the view.

14-14 Database Systems Using the WITH CHECK OPTION Clause Ensure that DML on the view stays within the domain of the view. If you attempt to change the department number for any rows in the view, the statement will fail because it violates the CHECK OPTION constraint. SQL> CREATE OR REPLACE VIEW empvu20 2 AS SELECT* 2 AS SELECT* 3 FROMemp 3 FROMemp 4 WHEREdeptno = 20 4 WHEREdeptno = 20 5 WITH CHECK OPTION CONSTRAINT empvu20_ck; 5 WITH CHECK OPTION CONSTRAINT empvu20_ck; View created. SQL> CREATE OR REPLACE VIEW empvu20 2 AS SELECT* 2 AS SELECT* 3 FROMemp 3 FROMemp 4 WHEREdeptno = 20 4 WHEREdeptno = 20 5 WITH CHECK OPTION CONSTRAINT empvu20_ck; 5 WITH CHECK OPTION CONSTRAINT empvu20_ck; View created.

14-15 Database Systems Using the WITH CHECK OPTION Clause Without check option, employee with empno = 7788 will be removed from the view. SQL> UPDATE empvu20 2 SET deptno = 10 2 SET deptno = 10 3 WHERE empno = 7788; 3 WHERE empno = 7788; Update empvu20 * ERROR at line 1: ORA-01402: view WITH CHECK OPTION where-clause violation SQL> UPDATE empvu20 2 SET deptno = 10 2 SET deptno = 10 3 WHERE empno = 7788; 3 WHERE empno = 7788; Update empvu20 * ERROR at line 1: ORA-01402: view WITH CHECK OPTION where-clause violation

14-16 Database Systems Denying DML Operations Ensure that no DML operations occur by adding the WITH READ ONLY option to your view definition. If you attempt to perform a DML on any rows in the view, you will see the Oracle Server error. SQL> CREATE OR REPLACE VIEW empvu10 2 (id_number, employee, title) 2 (id_number, employee, title) 3 AS SELECTempno, ename, job 3 AS SELECTempno, ename, job 4 FROMemp 4 FROMemp 5 WHEREdeptno = 10 5 WHEREdeptno = 10 6 WITH READ ONLY; 6 WITH READ ONLY; View created. SQL> CREATE OR REPLACE VIEW empvu10 2 (id_number, employee, title) 2 (id_number, employee, title) 3 AS SELECTempno, ename, job 3 AS SELECTempno, ename, job 4 FROMemp 4 FROMemp 5 WHEREdeptno = 10 5 WHEREdeptno = 10 6 WITH READ ONLY; 6 WITH READ ONLY; View created.

14-17 Database Systems Confirming Views The USER_VIEWS data dictionary table contains the name of the view and the view definition. SQL> SELECTview_name, text 2 FROMuser_views; 2 FROMuser_views; SQL> SELECTview_name, text 2 FROMuser_views; 2 FROMuser_views;

14-18 Database Systems Removing a View: Example Remove a view without losing data because a view is based on underlying tables in the database. SQL> DROP VIEW empvu10; View dropped. SQL> DROP VIEW empvu10; View dropped.

14-19 Database Systems Summary A view is derived from data in other tables or other views. A view is like a window to the underlying data. A view provides the following advantages: – Restrict database access – Simplify queries – Provide data independence – Allow multiple views of the same data – Can be dropped without removing the underlying data

14-20 Database Systems 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

14-21 Database Systems Practice 1 Create a view called DEPT20 that contains the employee number, name and department number for all employees in department 20 from EMP table. Label the view column EMPLOYEE_ID, EMPLOYEE and DEPARTMENT_ID. Do not allow an employee to be reassigned to another department through the view. Display the structure and contents of the DEPT20 view. Select the view name and text from the data dictionary USER_VIEWS. Attempt to reassign Smith to department 30.

14-22 Database Systems Practice 2 Create a view called SALARY_VU based on the employee name, depart name, salary and salary grade for all employees, using tables EMP, DEPT and SALGRADE. Label the columns Employee, Department, Salary and Grade respectively.