How to Create Views in sql Server 2008


       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.


CREATE VIEW view_name
AS select_statement [WITH CHECK OPTION]


      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.


    CREATE VIEW vwCustomer
    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.


       ALTER VIEW view _name [column_ name)]
       AS select_statement

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.


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.


Sp_rename_old_viewname, new_viewname


           old_viewname is the view that needs to be renamed.

            new_viewname is the new name of the view.


           Sp_rename vwCutomers vwCustomerDetails

           Renames vwCutomers to vwCustomerDetails.


Post a Comment