A view is a virtual table, which gives access to a subset of columns from one or more tables. Hence, a view is an object that derives its data from one or more tables. These tables are referred to as the base tables or the underlying tables.
Views ensure security of data by restricting access to the following data:
v      Specific rows of the table
v      Specific columns of the table.
v      Rows fetched by using joins.
v      Statistical summary of data in a given table.
v      Subsets of another view or a subset of views and tables
Common examples of views include:
ü       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 a base tables
ü       A subset of another view, or some combination of views and base tables.

Managing Views

Using the CREATE VIEW statement we can create a view.
The syntax of the CREATE VIEW statement is:
          CREATE VIEW view_name
          [(column_name [,column_name]….)]
          AS select_statement[WITH CHECK OPTION]



The restrictions imposed on views are as follows:
ü       A view can be created only in the current database.
ü       A view can be created only if there is the SELECT permission on its base table.
ü       The SELECT INTO statement cannot be used in a view declaration statement.
ü       A view cannot derive its data from temporary tables.
CREATE VIEW custview
SELECT customerid, companyname, phone FROM customers



The above statements create a view named custview, which contains the custoemerid, companyname and the phone columns of the customer table. The following statement can be used to execute a view:
SELECT * from custview




Getting View Information

          SQL Server stores information in a view in the following system tables.
ü       Sysobjects-stores the name of the view
ü       Syscolumns-stores the names of the columns defined in the view.
ü       Sysdepends-stores information on the view dependencies
ü       Syscomments-stores the text of the view definition

Altering a view

            A view can be modified without dropping it. It ensures that the permissions on the view are not lost. A view can be altered without affecting its dependent objects, such as, triggers and stores procedures.

 A view can be modified using the ALTER VIEW statement.

 The syntax of ALTER VIEW statement is:

          ALTER VIEW view_name([column_name])







ALTER VIEW custview


          SELECT customerid, companyname, phone, fax

          From customers




The statement generates the results:


SELECT * FROM custview


Dropping a View

            A view can be dropped from a database using the DROP VIEW statement. When a view is dropped, it has no effect on the underlying table(s). Dropping a view removes its definition and all permissions assigned to it. Further more if users query any views that refer to a dropped view, they receive an error message. However, dropping a table that refers to a view does not drop the view automatically. You must drop it explicitly.

The syntax of  DROP VIEW statement is:
          DROP VIEW view_name
Where view_name is the name of the view to be dropped.
It is possible to drop multiple views with a single DROP VIEW. A comma in the DROP statement separates each view that needs to be dropped.

Renaming a View

            A view can be renamed with out dropping it. This also ensures that the permissions on the view are not lost.
The guidelines to be followed for renaming a view as follows.
ü       The view must be in the current database.
ü       The new name for the view must follow the rules for renaming identifiers.
ü       Only the owner of the view and the database in which view is created can rename a view.
A view can be renamed by using the sp_rename system stored procedure.
The syntax of sp_rename is
Sp_rename old_viewname ,new_viewname
          sp_rename custview,customerView





This site uses Akismet to reduce spam. Learn how your comment data is processed.