How to Create Views in sql Server 2008



View:
_____

       A view is a virtual table that consists of columns from one or more tables.
 Though it is similar to a table, it is stored in the database.
 It is a query stored as an object. Hence, a view is an object that derives its data
 from one or more tables. These tables are referred to as base or underlying tables.


Views ensure the security of data by restricting access to the following data:
______________________________________________________________________________

    Specific rows of the tables.
    Specific columns of the tables.
    Specific rows and columns of the tables.
    Rows fetched by using joins.
    Statistical summary of data in a given tables.
    Subsets of another view or a subset of views and tables.



Some common examples of views are:
____________________________________

    A subset of rows or columns of a base table.
    A union of two or more tables.
    A join of two or more tables.
    A statistical summary of base tables.
    A subset of another view, or some combination of views and base table.


Creating Views:
___________________________

A view can be created by using the CREATE VIEW statement.


Syntax:
_______

CREATE VIEW view_name
[(column_name[,column_name]….)]
[WITH ENCRYPTION]
AS select_statement [WITH CHECK OPTION]

Where:

      view_name specifies the name of the view and must follow the rules for identifiers.

      column_name specifies the name of the column to be used in view. If the column_name option is not specified, then the view is created with the same columns as specified in the select_statement.

      WITH ENCRYPTION encrypts the text for the view in the syscomments table.

      AS specifies the actions that will be performed by the view.

      select_statement specifies the SELECT Statement that defines a view. The view may use the data contained in other views and tables.

      WITH CHECK OPTION forces the data modification statements to fulfill the criteria given in the SELECT statement defining the view. It also ensures that the data is visible after the modifications are made permanent.


Example:
--------------

    CREATE VIEW vwCustomer
    AS
    SELECT CustomerId, Company Name, Phone
    FROM Customers



Altering Views:
---------------------

You can modify a view without dropping it. This ensures that the permission on the view is also not lost. You can modify a view without affecting its dependent objects, such as triggers and stored procedures.

You modify a view using the ALTER VIEW statement.


Syntax:
_______

       ALTER VIEW view _name [column_ name)]
       [WITH ENCRYPTION]
       AS select_statement
       [WITH CHECK OPTION]



Dropping Views:
________________

You can drop a view from a database by using the DROP VIEW statement. When a view is dropped, it has no effect on the underlying tables. Dropping a view removes its definition and all the permissions assigned to it. Furthermore, if you query any views that reference a dropped view, you receive an error message. However, dropping a table that references a view does not drop the view automatically. You must drop it explicitly.

Syntax:
-----------

DROP VIEW view_name



Renaming Views:
_______________

You can rename a view without having to drop it. This ensures that the permissions on the view are not lost.

The guidelines for renaming a view are as follows:

    The view must be in the current database.
    The new name for the view must be followed by the rules for identifiers.
    A view can be renamed only by its owner.
    A view can also be renamed by the owner of the database.
    A view can be renamed by using the sp_rename system stored procedure.

Syntax:
--------


Sp_rename_old_viewname, new_viewname

      Where:

           old_viewname is the view that needs to be renamed.

            new_viewname is the new name of the view.

Example:
_________

           Sp_rename vwCutomers vwCustomerDetails

           Renames vwCutomers to vwCustomerDetails.

0 comments:

Post a Comment