SQL Server 如何创建视图

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

 

posted @ 2018-10-27 11:31  Zz-Plus  阅读(3758)  评论(0编辑  收藏  举报