Você está na página 1de 11

Introduction

In this article I am going to describe about VIEW in SQL Server 2005. Though this is a simple topic, still I am writing This is not only about view, I have made some plan to write a few article on SQL Server 2005. I hope this will also help you like my ASP.NET articles. Please give me your valuable suggestion and feedback to improve my articles.

What is VIEW
A view is an "Virtual Table". It is not like a simple table, this is an virtual table which contains columns and data from different table ( may be one or more tables) . View does not contain any data directly, it is a set of query that are applied to one or more tables that is stored within the database as object. After creating a view from some Table, it just used as a reference of those table and when executed shows only those data which are already mention in query during the creation of View

In the above diagram, we have created a View (View_Table1_Table2) from Table1 and Table2. So the View_Table1_Table2 will show only the information of those columns. Let's checkout the basic syntax for creating a view: Collapse
CREATE VIEW [View Name] AS [SELECT Statement]

Use of VIEW

Views are used as Security Mechanism of Database. Because it restricts user to view certain column and Rows. Views display only those data which are mentioned in the query, so its shows only those data which returns by the query that is defined at the time of creation of view. So rest data are totally abstract from the end user. Along with Security the other advantages of view is Data abstraction. Because end user is not aware of all the data in Table.

General Syntax for View


In this section I have described how to create view, select data from view and deleting a view. I have create on Database named, ViewDemo. It having on table called EmpInfo and below diagram showing you the design of the table,

which contain following data

Here all the example I have descrived only from this database.

Creating a View
Below is the general syntax for creating a view Collapse
CREATE VIEW [View_Name] AS [SELECT Statement]

As for example Collapse


CREATE VIEW SampleView As SELECT EmpID, EmpName FROM EmpInfo Which will create a view with name "SampleView",

that will only contain EmpID, EMPName.

Get Result From View


This is as similar as select statement of a table Collapse
select * from SampleView

Now have a look on the output of the SampleView

Drop a View
Collapse
DROP VIEW SampleView

Now if we want to select the data from the sampleview, we will get following error

Different Types of VIEW


There are two different types of VIEW

System View o Information Schema View o Catalog View o Dynamic Management View(DMV) User Define View o Simple View o Complex View

Now just have a look on different types of View in SQL Server 2005.

System View

In SQL Server there are few System Database available, like Master, Temp, msdb, tempdb . Each and . Each and every data base having its own responsibility, like master data is one of the template database for all the database which are created in SQL Server 2005. Similarly, System Views are predefined Microsoft created views that are already existed in master DB. These are also used as template View for all new database. These System Views will be automatically inserted into any user created database. There are around 230 System view available. We can explore the System view from the SQL Server Management Studio. Expand any Database > View > System View

In SQL Server all system view are divided into different schema. These are used for the security container of SQL Server database. We can categorized system view in following way,
Information Schema View Catalog View Dynamic Management View (DMV)

Now all above category are itself a huge topic, So I will not going to Details of It. Lets have a look into the over view of those view type Information View

These are the one of the most important system grouped view. There are twenty different schema views in this group. This are used for displaying most physical information of a database, such as

table, columns. Naming Conation of this type of views are INFORMATION_SCHEMA.[View Name] . From the System View Image we can get the few name of Information Schema View. Lets see it with one Example, I have create on Database named, ViewDemo. It having on table called EmpInfo and below diagram showing you the design of the table,

Now, if we want to know the details information columns of table Empinfo using View we have to run the following query, Collapse
SELECT * FROM INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME='EmpInfo'

Following will be the output

Similarly we can use all other Schema View to read Database information. Catalog View

This type of view are introduced in SQL Server 2005. catalog view are categorized in different group also. These are used to show the database self describing information. As for example, Collapse
select * from sys.tables

and following is the sample output

Dynamic Management View

This is newly introduced in SQL Server 2005.This Views gives the database administrator information about the current state of the SQL Server machine. These values will help the administrator to diagnose problems and tune the server for optimal performance. In SQL Server 2005, there are two types of DMV 1. Server-scoped DMV: Stored in Master Database 2. Database-scoped DMV: Specific to each database As for example if we want to check the all SQL Server connection, we have to use following query, Collapse
SELECT connection_id, session_id,client_net_address, auth_scheme FROM sys.dm_exec_connections

And following is the sample output

If you want to know details on DMV, Here is an Complete article on Code project , Dynamic Management Views [DMV] - A SQL Server 2005 Feature [^] Note: There are many things to learn on System View, I have just introduced them for beginners . If any one having more interest can have a look into this article System Views in SQL Server 2005 [^]

User Define View


Still now I have described about System View, Now have a look into User Define View. This views are created by the user as per requirement. There is no such classification of UDV and how to create the view, I have already explained the syntax. Now we can again have a look into another view creation. Collapse

CREATE VIEW DemoView AS SELECT EmpID, EmpName, Phone FROM EmpInfFROM EmpInfo

When To Use A View There are a number of scenarios where we will like to create our own view 1. To hide the complexity of the underlying database schema, or customize the data and schema for a set of users. 2. To control access to rows and columns of data.

View Creation Option


There are two different option for creating a view.
Schema Binding Option Encryption

Schema BindSchema Binding Option :


If we Creates a view with the SCHEMABINDING option it will locks the tables being referred by the view and restrict any kinds of changes that may change the table schema ( No Alter Command) . While creating schema binding view, we can't mention "Select * from tablename" with the query. We have to mention all the column name for reference

As for example, Collapse


CREATE VIEW DemoSampleView With SCHEMABINDING As SELECT EmpID, EmpName, FROM DBO.EmpInfo

And one more things that we need to remember, while specifying the Database name we have use Dbo.[DbName] .After creating the view, try to alter the
table EmpInfo , we cannot do it! This is the power of the SCHEMABINDING option.

If we want to change/Alter the defination of a table which refered by a schema binded view, we will get following error message.

Encryption : This option encrypts the definitionThis option encrypts the definition of the view. Users will not be able to see the definition of the View after it is created. This is the main adavatages of view where we can make it secure Collapse
CREATE VIEW DemoView With ENCRYPTION.EmpInfo Note: Once view is encrypted, there is no way to decrypt it again.

Use SSMS for Create View


studio provided handy GUI for creating and managing the View. In Object Explorer Tab, it listed all the View with corresponding to its database. What is view, Syntax for creating view and all other stuff I have already covered. Now, In this section we will just quickly check how SSMS used to create a maintain view.
SQL Server MSQL Server Management

First just expand da ViewDemoDB > Move to View . Right Click on View Folder.

When we will click on the New view , Following Screen will come . In the ViewDemoDB we are having two data tables. Now am going to create View from EmpInfo Table

Select the EmpInfo, Click on Add. You will redirected to Create View Screen where you can configure the view creation. Check the following image.

Above image showing three section, where we can select the table name or in below section, we can right query for view also. When its done. Just click on Save button on toolbar. Give the name of View and click on Ok.

Now again go to ViewDemoDB > View > Expand View folder, Here along with system view you can see the view that we are created right now.

So, this is our user define View. If we right click on it, we will get the option of open the view and which will show the result of View.

We can also create a view from a view itself as similar way that we already done with tabl

Você também pode gostar