Professional Documents
Culture Documents
3.creating and Managing Views
3.creating and Managing Views
Creating and
Managing Views
""''"�•••J2c-,..,..,. ,,_.,.,..,,.-,..,�,.___,..,,,_ . ....,..,,,,,.�..:,,,.,,,__,.3,,,,,.,,..,.,.;.iiii.:�--:,,i�....lf±'-"·"".<•;..;:•,. ··.. ;-�,.,,..,.,. ___,._
.;,;;;,;..,c-· "-"' >c-::-<!.,_._�..x•>-� .,... ..._..,,__,...,_,.L, ;, ,,.,; f
r
...... -
At times, the database administrator might want to restrict access of data to different
users. They might want some users to be able to access all the columns of a table whereas
other users to be able to access only selected columns. The SQL Server allows you to
create views to restrict user access to the data. Views also help in simplifying query
execution when the query involves retrieving data from multiple tables by applying joins.
A view is a virtual table, which provides access to a subset of columns from one or more
tables. It is a query stored as an object in the database, which does not have its own data.
A view can derive its data from one or more tables, called the base tables or underlying
tables. Depending on the volume of data, you can create a view with or without an index.
As a database developer, it is important for you to learn to create and manage views.
A view is a database object that is used to view data from the tables in the database. A
view has a structure similar to a table. It does not contain any data, but derives its data
from the underlymg tables.
Apart from restricting access,· views can also be used to create and save queries based on
· multiple tables. To view data from multiple tables, you can create a query that includes
various joins. If you need to frequently execute this query, you can create a view that
executes this query. You can access data from this view every time you need to execute
the query.
You can create a view by using the CREATE VIEW statement. The syntax of the
CREATE VIEW statement is:
WITH ENCRYPTION specifies that the the text of the view will be encrypted in the
syscornments view.
sCHEMABINDING binds the view to the schema of the underlying table or tables.
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 meet the criteria given ih
the SELECT statement defining the view. The data is visible through the view after the
modifications have been made permanent.
Note
Syscomments view is a system-defi,ned view stored in the database. It contains entries
for each view, rule, default, CHECK constraint, DEFAULT constraint, and stored
procedure within the database. This view contains a text column that stores the original
SQL definition statements.
For example, to provide access only to the employee ID, marital status, and department
ID for all the employees you can create the following view:
The preceding code creates the vwEmployeeDepData view containing selected columns
from the Employee and EmployeeDepartmentHisto1y tables.
Views do not maintain a separate_ copy of the data, but only display the data present in the
base tables. Therefore, you can modify the base tables by modifying the data in the view.
However, the following restrictions exist while inserting, updating, or deleting data
through views:
ii You cannot modify data in a view if the modification affects more than one
underlying table. However, you can modify data in a view if the modification affects
only one table at a time.
@i You cannot change a column that is the result of a calculation,· · such as a computed
column or an aggregate function.
For example, a view displaying the employee id, manager id :, and rate of the employees
has been defined using the following statement:
After creating the view, if you try executing the following update statement, it generates
an en-or. This is because the data is being modified in two tables through a single update
statement:
UPDATE vwSal
SET ManagerID = 2, Rate 12. 45
· WHERE EmployeeID = 1
Therefore, instead of a single UPDATE statement, you need to execute two UPDATE
statements for each table.
UPDATE vwSal
SET ManagerID = 2
WHERE EmployeeID = 1
The following statement would update the Rate attribute in the EmployeePayHistory
table:
UPDATE vwSal
SET Rate = 12.45
WHERE EmployeeID = 1
r
Therefore, to modify the data in two or more underlying tables through a view, you need
to execute separate UPDATE statements for each table.
f)Note
You can create an INSTEAD OF trigger on the view to modify data in a view if the
modification affects more than one underlying table. You will learn about triggers in
Chapter 8.
Similar to the tables, you can create indexes on views. By default, the views created on a
table are nonindexed. However, you can index the views when the volume of data in the
underlying tables is large and not frequently updated. Indexing a view helps in improving
the query performance.
Another benefit of creating an indexed view is that the optimizer starts using the view
index in queries that do not directly name the view in the FROM clause. If the query
contains references to columns that are also present in the indexed view, and the query
optimizer estimates that using the indexed view offers the lowest cost access mechanism,
the query optimizer selects the indexed view.
When indexing a view, you need to first create a unique clustered index on a view. After
you have defined a unique_ ch1stered index on a view, you can create additional
nonclustered indexes. When a view is indexed, the rows of the view are stored in the
database in the same format as a table.
You should consider the following guidelines while creating an indexed view:
� A unique clustered index must be the first index to be created on a view.
fi@ The view must not reference any other views, it can reference only base tables.
W® All base tables referenced by the view must be in the same database and have the
same owner as the view.
wa The view must be created with the SCHEMABINDING option. Schema binding
binds the view to the schema of the underlying base tables.
You can create indexes on views by using the CREATE INDEX statement. For example,
you can use the following statements for creating a unique clustered index on the
vwEmployeeDepData view:
The vwEmployeeDepData view was not bound to the schema at the time of creation.
Therefore, before executing the preceding statement, you need to bind the
vwEmployeeDepData view to the schema using the following statement:
The preceding statement alters the existing view, vwEmployeeDepData, and binds it with
the schema of the underlying tables. You can then create a unique clustered index on the
view.
Answer
2. When the data is regularly updated
(Managing Vi;'
In addition to creating views, you also need to manage then1. Management of a view
includes altering, dropping, or renaming views.
Altering Views
If you define a view with a SELECT* statement and then alter the structure of the
underlying tables by adding columns, the new columns do not appear in the view.
Similarly, when you select all the columns in a CREATE VIEW statement, the columns
list is interpreted only when you first create the view. To add new columns in the view,
you must alter the view.
You can modify a view without dropping it. This ensures that permissions on the view are
not lost. You can modify a view without affecting its dependent objects.
To modify a view, you need to use the ALTER VIEW statement. The syntax of the
ALTER VIEW statement is:
where,
WITH ENCRYPTION option encrypts the text of the view in the syscomments view.
WITH CHECK OPTION forces the data modification statements to follow the criteria given
in the SELECT statement.
For example, you created a view to retrieve selected data from the Employee and
EmployeeDepartmentHistory tables. You need to alter the view definition by including
the LoginID attribute from the Employee table.
To modify the definition, you can write the following statement:
The preceding code alters the view definition by including the LoginID attribute from the
Employee table.
Dropping Views
You need to drop a view when it is no longer required. 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 table(s). Dropping a view removes its definition and all the permissions
assigned to it.
Further, if you query any view that references a dropped table, you receive an error
message. Dropping a table that references a view does not drop the view automatically.
You have to use the DROP VIEW statement explicitly.
For example, you can use the following statement to remove the vwEmployeeDepData
view:
The preceding statement will drop the vwEmployeeDepData view from the database.
You can drop multiple views with a single DROP VIEW statement. The names of the
views that need to be dropped are separated by commas in the DROP VIEW statement
Renaming Views
At times, you might need to change the name of a view. You can rename a view without
dropping it. This ensures that permissions on the view are not lost. A view can be
renamed by using the sp_rename system stored procedure.
where,
. For example, you can use the following statement to rename the vwSal view: