SQL VIEW

 

SQL CREATE VIEW Statement

In SQL, a view is a virtual table based on the result-set of an SQL statement. A view is a virtual table whose contents are defined by a query. Like a table, a view consists of a set of named columns and rows of data. Unless indexed, a view does not exist as a stored set of data values in a database. The rows and columns of data come from tables referenced in the query defining the view and are produced dynamically when the view is referenced

A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.

You can add SQL statements and functions to a view and present the data as if the data were coming from one single table.

A view is created with the CREATE VIEW statement. 

Types of views

Besides the standard role of basic user-defined views, SQL Server provides the following types of views that serve special purposes in a database.

Indexed Views

An indexed view is a materialized view. This means the view definition has been computed and the resulting data stored just like a table. You index a view by creating a unique clustered index on it. Indexed views can dramatically improve the performance of some types of queries. Indexed views work best for queries that aggregate many rows. They are not well-suited for underlying data sets that are frequently updated.

Partitioned Views

A partitioned view joins horizontally partitioned data from a set of member tables across one or more servers. A partitioned view makes the data appear as if from one table. A view that joins member tables on the same instance of SQL Server is a local partitioned view.

System Views

System views expose catalog metadata. You can use system views to return information about the instance of SQL Server or the objects defined in the instance.

CREATE VIEW Syntax

CREATE VIEW view_name  AS

Select Column1,Column2, Column3, ...

FROM table_name

WHERE condition:

Table named Grahak with column and rows with Values

CustomerID

CustomerName

Contactnumber

Address

City

PostalCode

Country

1

Ram

9871234563

#47 Sector 22

Chandigah

160002

Japan

2

Sham

9876543216

#4110 Sector 122

Mohali

140002

India

3

Shivam

3278955152

#5 Sector 88

Gurgaon

180002

UAE

4

Ravi

2277894562

#5 Sector 48

Badlapur

220002

North korea

Example

CREATE VIEW [Indian_Grahak] AS
SELECT CustomerName, ContactName
FROM GRAHAK
WHERE Country = 'India';

We can query the view above as follows:

Example

SELECT * FROM [Indian_Grahak];

 

 

 

Comments

Popular posts from this blog

Sending reset password link for one time use only in asp.net

add delete update inside gridview using store procedure in ASP.NET

Change password using asp.net