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
相关文档:
世事洞明皆学问,人情练达即文章。做ASP时,最常用的数据库即Sqlserver与Access数据库莫属了!
但使用会经常发现很多SQL执行的问题。这里整理出之间的差异,做个十大差异的总结。
ACCESS结构简单容易处理,而且也能满足多数的网站程序要求,也是初学者的试牛刀。
ACCESS是小型数据库,既然是小型就有他根本的局限性:
......
转换方法: convert(nvarchar(8),starttime,14)
100 (1, 2)
默认设置
mon dd yyyy hh:miAM(或 PM)
101
&nbs ......
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,
sag ......
写了一个存储过程对视图进行分页查询,但数据增多后发现基效率低得要命,三万多条数据要查询一个半小时都没出来,这不是要了命,于是想到了索引,应用过后仍无济于事。最后对sql进行分析和实践中得出,使用临时表可以大大加快视图的查询速度,见如下sql语句
性能超低的视图分页sql语句:
select top 100 * from
view_c ......