view-dsil

Upload: neepolion

Post on 05-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 View-Dsil

    1/16

    What is VIEW

    Logically represents subsets of data from one or more tables

    A view is a DBO that consists of columns from one or more tables.

    It is a query stored as an object.

    View can be referred like any other table in a database.

    A view serves as a security mechanism. This ensures that users are ableto retrieve and modify only the data seen by them

  • 8/2/2019 View-Dsil

    2/16

    Advantages of VIEW

    To restrict

    data access

    To make complex

    queries easy

    To provide dataindependence

    To present differentviews of the same

    data

  • 8/2/2019 View-Dsil

    3/16

    User Defined Views

    User Defined Views

    Simple View

    Complex View

    Indexed View

  • 8/2/2019 View-Dsil

    4/16

    User Defined View

    These Views are created by a user as per

    requirements.

    Simple Views and Complex Views

  • 8/2/2019 View-Dsil

    5/16

    Creating a View

    CREATE VIEW View_Name [(Column List)]

    [with {ENCRYPTION |SCHEMABINDING | VIEWMETADAT}]

    AS Select

    [WITH CHECK OPTION]

  • 8/2/2019 View-Dsil

    6/16

    Example : Simple View

    Create the EMPVDEPT10 view, which contains

    details of employees in department 10

    CREATE VIEW EMPVDEPT10 ASSELECT Eno,EmpName,Salary,DeptNo

    FROM employees

    WHERE DeptNo = 10

  • 8/2/2019 View-Dsil

    7/16

    Example: Complex View

    Create a complex view that contains group functions to displayvalues from two tables

    CREATE VIEW EMPV(Ename, Minsal, Maxsal, Avgsal)AS SELECT d.DeptName,MIN(e.salary),MAX(e.salary),AVG(e.salary)

    FROM employees e JOIN Departments dON (e.DeptNo = d.DeptId)GROUP BY d.DeptName

  • 8/2/2019 View-Dsil

    8/16

    Rules for Performing

    Insert 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

  • 8/2/2019 View-Dsil

    9/16

    Querying a View

  • 8/2/2019 View-Dsil

    10/16

    Rules for Performing

    Update Operations on a View

    You cannot modify data in a view if it contains:

    Group functions

    A GROUP BY clause

    The DISTINCT keyword

    The pseudo column ROWNUM keyword

    Columns defined by expressions

  • 8/2/2019 View-Dsil

    11/16

    Rules for Performing

    Delete 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

  • 8/2/2019 View-Dsil

    12/16

    Using the WITH CHECK OPTION

    Clause

    You can ensure that DML operations performed onthe view stay in the domain of the view by using theWITH CHECK OPTION clause:

    CREATE OR REPLACE VIEW empvu20

    AS SELECT *FROM employeesWHERE department_id = 20WITH CHECK OPTION CONSTRAINT

    empvu20_ck Any attempt to change the department number for

    any row in the view fails because it violates the WITHCHECK OPTION constraint.

  • 8/2/2019 View-Dsil

    13/16

    WITH Encryption

    The WITH ENCRYPTION option encrypts the

    SELECT statement, thus enhancing the security

    of the database system. Software vendors who

    use SQL Server in the back end often encrypt

    their views or stored procedures in order to

    prevent tampering or reverse-engineering from

    clients or competitors. If you use encryption, besure to save the original, unencrypted definition.

  • 8/2/2019 View-Dsil

    14/16

    WITH SCHEMABINDING

    The SCHEMABINDING clause binds the view to

    the schema of the underlying table. Creating a

    view with the SCHEMABINDING option locks

    the tables being referred by the view and

    prevents any changes that may change the

    table schema.

  • 8/2/2019 View-Dsil

    15/16

    Drop View

    You can remove a view without losing data

    because a view is based on underlying tables

    in the database.

    DROP VIEWview;

    DROP VIEW empvu80;

    ALTER VIEW "Alphabetical list of products" AS

    SELECT Statement

  • 8/2/2019 View-Dsil

    16/16

    Indexed View

    A view created with a unique clustered index is known as an Indexed View orMaterialized View. Unlike views, an Indexed View exists on the disk like a table inwhich the clustered index is created. Once a clustered index is created you maycreate non-clustered indexes on the view

    CREATE VIEW vwOrderDetails WITH SCHEMABINDING

    AS

    SELECT OD.OrderID, OD.ProductID, P.ProductName , OD.UnitPrice , OD.Quantity, OD.Discount

    FROM dbo.Products P

    INNER JOIN dbo.[Order Details] OD

    ON P.ProductID = OD.ProductID

    GO

    SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON

    GO

    CREATE UNIQUE CLUSTERED INDEX [IDX_vwOrderDetails]