视图建立引索失败 怎么办?还是找课本打的
没找到错误 谢谢大家了
Use Student
Go
Create view xk_view
With schemabinding
As
Select dbo.stab.xh 学号,dbo.stab.xm 姓名,COUNT_big(*) 选课门数,isnull(sum(dbo.sctab.cj),0) 平均成绩
From dbo.stab,dbo.sctab
Where dbo.stab.xh=dbo.sctab.xh
Group by dbo.stab.xm,dbo.stab.xh
Go
Create Unique clustered index xk_index
On xk_view(学号,姓名)
Go
--无法创建..........包含聚合函数或组合列的结果表达式。列表中删除聚合函数或组合列的结果表达式。
---------------------建库数据---------------------------------------------------------
Create Database Student
On Primary
(Name = student_data ,
Filename = 'E:\大学生选课系统\student.mdf' ,
Size = 10MB ,
Maxsize = 100MB ,
Filegrowth = 15%) ,
(Name = student_dat ,
Filename = 'E:\大学生选课系统\student1.ndf' ,
Size = 10MB ,
Maxsize = 100MB ,
Filegrowth = 15%)
Log On
(Name = student_log ,
Filename = 'E:\大学生选课系统\student.ldf' ,
Size = 5MB ,
Maxsize = 25MB ,
Filegrowth = 5MB)
Go
--创建学生信息表
Use Student
Go
Create Table stab
(xh char(6) PRIMARY KEY ,
xm varchar(8) NOT NULL ,
xb char(2) NOT NULL DEFAULT'男' ,
csrq date NOT NULL ,
rxsj date NOT NULL ,
ssx varchar(20) NOT NULL ,
bj varchar(20) NOT NULL ,
dh char(11)
)
Go
--创建教师信息表
Use Student
Go
Create Table ttab
(jsh char(4) PRIMARY KEY ,
xm varchar(8) NOT NULL ,
xb char(2) NOT NULL DEFAULT'男' ,
csrq date NOT NULL ,
rjsj date NOT NULL ,
xf varchar(10) ,
zy varchar(10) ,
zc varchar(10) ,
ssx varchar(20) NOT NULL ,
dh char(11)
)
GO
--创建课程表信息
Use Student
Go
Create Table ctab
(kch char(3) PRIMARY KEY ,
kcm varchar(20) NOT NULL UNIQUE ,
xxkch char(3) ,
xf tinyint NOT NULL CHECK(xf>=1 and xf<=5) ,
xs tinyint NOT NULL CHECK(xs>=20 and xs<=120) ,
sf smallint NOT NULL CHECK(sf>=100 and sf<=300) ,
)
Go
--创建教师课程信息表
Use Student
Go
Create Table tctab
(jsh char(4) NOT NULL REFERENCES ttab(jsh) ,
kch char(3) NOT NULL REFERENCES ctab(kch) ,
cj smallint NOT NULL CHECK(cj>=200 and cj<=1000) ,
PRIMARY KEY(jsh,kch)
)
Go
--创建学生选课信息表
Use Student
Go
Create Table sctab
(xh char(6) NOT NULL REFERENCES stab(xh) ,
kch char(3) NOT NULL ,
jsh char(4) NOT NULL ,
cj decimal(4,1) CHECK(cj>=0 and cj<=100) ,
PRIMARY KEY(xh,kch) ,
FOREIGN KEY(jsh,kch) REFERENCES tctab(jsh,kch) ,
--REFERENCES ttab(jsh) ,REFERENCES ctab(kch) ,
)
Go
--
/* --向学生表添加数据*/
Use Student
Go
Insert Into stab values('100013','张闻天','女','1990-9-8','2010-9-1',
'机械系','10模具','1589766690')
Insert Into stab VALUES('100018','孙晓亮','男','1989-5-28','2009-9-1',
'电气系','09机电','13546780987')
Insert Into stab VALUES ('100017','王菲','女','1991-12-1','2011-9-2',
'计算机系','网络','15897777229')
Go
--插入stab (学生信息表)
Use Student
Go
insert into stab values('100001','孙大有','男','1989-6-23','2009-9-4','电气系','09机电','13509877890')
insert into stab values('100002','王芳','女','1990-12-9','2008-9-4','会计系','08会计','15845677654')
insert into stab values('100003','刘晓亮','男','1988-5-4','2008-9-4','电气系','08电汽','13512344321')
insert into stab values('100004','孙晓光','男','1989-7-2','2009-9-4','电气系','09机电','13509870000')
insert into stab values('100005','陈玉霞','女','1990-1-19','2008-9-4','计算机系','08网络','15832857837')
insert into stab values('100006','黑昊天','男','1988-5-24','2008-9-4','计算机系','08应用','13578653876')
insert into stab values('100007','陈江中','男','1990-1-12','2009-9-4','会计系','08投资','13553467233')
insert into stab values('100008','汪云','女','1989-11-23','2008-9-4','会计系','09机电','13558377636')
insert into stab values('100009','胡满霞','女','1990-5-4','2008-9-4','计算机系','08应用','13587482387')
insert into stab values('100010','良天','男','1989-9-4','2008-9-4','会计系','08会计','15838728522')
insert into stab values('100011','程云丽','女','1990-8-7','2009-9-4','会计系','09会计','13567345554')
insert into stab values('100012','孙大鹏','男','1988-12-23','2008-9-4','计算机系','08网络','15834378934')
go
--插入ttab(教师信息表)
Use Student
Go
insert into ttab values('0001','孙科','男','1963-6-23','1985-7-1','本科','数学','教授','数学系','13239877890')
insert into ttab values('0002','张云','女','1982-12-9','2005-7-4','硕士','数学','讲师','数学系','13449877890')
insert into ttab values('0003','刘天','男','1975-5-4','1995-9-4','本科','会计','副教授','会计系','15109877890')
insert into ttab values('0004','孙一','男','1961-6-2','1983-7-1','本科','物理','教授','电气系','13505277890')
insert into ttab values('0005','张霞','女','1985-1-9','2004-7-4','博士','电气','讲师','电气系','13504577890')
insert into ttab values('0006','刘豪','男','1973-4-4','1993-9-4','本科','机械','副教授','机械系','13679877890')
insert into ttab values('0007','王云','女','1965-9-9','1988-7-4','硕士','制造','教授','机械系','13504777890')
insert into ttab values('0008','李飒','男','1986-9-3','2005-7-1','硕士','会计','讲师','会计系','13525877890')
Go
--插入ctab(课程信息表)
Use Student
Go
insert into ctab values('C1','高等数学',null,'5','120','300')
insert into ctab values('C2','大学物理','C1','4','90','200')
insert into ctab values('C3','会计基础',null,'2','70','160')
insert into ctab values('C4','企业会计','C3','3','80','185')
insert into ctab values('C5','计算机基础',null,'2','70','200')
insert into ctab values('C6','程序设计','C1','3','72','180')
insert into ctab values('C7','数据结构','C6','4','90','200')
insert into ctab values('C8','电子技术','C2','3','72','180')
go
----插入tctab(教师教程信息表)
Use Student
Go
insert into tctab values('0001','C1','900')
insert into tctab values('0001','C5','870')
insert into tctab values('0001','C6','910')
insert into tctab values('0002','C1','700')
insert into tctab values('0002','C5','740')
insert into tctab values('0002','C6','710')
insert into tctab values('0003','C3','800')
insert into tctab values('0003','C4','850')
insert into tctab values('0004','C2','920')
insert into tctab values('0004','C5','900')
insert into tctab values('0004','C6','950')
insert into tctab values('0005','C2','750')
insert into tctab values('0005','C8','720')
go
----插入sctab(学生选课信息表)
Use Student
Go
insert into sctab values('100001','C1','0001',65)
insert into sctab values('100001','C2','0005',87)
insert into sctab values('100001','C3','0003',73)
insert into sctab values('100002','C1','0002',90)
insert into sctab values('100002','C3','0003',56)
insert into sctab values('100002','C4','0003',67)
insert into sctab values('100002','C5','0002',82)
insert into sctab values('100003','C2','0005',76)
insert into sctab values('100003','C5','0002',49)
insert into sctab values('100004','C1','0001',90)
insert into sctab values('100004','C2','0005',59)
insert into sctab values('100004','C3','0003',87)
insert into sctab values('100009','C1','0001',78.9)
insert into sctab values('100009','C2','0005',56)
insert into sctab values('100009','C5','0002',98.3)
insert into sctab values('100009','C6','0001',67)
insert into sctab values('100012','C1','0001',53)
insert into sctab values('100012','C5','0002',89)
insert into sctab values('100012','C6','0001',91.3)
Go
------解决思路----------------------
alter view xk_view
With schemabinding
As
Select dbo.stab.xh 学号,dbo.stab.xm 姓名,sum(isnull(dbo.sctab.cj,0)) 平均成绩,COUNT_big(*) 选课门数
From dbo.stab,dbo.sctab
Where dbo.stab.xh=dbo.sctab.xh
Group by dbo.stab.xm,dbo.stab.xh
Go
Create Unique clustered index xk_index
On xk_view(学号,姓名)
try this
------解决思路----------------------
一开始我以为是版本原因,现在看来不是,楼主把聚合包在ISNULL里面了,我猜想SQLSERVER没认出这是聚合,囧~~
如果把ISNULL包在SUM里面应该就可以了