Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
sql10

sql10

Ratings: (0)|Views: 44|Likes:
Published by bookworm_shashi

More info:

Published by: bookworm_shashi on Sep 16, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

02/01/2013

pdf

text

original

Views:
Introduction

A view is quite easy to define, but a lot more difficult to create, use and manage. It\u2019s not anymore than a named SELECT statement, or a virtual table. You can select data, alter data, remove data and all other things you can do with a table (with some limitations). But there is a difference between a view and a table. The data accessible through a view is not stored in the database as its own object. It\u2019s stored in the underlying tables that make up the view. The only thing that is stored in the database is the SELECT statement that forms the virtual table . Once you have defined a view, you can reference it like any other table in a database.

Advantages of using views

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.

\u2022
Restrict data access and/or simplify data access

A view can be used to limit the user to only use a few columns in a table. For
example if we do not want a user to be able to access all columns because of
security. But it could also be because not all columns are interesting for the user.
It is also possible to limit access to rows, using a WHERE clause. If we use
USER_ID(), we can even find out who is using the view, and return only data that
is relevant for this user. Furthermore, a view can join several tables, in several
databases on several servers, but all the user use is the view\u2019s name. Simple, but
powerful!

\u2022
Simplify data manipulation

We can also use a view to manipulate data. Often with a relational database
design, this means that many tables must be joined together. Using a view can
simplify this, and the users do not need to know all tables involved.

\u2022
Import and export data
A view can also be used to export and/or import data to/from another application.
Both the bcp utility and BULK INSERT works with a view.
\u2022
Merge data

A so called Partition View can be used to merge data from multiple tables in
multiple databases, so it appears as one table only, but it is in fact several tables.
This can be accomplished by using the UNION operator.

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:
\u2022
Specific rows of the tables.
\u2022
Specific columns of the tables.
\u2022
Specific rows and columns of the tables.
\u2022
Rows fetched by using joins.
\u2022
Statistical summary of data in a given tables.
\u2022
Subsets of another view or a subset of views and tables.
Some common examples of views are:
\u2022
A subset of rows or columns of a base table.
\u2022
A union of two or more tables.
\u2022
A join of two or more tables.
\u2022
A statistical summary of base tables.
\u2022
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]\u2026.)]
[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:
\u2022
A view can be created only in the current database.
\u2022
The name of a view must follow the rules for identifiers and must not be the same
as that of the base table.
\u2022
A view can be created only if there is a SELECT permission on its base table.
\u2022
A SELECT INTO statement cannot be used in view declaration statement.
\u2022
A trigger or an index cannot be defined on a view.
\u2022
The CREATE VIEW statement cannot be combined with other SQL statements in
a single batch.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->