Views
View is a virtual table. It can contain columns from one or more table .View appears just like a real table, with a set of named columns and rows of data. SQL creates the illusion like table we can even
insert,update,delete using views
Syntax to create view:
create view <NameofView>
As
--- write select query here
Syntax to execute view
select * from <NameOfView>
Eg. Following is the view that retrieves the employees of Admin department. View keyword is used when creating view.
Create View adminEmployees
As
Select * from Employees where DeparmentName ='Admin'
Executing the view
select * from adminEmployees
If a view is created from one table then it can also be used to insert,update,delete values to the table
Insert into adminEmployees
Values('Priya','5/5/2010','Admin')
Insert into adminEmployees
Values('Puja','5/15/2010','IS')
The above statement will insert value to Employee table.
Query 1:
Select name from Employee
Result;
Priya
Puja
Query 2:
select name from adminEmployees
Result:
Priya
Explanation:
Employee table contains both the employee of all departments but the adminEmployee view
displays Employee of admin department.
With Check Option:
If we use with check option when we create view, we can restrict modification to the table based
on the condition specified in the view. That is insert/update/delete is based on the condition
specified in the view.
Create View adminEmployees
As
Select * from Employees where DeparmentName ='Admin'
with check option
Reference Link
http://www.c-sharpcorner.com/Blogs/10575/advantages-and-disadvantages-of-views-in-sql-server.aspx
No comments:
Post a Comment