Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Views Presented by: Dr. Samir Tartir

Similar presentations


Presentation on theme: "SQL Views Presented by: Dr. Samir Tartir"— Presentation transcript:

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

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

3 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.

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

5 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

6 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;

7 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;

8 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

9 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

10 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

11 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

12 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

13 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)

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

15 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

16 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

17 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

18 “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

19 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

20 Resources & References
Dr. Samir Tartir Website: Uwe Hesse. Brief introduction into Materialized Views.


Download ppt "SQL Views Presented by: Dr. Samir Tartir"

Similar presentations


Ads by Google