Oracle_ch13.pptx

download Oracle_ch13.pptx

of 43

Transcript of Oracle_ch13.pptx

  • 8/10/2019 Oracle_ch13.pptx

    1/43

    Dr. Chen, Oracle Database System (Oracle) 1

    Chapter 13Views

    (up to p.495)

    Jason C. H. Chen,Ph.D.

    Professor of MIS

    School of BusinessGonzaga University

    Spokane, WA 99258 USA

    [email protected]

  • 8/10/2019 Oracle_ch13.pptx

    2/43

    Dr. Chen, Oracle Database System (Oracle) 2

    Objectives

    Create a view by using CREATE VIEW command orthe CREATE OR REPLACE VIEW command

    Employ the FORCE and NOFORCE options

    State the purpose of the WITH CHECK OPTIONconstraint

    Explain the effect of the WITH READ ONLY option

    Update a record in a simple view

    Re-create a view

  • 8/10/2019 Oracle_ch13.pptx

    3/43

    Dr. Chen, Oracle Database System (Oracle) 3

    Objectives (continued)

    Explain the implication of an expression in aview for DML operations

    Update a record in a complex view Identify problems associated with adding records

    to a complex view Identify the key-preserved table underlying a

    complex view Drop a view

    Explain inline views and the use of ROWNUMto perform a TOP-N analysis

    Create a materialized view to replicate data

  • 8/10/2019 Oracle_ch13.pptx

    4/43

    Dr. Chen, Oracle Database System (Oracle) 4

    Database Views

    Table-1 Table-NTable-2

    database

    Single view table

    Query

    Output:

    Report, Graphs

    Database view

    A database view is

    a logical (virtual)

    table based on a

    query.

    It does not

    store data, but

    presents it in aformat

    different from

    the one in

    which it is

    stored in theunderlying

    tables.

    With the database view,

    you can view database

    records, but you cant

    insert new records or

    modify or delete exiting

    records.

  • 8/10/2019 Oracle_ch13.pptx

    5/43

    Dr. Chen, Oracle Database System (Oracle) 5

    Views

    Permanent objects (logical or virtual table)that store queries but nodata

    Based on a source query that: can specify a subset of a single tables fields or records

    can join multiple tables

    Two purposes

    Reduce complex query requirements

    Restrict users access to sensitive data (enforce

    security; user has access to view but not

    underlying table)

  • 8/10/2019 Oracle_ch13.pptx

    6/43

    Dr. Chen, Oracle Database System (Oracle) 6

    Creating and Using Database Views

    Views can be updateable if: SELECT clause contains only fieldnames, no functions

    or calculations

    cannot contain the ORDER BY, DISTINCT, or

    GROUP BY clauses, group functions, or set operators search condition cannot contain a nested query

    Views are used like tables for selecting, inserting,

    updating and deleting data (only updatable views canbe modified)

  • 8/10/2019 Oracle_ch13.pptx

    7/43

    Dr. Chen, Oracle Database System (Oracle) 7

    Database View (cont.)

    SELECT view_name FROM user_views;

    SELECT view_name FROM ALL_VIEWS

    WHERE owner=SYSTEM;

    DROP VIEW;

    CREATEORREPLACE VIEW AS

    ;

  • 8/10/2019 Oracle_ch13.pptx

    8/43

    Dr. Chen, Oracle Database System (Oracle) 8

    Creating a Simple View

    Only references one tableno group functions,

    GROUP BY clause, or expressions

    Figure 13-4 Selecting all records from the INVENTORY view

    -- chapter 13, Figure 13-4(b); p.477

    CREATE VIEW books_inventory_vu

    AS SELECT isbn, title, retail price

    FROM booksWITH READ ONLY;

    SELECT *

    FROM books_inventory_vu;

    -- chapter 13, Figure 13-4; p.477

    CREATE VIEW inventory

    AS SELECT isbn, title, retail price

    FROM booksWITH READ ONLY;

    SELECT *

    FROM inventory;

    DROP VIEW inventory;

    SELECT *

    FROM inventory;

    What happen to

    Database after

    DROP view?

    SELECT *

    FROM books;

    -- REcreate inventory view

  • 8/10/2019 Oracle_ch13.pptx

    9/43

    Dr. Chen, Oracle Database System (Oracle) 9

    CREATE OR REPLACE VIEW book_vu AS

    SELECT isbn, title, categoryFROM books

    WHERE category = 'COOKING';

    SELECT *FROM book_vu;

  • 8/10/2019 Oracle_ch13.pptx

    10/43

    Dr. Chen, Oracle Database System (Oracle) 10

    Figure 13-1 View Processing

  • 8/10/2019 Oracle_ch13.pptx

    11/43

    Dr. Chen, Oracle Database System (Oracle) 11

    Types of Views

    Table 13-1 Overview of View Concepts

  • 8/10/2019 Oracle_ch13.pptx

    12/43

    Dr. Chen, Oracle Database System (Oracle) 12

    I. Creating a View

    You use the CREATE VIEWkeywords to create a view

    Use OR REPLACE if the view already exists

    Use FORCE if the underlying table does not exist at the timeof creation

    Provide new column names if necessary

    WITH CHECK OPTION constraintif used, prevents data

    changes that will make the data subsequently inaccessible to

    the view

    WITH READ ONLYprevents DML operations

    Figure 13-2 Syntax of the CREATE VIEW command

  • 8/10/2019 Oracle_ch13.pptx

    13/43

    Dr. Chen, Oracle Database System (Oracle) 13

    Database View

    What are gains?

    --EXTRA for VIEW

    SELECT firstname || || lastname, order#, orderdate

    FROM customers_vu, orders

    WHERE customers_vu.customer# = orders.order#;

    DELETING VIEWSDROP VIEW viewname;

    DROP VIEW customers_vu;

    CREATE VIEW AS

    ;e.g.,

    SELECT view_name FROM user_views;

    CREATEOR REPLACE VIEWcustomers_vu ASSELECT customer#, lastname, firstname, regionFROM customers;

  • 8/10/2019 Oracle_ch13.pptx

    14/43

    Dr. Chen, Oracle Database System (Oracle) 14

    DML Operations on a Simple View

    Any DML operations are allowed through simple

    views unless created with WITH READ ONLY

    option

    DML operations that violate constraints on the

    underlying table are not allowed

    -- chapter 13, Figure 13-5; p.478

    CREATE VIEW region_ne

    AS SELECT *

    FROM customersWHERE region = 'NE';

    SELECT customer#, lastname, city, state, region

    FROM region_ne;

  • 8/10/2019 Oracle_ch13.pptx

    15/43

    Dr. Chen, Oracle Database System (Oracle) 15

    Why Failed to Update?

    -- chapter 13, Figure 13-6; p.479UPDATE inventory

    SET price = 45.96

    WHERE title LIKE '%POEM';

    SELECT *

    FROM inventory

    WHERE title LIKE '%POEM%';

    UPDATE inventory

    SET title = 'THE SHORTEST POEMS'

    WHERE title LIKE '%POEM%';

    Figure 13-6 Failed updates on the INVENTORY view

  • 8/10/2019 Oracle_ch13.pptx

    16/43

    Dr. Chen, Oracle Database System (Oracle) 16

    -- chapter 13, Figure 13-7; p.480

    CREATE OR REPLACE VIEW inventoryAS SELECT isbn, title, retail price

    FROM books;

    SELECT *

    FROM inventoryWHERE title LIKE '%BODYBUILD%';

    -- chapter 13, Figure 13-8; p.481

    UPDATE inventory

    SET price = 49.95

    WHERE title LIKE '%BODYBUILD%';

    SELECT *

    FROM inventory

    WHERE title LIKE '%BODYBUILD%';

    SELECT *FROM books

    WHERE title LIKE '%BODYBUILD%';

    DML Operations on a Simple View (continued)

  • 8/10/2019 Oracle_ch13.pptx

    17/43

    Dr. Chen, Oracle Database System (Oracle) 17

    Rule for DML operations

    As long as the view isnt created with the WITH

    READ ONLY option and any DML operation is

    allowed if it doesnt violate an existing constraint

    on the underlying table.

    You can add, modify, and even delete data in anunderlying table as long as one of the following

    constraints doesnt prevent the operation:

    PRIMARY KEY - FOREIGN KEY

    UNIQUE - NOT NULL

    WITH CHECK OPTIONPractice Figure 13-9 to

    13-11 (pp.482-483)

  • 8/10/2019 Oracle_ch13.pptx

    18/43

    Dr. Chen, Oracle Database System (Oracle) 18

    Exercises

    Practice all the examples in the text.

    A Script file is available on the Bb (file

    name: ch13Queries.sql)

    After completing all examples, do the HW.

    In-class Exercise

    #1 (p.508)

  • 8/10/2019 Oracle_ch13.pptx

    19/43

    Dr. Chen, Oracle Database System (Oracle) 19

    Homework - Hands-On Assignments

    Read and Practice all examples on Chapters 13

    1. Read Oracle assignment and create a script fileOracle_ch13_Lname_Fname.sql for questions (#1-5and 9,10; p.508) on Hands-on Assignments.

    2. Execute and test one problem at a time and make surethey are all running successfully.

    3. When you done, spool the script files (see next slidefor spooling instructions) and email the file(Oracle_ch13_Spool_Lname_Fname.txt) to me by themidnight before the next class.

    Email me with one attachment

    (Oracle_ch13_Spool_Lname_Fname.) to:

    [email protected] subject title of

    bmis441_Oracle_ch13 or mbus699_Oracle_ch13

  • 8/10/2019 Oracle_ch13.pptx

    20/43

    Dr. Chen, Oracle Database System (Oracle) 20

    How to Spool your Script and Output Files

    After you tested the script file of Oracle_ch13_Lname_Fname.sqlsuccessfully, follow the instructions below to spool both script and output

    files:Step 0. Run the following script file from SQL*Plus (since you have created

    JLDB tables)

    1. type the following on SQL> Spool c:\oradata\Oracle_ch13B_Spool_Lname_Fname.txt(make sure your name is

    entered)

    2. open Oracle_ch13_Lname_Fname.sql that you already tested

    3. copy and paste all the SQL commands (including all comments) to theSQL*PLUS

    4. type Spool Off on the SQL>

    The output should contain your personal information, all SQL commands andtheir solution on the .txt file and saved in C: drive (oradata\ folder)

    Email me with the spooled file (.txt) with attachment to:

    [email protected]

    with subject title of

    bmis441_Oracle_ch13 or mbus699_Oracle_ch13

  • 8/10/2019 Oracle_ch13.pptx

    21/43

    Dr. Chen, Oracle Database System (Oracle) 21

    PART II

  • 8/10/2019 Oracle_ch13.pptx

    22/43

    Dr. Chen, Oracle Database System (Oracle) 22

    II. Creating a Complex View

    A complex view may contain data from multipletables or data created with the GROUP BY clause,

    functions, or expressions

    Type of DML operations allowed depends on

    various factors

  • 8/10/2019 Oracle_ch13.pptx

    23/43

    Dr. Chen, Oracle Database System (Oracle) 23

    DML Operations on a Complex View with an Arithmetic Expression

    Figure 13-12 Create and update a complex view named prices

  • 8/10/2019 Oracle_ch13.pptx

    24/43

    Dr. Chen, Oracle Database System (Oracle) 24

    Rule#1:

    DML

    operations

    are not

    permitted ifthe view

    includes a

    column

    based on

    arithmetic

    expression

    Figure 13-14 Failed attempts to add a new book via the PRICES view

    -- chapter 13, Figure 13-15; p.488

    INSERT INTO prices (isbn, title, cost, retail)

    VALUES(0202020202, 'A New Book', 8.95, 15.95);

  • 8/10/2019 Oracle_ch13.pptx

    25/43

    Dr. Chen, Oracle Database System (Oracle) 25Figure 13-16 Constraint violation with an INSERT command via the PRICES view

    no pubid is

    inserted

    Rule#2:

    DML

    operations

    are not

    permitted ifthey violate

    a constraint

    such as NOT

    NULL,

    UNIQUE

    etc.

  • 8/10/2019 Oracle_ch13.pptx

    26/43

    Dr. Chen, Oracle Database System (Oracle) 26

    DML Operations on a Complex View Containing Data

    from Multiple Tables

    DML operations cannot be performed on non-key-preserved

    tables, but they are permitted on key-preserved tables

    Figure 13-17 PRICES view with a table joinPractice: Figure 13-18

  • 8/10/2019 Oracle_ch13.pptx

    27/43

    Dr. Chen, Oracle Database System (Oracle) 27

    DML Operations on a Complex View Containing Data

    from Multiple Tables (continued)

    Figure 13-19 Failed attempt to update the publisher name via the PRICES view

    Rule#3:

    DMLoperations

    cant be

    performed

    on a non-key-

    preserved

    table.

  • 8/10/2019 Oracle_ch13.pptx

    28/43

    Dr. Chen, Oracle Database System (Oracle) 28

    DML Operations on a Complex View Containing

    Functions or Grouped Data

    Rule#4: DML operations are notpermitted if the view

    includes a group function or a GROUP BY clause, theDISTINCT keyword, or the ROWNUM pseudocolumn.

    Figure 13-21 View including grouped data

    -- chapter 13, Figure 13-21; p.492

    CREATE VIEW balancedue

    AS SELECT customer#, order#, SUM(quantity * retail) AmtdueFROM customers JOIN orders USING (customer#)

    JOIN orderitems USING (order#)

    JOIN books USING (isbn)

    GROUP BY customer#, order#;

    SELECT *

    FROM balancedue;

  • 8/10/2019 Oracle_ch13.pptx

    29/43

    Dr. Chen, Oracle Database System (Oracle) 29

    SELECT *

    FROM balancedue;

    Figure 13-21 View

    including grouped data

  • 8/10/2019 Oracle_ch13.pptx

    30/43

    Dr. Chen, Oracle Database System (Oracle) 30

    DML Operations on a Complex View Containing

    Functions or Grouped Data (continued)

    Figure 13-22 Failed DELETE command on a view with grouped data

  • 8/10/2019 Oracle_ch13.pptx

    31/43

    Dr. Chen, Oracle Database System (Oracle) 31

    Dropping a View

    Use DROP VIEW command

    Figure 13-25 Command to drop the PRICES view

  • 8/10/2019 Oracle_ch13.pptx

    32/43

    Dr. Chen, Oracle Database System (Oracle) 32

    III. Creating an Inline View

    An inline view is a temporary table createdby using a subquery in the FROM clause

    It can only be referenced while the

    command is being executed

    Most common usageTOP-N analysis

  • 8/10/2019 Oracle_ch13.pptx

    33/43

    Dr. Chen, Oracle Database System (Oracle) 33

    TOP-N Analysis

    ORDER BY included to identify top values:Descending for highest values

    Ascending for lowest values

    Extract data based on ROWNUM

    Figure 13-26 Syntax of TOP-N analysis

  • 8/10/2019 Oracle_ch13.pptx

    34/43

    Dr. Chen, Oracle Database System (Oracle) 34

    TOP-N Analysis (continued)

    Figure 13-28 TOP-N analysis to identify the five most profitable books

  • 8/10/2019 Oracle_ch13.pptx

    35/43

    Dr. Chen, Oracle Database System (Oracle) 35

    Figure 13-28 TOP-N analysis to identify the five

    most profitable booksFigure 13-28 TOP-N analysis to determine the three

    lest profitable books

  • 8/10/2019 Oracle_ch13.pptx

    36/43

    Dr. Chen, Oracle Database System (Oracle) 36

    PART III

  • 8/10/2019 Oracle_ch13.pptx

    37/43

    Dr. Chen, Oracle Database System (Oracle) 37

    Materialized Views

    Replicate data

    Store data retrieved from view query

    Referred to as snapshots

  • 8/10/2019 Oracle_ch13.pptx

    38/43

    Dr. Chen, Oracle Database System (Oracle) 38

    Materialized Views (continued)

    Figure 13-30 Creating a materialized view named CUSTBAL_MV

  • 8/10/2019 Oracle_ch13.pptx

    39/43

    Dr. Chen, Oracle Database System (Oracle) 39

    Figure 13-31 Query a

    materialized view

  • 8/10/2019 Oracle_ch13.pptx

    40/43

    Dr. Chen, Oracle Database System (Oracle) 40

    Materialized Views (continued)

    Figure 13-32 Drop a materialized view

  • 8/10/2019 Oracle_ch13.pptx

    41/43

    Dr. Chen, Oracle Database System (Oracle) 41

    Summary

    A view is a temporary or virtual table that is used toretrieve data that exists in the underlying database tables

    The view query must be executed each time the view is

    used

    A view can be used to simplify queries or to restrict accessto sensitive data

    A view is created with the CREATE VIEW command

    A view cannot be modified; to change a view, it must be

    dropped and then re-created, or the CREATE ORREPLACE VIEW command must be used

  • 8/10/2019 Oracle_ch13.pptx

    42/43

    Dr. Chen, Oracle Database System (Oracle) 42

    Summary (continued)

    Any DML operation can be performed on a simple query if itdoes not violate a constraint

    A view that contains expressions or functions, or that joins

    multiple tables, is considered a complex view A complex view can be used to update only one table; the

    table must be a key-preserved table

    Data cannot be added to a view column that contains anexpression

    DML operations are not permitted on non-key-preservedtables

  • 8/10/2019 Oracle_ch13.pptx

    43/43

    Summary (continued)

    DML operations are not permitted on views that include groupfunctions, a GROUP BY clause, the ROWNUM pseudocolumn, or theDISTINCT keyword

    Oracle 11g assigns a row number to every row in a table to indicate itsposition in the table; the row number can be referenced by the keywordROWNUM

    A view can be dropped with the DROPVIEW command; the data isnot affected, because it exists in the original tables

    An inline view can be used only by the current statement and caninclude an ORDER BY clause

    TOP-N analysis uses the row number of sorted data to determine a

    range of top values Materialized views physically store view query results