Sql Server :第五章作业

假设有下面两个关系模式:


职工(职工号,姓名,年龄,职务,工资,部门号), 其中职工号为主码;
部门(部门号,名称,经理名,电话),其中部门号为主码。
用 SQL 语言定义这两个关系模式,要求在模式中完成以下完整性约束条件的定义:

  1. 定义每个模式的主码;
  2. 定义参照完整性;

  3. 定义职工年龄不得超过60岁

create table Dep
( Dnum int primary key,
Dname char(10),
Dmname char(10),
Dcall char(20));

create table Staff
( Snum char(10) primary key,
  Sname char(10),
  Sage int,
  constraint C1 check (Sage<=60),--完整性约束
  Swork char(10),
  Ssal char(10),
  Sdep int 
  foreign key(Sdep) references Dep(Dnum));

 

 

对学生-课程数据库编写存储过程, 完成下述功能:


(1)统计离散数学的成绩分布情况,即按照各分数段统计人数。

建表

create table D_math
(
   rankGrade char(20), --成绩段
	num int --每个成绩段的人数
);
create procedure SepGrade(
@name char(10))
as
declare
 @less60 int,
 @f60t69 int,
 @f70t79 int,
 @f80t89 int,
 @f90t100 int,
 @cpo char(10); -- 用来对应课程号
 begin
 select @cpo=Cno from Course
 where Cname =@name; --这里替换想成绩分段的科目
 if @cpo is null
 begin 
 print '未录入'+@name+'这门课课程'
 rollback ;
 return 
 end;
 select @less60=count(*)  from SC
 where Grade < 60 and Cno =@cpo;

  select @f60t69 =count(*)  from SC
 where Grade <= 69 and Grade >=60 and Cno =@cpo;

  select @f70t79=count(*)  from SC
 where Grade <=79 and  Grade >=70 and Cno =@cpo;
 
  select @f80t89=count(*)  from SC
  where Grade <=89 and  Grade >=80 and Cno =@cpo;

  select @f90t100=count(*)  from SC
  where Grade <=100 and  Grade >=90 and Cno =@cpo;
  end;
 update D_math set num = @less60 where rankGrade = '[0,60)';
update D_math set num = @f60t69 where rankGrade = '[60,69]';
update D_math set num = @f70t79 where rankGrade = '[70,79]';
update D_math set num = @f80t89 where rankGrade = '[80,89]';
update D_math set num = @f90t100 where rankGrade = '[90,100]';


(2)统计任意一门课的平均成绩。

建表

create table Avg_Grade
(Cname char(10), --课程名称
 Cno int, --课程号
 A_Gra int --该课程的平均成绩
 );
create procedure Cal_avg(
@Cno int)
as
declare
@Cpo int,
@grade int,
@Cname char (10);
select @Cpo = Cno   from Course
where Cno = @Cno; --对应这门课的
select  @Cname =Cname  from Course
where Cno = @Cno;
if @Cpo is null
begin
print '未录入'+@Cno+'这门课'
rollback;
return 
end;
select @grade = avg(Grade) from Sc
where Cno =@Cpo;
update Avg_Grade set A_Gra =@grade where Cno =@Cpo and Cname =@Cname;

(3)将学生选课成绩从百分制改为等级制 (即A、 B、C、D、E)

 

create procedure Value_Grade
(@grade int,@Cno int,@Sno char(10))
as
declare 
@A char(5),
@B char(5),
@C char(5),
@D char(5),
@E char(5),
@Cpo int,
@Spo char(10);
select @Cpo = Cno from SC where @Cno =Cno;
if @Cpo is null
begin
print '没有该课程'
rollback;
return
 end;
 select @Spo = Sno from SC where @Sno =Sno;
 if @Cpo is null
begin
print '没有该学生'
rollback;
return
 end;
 
 update SC set Val ='A' where @grade >=90;
 update SC set Val ='B' where @grade >=80 and @grade <=89;
 update SC set Val ='C' where @grade >=70 and @grade <=79;
 update SC set Val ='D' where @grade >=69 and @grade <=60;
 update SC set Val ='E' where @grade <60;

 

 

 

如果你也出现了 新建了一个表或列,但是在编写procedure的时候显示对象名无效 可以采用下面图的方法 可以有效解决

 

 

Sql Server :第五章作业_第1张图片

你可能感兴趣的:(DB实验,sql)