1、為什么要使用視圖
1.進行多表聯合查詢時,編寫程序時每調用一次需編寫一次,不太現實,容易出錯;
這時把所需的數據集於視圖中,訪問視圖即可
2.避免直接訪問基表
2、視圖可以做什么
增、刪、改、查
3、實例
//所需數據
//在SQL Sever數據庫中創建表
create table tb_stu
(
stuNo int primary key,
stuName varchar(10) not null,
stuSex varchar(2) null,
stuAge int not null
)
insert into tb_stu values(1,'小張','男',18)
insert into tb_stu values(2,'小麗','女',17)
insert into tb_stu values(3,'小明','男',18)
/*創建視圖1*/
create view view_stu1
as
select*from tb_stu
/*創建視圖2*/
create view view_stu2
as
select stuNo,stuName,stuSex from tb_stu
/*查詢所有視圖*/
select name from sysobjects where xtype='v'
/*查詢視圖1*/
select*from view_stu1
/*查詢視圖2*/
select*from view_stu2
/*向視圖1插入數據*/
insert into view_stu1 values(4,'李麗','男',18)
/*通過視圖1刪除數據*/
delete from view_stu where stuNO=4
/*通過視圖1更改數據*/
update view_stu1 set stuSex='女' where stuNo=4
