SQL Views Presented by: Dr. Samir Tartir

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

Loading & organising data. Objectives Loading data using direct-load insert Loading data into oracle tables using SQL*Loader conventional and direct paths.
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Dec 4, 2003Murali Mani SQL B term 2004: lecture 14.
View Sen Zhang. Views are very common in business systems users view of data is simplified a form of security - user sees only the data he/she needs to.
Database Systems More SQL Database Design -- More SQL1.
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.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 11 July 12,2012 More SQL: Complex Queries, Triggers,
1 Chapter 14 DML Tuning. 2 DML Performance Fundamentals DML Performance is affected by: – Efficiency of WHERE clause – Amount of index maintenance – Referential.
Views In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual relations stored in the database.) In some.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
DATABASE VIEWS CHAPTER 5 (6/E) CHAPTER 8 (5/E) 1.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
Chapter 6 MORE SQL: Assertions, Views, and Programming Techniques Copyright © 2004 Pearson Education, Inc.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
Advanced SQL: Triggers & Assertions
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
Indexes and Views Unit 7.
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.
Query Processing – Query Trees. Evaluation of SQL Conceptual order of evaluation – Cartesian product of all tables in from clause – Rows not satisfying.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Chapter 7 SQL HUANG XUEHUA. Chapter Objectives Specification of more general constraints via assertions SQL facilities for defining views (virtual.
SQL- Updates, Assertions and Views. Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations)
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
Structured Query Language (3) The main reference of this presentation is the textbook and PPT from : Elmasri & Navathe, Fundamental of Database Systems,
10/3/2017.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Rob Gleasure robgleasure.com
Query Processing and Optimization, and Database Tuning
Introduction to SQL Programming Techniques
Rob Gleasure robgleasure.com
Implementing Views Advanced Database Dr. AlaaEddin Almabhouh.
Chapter 6 - Database Implementation and Use
Physical Changes That Don’t Change the Logical Design
SQL Stored Triggers Presented by: Dr. Samir Tartir
Views, Stored Procedures, Functions, and Triggers
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Normalization 2NF & 3NF Presented by: Dr. Samir Tartir
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Database Fundamentals
Company Requirements.
CS4222 Principles of Database System
Session - 6 Sequence - 2 SQL: The Structured Query Language:
Advanced SQL: Views & Triggers
Instructor: Mohamed Eltabakh
Introduction to SQL Programming Techniques
Normalization DB Design Guidelines Presented by: Dr. Samir Tartir
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Structured Query Language (3)
Chapter 8 Advanced SQL.
Views 1.
Session - 6 Sequence - 5 SQL Updating Database Contents
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
SQL Updating Database Contents Presented by: Dr. Samir Tartir
SQL Stored Procedures and Functions Presented by: Dr. Samir Tartir
Session - 6 Sequence - 1 SQL: The Structured Query Language:
SQL-Views and External Schemas
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
Views Base Relation View
CSC 453 Database Systems Lecture
Presentation transcript:

SQL Views Presented by: Dr. Samir Tartir Session - 6 Sequence - 6 SQL Views Presented by: Dr. Samir Tartir

Outline What views are? Creating a view Redefining a view Using a view Design considerations Updatable views Materialized Views

WHAT is a VIEW? A view is a virtual table based on the result-set of a SELECT statement. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database. You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if the data were coming from one single table.

Benefits Simplifying queries Security Hiding the tables that hold the data Restricting access to parts of tables (horizontally or vertically)

Specification of Views SQL command: CREATE VIEW a table (view) name a possible list of attribute names (for example, when arithmetic operations are specified or when we want the names to be different from the attributes in the base relations) a query to specify the table contents

SQL Views: An Example Specify a different WORKS_ON table CREATE VIEW WORKS_ON_NEW AS SELECT FNAME, LNAME, PNAME, HOURS FROM EMPLOYEE, PROJECT, WORKS_ON WHERE SSN = ESSN AND PNO = PNUMBER;

Using a Virtual Table We can specify SQL queries on a newly create table (view): SELECT FNAME, LNAME FROM WORKS_ON_NEW WHERE PNAME=‘Seena’; When no longer needed, a view can be dropped: DROP VIEW WORKS_ON_NEW;

UPDATING AN SQL View If the view definition needs to change, a CREATE OR REPLACE VIEW command is used. E.g. CREATE OR REPLACE VIEW WORKS_ON_NEW AS SELECT FNAME, LNAME, PNAME, HOURS, PNO FROM EMPLOYEE, PROJECT, WORKS_ON WHERE SSN = ESSN AND PNO = PNUMBER; Such changes need to be coordinated with view users as it may cause their programs to stop working

Efficient View Implementation Query modification: Present the view query in terms of a query on the underlying base tables Disadvantage: Inefficient for views defined via complex queries Especially if additional queries are to be applied to the view within a short time period

Efficient View Implementation View materialization: Involves physically creating and keeping a temporary table Assumption: Other queries on the view will follow Concerns: Maintaining correspondence between the base table and the view when the base table is updated Strategy: Incremental update

Update Views Update on a single view without aggregate operations: Update may map to an update on the underlying base table Views involving joins: An update may map to an update on the underlying base relations Not always possible

Un-updatable Views Views defined using groups and aggregate functions are not updateable Views defined on multiple tables using joins are generally not updateable WITH CHECK OPTION: must be added to the definition of a view if the view is to be updated To allow check for updatability and to plan for an execution strategy

Materialized Views A materialized view stores the result set of the select statement as a container table. An ordinary view stores the select statement and runs it only if we use the view Materialized views are used to speed up queries, in a OLAP environment (not OLTP)

Creation CREATE MATERIALIZED VIEW viewname [options] AS SELECT ... 14

Example If we have the following SALES table that contains > 7 million records. Name Null? Type PROD_ID NOT NULL NUMBER CUST_ID NOT NULL NUMBER TIME_ID NOT NULL DATE CHANNEL_ID NOT NULL NUMBER PROMO_ID NOT NULL NUMBER QUANTITY_SOLD NOT NULL NUMBER(10,2) AMOUNT_SOLD NOT NULL NUMBER(10,2) 15

Example (Contd.) Issuing queries on the table will take a long time. E.g. select channel_id,sum(amount_sold) from sales group by channel_id; might take up to 5 secs. 16

Example (Contd.) create materialized view mv1 enable query rewrite as select channel_id, sum(amount_sold) from sales group by channel_id; Issuing the same previous query now will take .01 seconds! 17

“Stale” Materialized Views A materialized view can become stale if the underlying table is updated. In this case, the view must refreshed. This is done in many ways Using the “REFRESH FAST on commit” option when creating the view. A variety of PL/SQL procedures (customized or Oracle’s) 18

SUMMARY Views are SQL’s way of virtual data Views can be used as a means of data hiding to present different parts of the same table to different groups of users Views can be “materialized” to speed up queries, but between refreshes, their contents can be out-of-date. Views can be updatable or non-updatable, depending on the source of the data. 19

Resources & References Dr. Samir Tartir Email: startir@philadelphia.edu.jo Website: http://www.philadelphia.edu.jo/academics/startir Uwe Hesse. Brief introduction into Materialized Views.