blog

Home / DeveloperSection / Blogs / SQL View

SQL View

Anchal Kesharwani 3161 12-Aug-2014

In this blog describe the concept of sql view in sql server. SQL view is a virtual table. Here we give simple example of view.

A View is a Virtual Table. It is not like a simple table, but is a virtual table which contains columns and data from different tables (may be one or more tables). A View does not contain any data, it is a set of queries that are applied to one or more tables that is stored within the database as an object. Once you have defined a view, you can reference it like any other table in a database.

Views are used for security purposes because they provide encapsulation of the name of the table. Data is in the virtual table, not stored permanently. Views display only selected data.

Syntax
CREATE VIEW <view name>
 AS
SELECT statement
Here we create the table of department and employee.
create table Departments(
 
DepartID int identity(1,1) primary key,
DepartmentName varchar(50)
)
create table Employee(
 
EmpID int identity(1,1) primary key,
FirstName varchar(50),
LastName varchar(50),
DepartID int references Departments(DepartID)
)

Example 1

This is an example is a example of simple view which contain only one table.

CREATE VIEW ShowEmployeeData
 AS
SELECT * FROM Employee
SELECT * FROM ShowEmployeeData

Output

SQL View

Example 2

This is an example for multiple table using join. It is also known as complex view.

CREATE VIEW ShowEmployee
 AS
SELECT e1.FirstName,e1.LastName,e2.DepartmentName FROM Employee e1 INNER JOIN Departments e2
ON e1.DepartID = e2.DepartID

Output


SQL View


Updated 18-Sep-2014

Leave Comment

Comments

Liked By