sqlserver 视图
/*drop table scourse
drop table course
drop table student
drop table major*/
create database db
use db
--专业表
create table major
(spno char(5) not null primary key,
spname varchar(20) not null,
pno char(2) )
--学生表
create table student
(sno char(7) not null primary key,
sname varchar(20) not null,
ssex char(2) not null,
sage smalldatetime ,
spno char(5) not null foreign key references major(spno),
classid char(5),
Inyear char(4) not null )
--课程表
create table course
(cno char(10) not null primary key,
cname varchar(20) not null,
credit smallint ,
tno char(3))
--选课表
create table scourse
(sno char(7) not null foreign key references student(sno),
cno char(10) not null foreign key references course(cno),
Gmark numeric(4,1),
primary key(sno,cno))
/*(1)建立00312专业选修了001号课程的学生视图Stu_01312_1;
(2)建立00312专业选修了1号课程并且成绩不及格的学生视图Stu_00312_2;
(3)建立视图Stu_year,由学生学号、姓名、年龄组成;
(4)通过视图查询20岁以上的学生姓名;
(5)通过视图查询00312专业选修了1号课程并且成绩不及格的学生的学号、姓名、年龄。*/
//第一题
drop view Stu_01312_1
go
create view Stu_01312_1 as select * from student where spno='00312' and sno in (select sno from scourse where cno='1')
go
select * from Stu_01312_1
//第二题
drop view Stu_00312_2
go
create view Stu_00312_2 as select * from student where spno='00312' and sno in(select sno from scourse where cno='1' and Gmark < 60)
go
select * from Stu_00312_2
//第三题
drop view Stu_00312_3
go
create view Stu_00312_3 as select sno,sname,sage from student
go
//第四题
select sname from Stu_00312_3 where sage >20
//第五题
select sno,sname,sage from Stu_00312_2
相关文档:
select case when c.colid=1 then object_name(c.id) else '' end as 表名
,c.name as 字段名
,t.name 数据类型
,c.prec as 长度 ......
游标(Cursor)是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次以行或者多行前进或向后浏览数据的能力。我们可以把游标当作一个指针,它可以指定结果中的任何位置,然后允许用户对指定位置的数据进行处理。
1.游标的组成
......
Sql Server的存储过程是一个被命名的存储在服务器上的Transacation-Sql语句集合,是封装重复性工作的一种方法,它支持用户声明的变量、条件执行和其他强大的编程功能。
存储过程相对于其他的数据库访问方法有以下的优点:
&nbs ......
写了一个存储过程对视图进行分页查询,但数据增多后发现基效率低得要命,三万多条数据要查询一个半小时都没出来,这不是要了命,于是想到了索引,应用过后仍无济于事。最后对sql进行分析和实践中得出,使用临时表可以大大加快视图的查询速度,见如下sql语句
性能超低的视图分页sql语句:
select top 100 * from
view_c ......