0% found this document useful (0 votes)
3 views

SQL_9

The document provides an overview of creating and managing views in MS SQL Server, highlighting their purpose, benefits, and rules. It explains how to create, modify, and delete views, as well as how to update data in tables using views. Key points include the security advantages of views, restrictions on their use, and the importance of permissions for accessing the underlying data.

Uploaded by

nidhit102646
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SQL_9

The document provides an overview of creating and managing views in MS SQL Server, highlighting their purpose, benefits, and rules. It explains how to create, modify, and delete views, as well as how to update data in tables using views. Key points include the security advantages of views, restrictions on their use, and the importance of permissions for accessing the underlying data.

Uploaded by

nidhit102646
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

MS SQL SERVER

SESSION # : 9
CREATING VIEWS
Focus Points :

Create views
Update data of a Table using views
Contents

 Views
 Create view
 Update data using views
 Modify Views
 Drop Views
Views

 A view, or virtual table, can be defined as an alternate way


of collecting data from one or more tables in a database.
Thus, the view consists of rows and columns just like a
normal table that is generated dynamically when a query is
executed. It, however, does not actually exist as a stored
set of data in the database.

Views are useful for the following purposes:


• To restrict a user to specific rows in a table.
• To restrict a user to specific columns.
• To join columns from multiple tables so that they appear to be a single
table.
• To get aggregate information instead of supplying details.
Views

 Control over what the user can see. This is useful for both security and ease
of use. The user does not have to look at extra information that he or she
doesn't require.
 Simplify the user interface by creating views of often-used queries. This will
enable a user to run a view with a simple statement rather than supplying
parameters every time the query is run.
 Security. Users can control only what you let them see. This might be a
subset of rows or columns, statistical information, or a subset of information
from another view.
 Because a view is a database object, you can assign user permissions on the
view. This is much more efficient than placing the same permissions on
individual columns in a table.
 Data can be exported from a view using the BCP utility.
Rules and Restrictions

 CREATE VIEW statements cannot be combined with other SQL statements in a


batch.
 When you are creating a view, any database objects that are referenced by the view
are verified at the time the view is created.
 When you are running a view, you must have SELECT permission on the objects
referenced in the view definition unless the objects do not have an owner specified.
This means that you could potentially create a view that you could not run.
Permissions on views are checked each time the view is run—not when it is created.
 You cannot include the ORDER BY, COMPUTE, or COMPUTE BY clauses in
your SELECT statement within a view.
 If you drop objects referenced within a view, the view still remains. You will receive
an error message the next time you attempt to use that view. For example, if you
create a view by joining information from two base tables and then drop one of the
base tables, the view remains but won't run. To avoid this problem, you can use
the sp_depends system-stored procedure to see what dependent objects the table
has before it is dropped.
 Temporary tables cannot be referenced in a view. This also means that you cannot
use a SELECT INTO clause in a view.
Rules and Restrictions

 If your view uses a SELECT * statement and the base table


referenced in the SELECT statement has new columns added,
the new columns will not show up in the view. The * is
resolved at creation time into a fixed list of columns. You must
alter the view to include the newly added columns.
 If you create a child view based on a parent view, you should
be aware of what the parent view is doing. You could run into
problems if the parent view is large and complex.
 Data in a view is not stored separately. This means that if you
modify data in a view, you are modifying the data in the base
tables.
 You cannot reference more than 1,024 columns in a view.
 You must specify names for all derived columns in your view.
 Triggers and indexes cannot be created on a view.
Creating a view

Create view viewname


As
Query
Modifying a view

Alter view viewname


As
Query
Deleting a view

Drop view viewname


Altering Data of a table using Views

insert into viewname values()

update viewname set columnname = value,…


[where]

delete from viewname [where] [expression]


Examples
Summary

 Views
 Creating view
 Modifying view
 Deleting a view
 Altering data of a table using view

You might also like