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.
Once you have defined a view, you can reference it like any other table in a database.
A view serves as a security mechanism. This ensures that users are able to retrieve and modify only the data seen by them. Users cannot see or access the remaining data in the underlying tables. A view also serves as a mechanism to simplify query execution. Complex queries can be stored in the form as a view, and data from the view can be extracted using simple queries.
Example
Consider the Publishers table below. If you want users to see only two columns in the table, you can create a view called vwPublishers that will refer to the Publishers table and the two columns required. You can grant Permissions to users to use the view and revoke Permissions from the base Publishers table. This way, users will be able to view only the two columns referred to by the view. They will not be able to query on the Publishers table.
Publishers
Publd |
PubName |
City |
State |
Country |
0736 |
New Moon Books |
Boston |
MA |
USA |
0877 |
Binnet & Hardly |
Washington |
DC |
USA |
1389 |
Algodata Infosystems |
Berkeley |
CA |
USA |
1622 |
Five Lakes Publishing |
Chicago |
IL |
USA |
VW Publishers
Publd |
PubName |
0736 |
New Moon Books |
0877 |
Binnet & Hardly |
1389 |
Algodata Infosystems |
1622 |
Five Lakes Publishing |
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.
The restrictions imposed on views are as follows:
- A view can be created only in the current database.
- The name of a view must follow the rules for identifiers and must not be the same as that of the base table.
- A view can be created only if there is a SELECT permission on its base table.
- A SELECT INTO statement cannot be used in view declaration statement.
- A trigger or an index cannot be defined on a view.
- The CREATE VIEW statement cannot be combined with other SQL statements in a single batch.
Example
CREATE VIEW vwCustomer
AS
SELECT CustomerId, Company Name, Phone
FROM Customers
Creates a view called vwCustomer. Note that the view is a query stored as an object. The data is derived from the columns of the base table Customers.
You use the view by querying the view like a table.
SELECT *FROM vwCUSTOMER
The output of the SELECT statement is:
CustomerId |
Company Name |
Phone |
ALFKI |
Alfreds Futterkiste |
030-0074321 |
ANTON |
Antonio Moreno Taqueria |
(5)555-3932 |
(91 rows affected)