A View is a logical or virtual table,which does not exists physically in the database.View is an object which contains 'select' statement.View is consider like as Virtual table.
We create a view for two purpose.
We create a view for two purpose.
- Security region --> If we don't want to show all the column's data of the table to an user then we generally create a view.
- To make complex query simple.
I have already created the tables studentdetails and scourse in joins in sql server. I will explain view with the help of these tables.
create view vdata1(student_id,student_name,student_course)
as
select sid,sname,cname from scourse c Left join studentdetails s
on c.id=s.sid
Output:-
Create view with Right Outer Join:-
create view vdata2(student_id,student_name,student_course)
as
select sid,sname,cname from scourse c Right join studentdetails s
on c.id=s.sid
Output:-
Create view with Full Outer Join:-
create view vdata3(student_id,student_name,student_course)
as
select sid,sname,cname from scourse c full outer join
studentdetails s on c.id=s.sid
Output:-
Syntax:-
select *from view_name
Ex.
select*from vdata
To get the details of view:-
sp_help vdata
sp_helptext vdata
output:-
Drop view and View Name:-
Drop view vdata
Insert,Delete,Update records within a table through view:-We can insert ,delete,update the records within a table through a view,but only two conditions.
- View must contain only one table within select query(no join tables).
- Select query in view must contain all not nullable columns.
Step1:-First create a studentdetails table as above shown-->Create constraint not nullable column if table is already created as given below:
alter table studentdetails alter column sage int not null
Step2:- Now Create View with all not nullable column otherwise you can not insert data through view.
create view myview1
as
select sid,sname,sage from studentdetails
Step3:-Insert data in studentdetails table through myview1.-->you can easily insert and update the table through myview1 because you have selected all not nullable column.
insert into myview1 values(109,'kajal',21)
Output:-
Step4:-Update values in studentdetails tables through myview1.
update myview1 set sage=50
where sid=106
output:-
Step5:- Now i am going to create a new view (myview2) as given below:-
create view myview2
as
select sid,sname from studentdetails
Step6:- Now insert values in studentdetails table through myview2,Then it will give error as shown below.
insert into myview2 values(110,'karan',21)
Note:- you can insert and update records in tables through view when table has no constraints(not nullable).if any column is not nullable the you have to follow the above steps otherwise you can not insert and update the records through view.
For More:-
- Default constraints and check constraints
- solve sql server problems
- Multithreading in c#
- create captcha image
- File handling real application
- Interview questions and Answers
- Send mail from asp.net application free
To Get the Latest Free Updates Subscribe
0 comments:
Post a Comment