Chapter 2 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

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
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
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.
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
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 Structured Query Language (SQL)
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.
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.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
11 Copyright © Oracle Corporation, All rights reserved. Creating Views.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
Oracle 11g DATABASE DEVELOPMENT LAB1. Introduction  Oracle 11g Database:-  Oracle 11g database is designed for some features, which helps to the organizations.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
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.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
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.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  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;
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.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
 CONACT UC:  Magnific training   
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Creating Database Triggers
SQL Creating and Managing Tables
Manipulating Data.
Creating Views Schedule: Timing Topic 20 minutes Lecture
Using the Set Operators
Creating Other Schema Objects
Using DDL Statements to Create and Manage Tables
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 5 Sequences.
Chapter 4 Indexes.
CH 4 Indexes.
Chapter 2 Views.
“Manipulating Data” Lecture 6.
Database Programming Sections 11 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
A Guide to SQL, Eighth Edition
“Manipulating Data” Lecture 6.
CH 4 Indexes.
Manipulating Data.
Using DDL Statements to Create and Manage Tables
Contents Preface I Introduction Lesson Objectives I-2
Using DDL Statements to Create and Manage Tables
IST 318 Database Administration
Using the Set Operators
Presentation transcript:

Chapter 2 Views

Objectives Create simple and complex views Creating a view with a check constraint Retrieve data from views Data manipulation language (DML) operations on a view Dropping a view

Database Objects Logically represents subsets of data from one or more tables View Generates numeric values Sequence Basic unit of storage; composed of rows Table Gives alternative names to objects Synonym Improves the performance of data retrieval queries Index Description Object

What Is a View? EMPLOYEES table

What Is a View? A view is a logical table based on a table or another view A view contains no data of its own, but its like a window through which data from tables can be viewed or changed The tables on which a view is based are called base tables

Advantages of Views Security: views prevent undesired access by providing security as the data that is not of interest to a user can be left out of the view. Views are usually virtual and occupy no space. Display different data for different types of users. Simplicity: Complex queries that need to be executed often can be saved in a view. Hence by calling the view name, query can be executed.

Advantages of Views Independence: View can make the application and database tables to a certain extent independent. If there is no view, the application must be based on a table. With the view, the program can be established in view of above, to view the program with a database table to be separated. One view can be used to represent data from several tables

Simple Views and Complex Views Yes No One Simple Views Contain functions Contain groups of data One or more Number of tables Not always DML operations through a view Complex Views Feature

Creating a view You embed a subquery in 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

Creating a view Create the EMPVU80 view, which contains details of the employees in department 80: Describe the structure of the view by using the iSQL*Plus DESCRIBE command: CREATE VIEW empvu80 AS SELECT employee_id, last_name, salary FROM employees WHERE department_id = 80; DESCRIBE empvu80

Guidelines The sub query that defines a view can contain complex SELECT syntax, including Joins ,groups and Subqueries If you don’t specify a constraint name for the view created with the WITH CHECK OPTION, the system assigns a default name You can use the OR REPLACE option to change the definition of the view without droping and re-creating it , or re-granting the object privileges

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;

Creating a View Another way to use aliases : CREATE OR REPLACE VIEW salvu50 (ID_NUMBER, NAME, ANN_SALARY) AS SELECT employee_id, last_name, salary*12 FROM employees WHERE department_id = 50;

Retrieving Data from a View You can display all the content of a view or specify selected column from the view. SELECT * FROM salvu50;

Modifying a View Modify the EMPVU80 view by using a CREATE OR REPLACE VIEW clause. Add an alias for each column name: Column aliases in the CREATE OR REPLACE 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; Oracle Database 11g: SQL Fundamentals I 11 - 16

Creating a Complex View Create a complex view that contains group functions to display values from two tables: CREATE OR REPLACE 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 JOIN departments d ON (e.department_id = d.department_id) GROUP BY d.department_name; Note that alternative names have been specified for the view. This is a requirement if any column of the view is derived from a function or an expression. Oracle Database 11g: SQL Fundamentals I 11 - 17

Rules for Performing DML Operations on a View You can usually 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 Oracle Database 11g: SQL Fundamentals I 11 - 18

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 (for example, SALARY * 12). Oracle Database 11g: SQL Fundamentals I 11 - 19

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 without default values in the base table Remember that you are adding values directly to the underlying table through the view. Oracle Database 11g: SQL Fundamentals I 11 - 20

Using the WITH CHECK OPTION Clause You can ensure that DML operations performed on the view stay in the domain of the view by using the WITH CHECK OPTION clause: Any attempt to INSERT a row with a department_id other than 20, or to UPDATE 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 ; Oracle Database 11g: SQL Fundamentals I 11 - 21

Using the WITH CHECK OPTION Clause The WITH CHECK OPTION clause specifies that INSERTs and UPDATEs performed through the view cannot create rows that the view cannot select. Therefore it enables integrity constraints and data validation checks to be enforced on data being inserted or updated. If there is an attempt to perform DML operations on rows that the view has not selected, an error is displayed, along with the constraint name if that has been specified. UPDATE empvu20 SET department_id = 10 WHERE employee_id = 201; causes: ERROR

Using the WITH CHECK OPTION Clause No rows are updated because, if the department number were to change to 10, the view would no longer be able to see that employee. With the WITH CHECK OPTION clause, therefore, the view can see only the employees in department 20 and does not allow the department number for those employees to be changed through the view.

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 operation on any row in the view results in an Oracle server error. Oracle Database 11g: SQL Fundamentals I 11 - 24

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 ; Any attempt to remove a row from a view with a read-only constraint results in an error: DELETE FROM empvu10 WHERE employee_number = 200; Similarly, any attempt to insert a row or modify a row using the view with a read-only constraint results in the same error. Oracle Database 11g: SQL Fundamentals I 11 - 25

Removing a View You can remove a view without losing data because a view is based on underlying tables in the database. DROP VIEW view; DROP VIEW empvu80; dropping views has no effect on the tables on which the view was based. On the other hand, views or other applications based on the deleted views become invalid Oracle Database 11g: SQL Fundamentals I 11 - 26