(1)统计离散数学的成绩分布情况,即按照各分数段统计人数
create table Lisan(
Score_lisan char(20),
Count_lisan int );
-----首先创造统计离散数学的表格,其中包含了int的人数count和score的分数段
insert into Course values('8','离散数学',NULL,4)--向Course中添加离散数学
insert into SC values('201215121','8',45);
insert into SC values('201215122','8',65);
insert into SC values('201215123','8',78);
insert into SC values('201215125','8',82);--查看添加结果
select * from Course ;
select * from SC where Cno='8';--查看添加结果
然后我们就开始设计进程了
create procedure Grade_add
as
declare
@less60 int,
@60to70 int,
@70to80 int,
@80to90 int,
@90to100 int
select @less60 = count(*)
from SC
where Grade <=60 and Cno='8';
select @60to70 = count(*)
from SC
where Grade > 60 and Grade <=70 and Cno='8';
select @70to80 = count(*)
from SC
where Grade > 70 and Grade<=80 and Cno='8';
select @80to90 = count(*)
from SC
where Grade > 80 and Grade <= 90 and Cno='8';
select @90to100 = count(*)
from SC
where Grade>90 and Grade<=100 and Cno='8';
-----需要说的是如果我们不知道预先的课程号的时候可以额外设置一个变量来存储查找出的课程号。
insert into Lisan values('<=60',@less60);
insert into Lisan values('60~70',@60to70);
insert into Lisan values('70~80',@70to80);
insert into Lisan values('80~90',@80to90);
insert into Lisan values('90~100',@90to100);----
这里我是因为直接建表,其实更常规的操作应该是使用update来操作
exec Grade_add;
select * from Lisan;
(2)统计任意一门课的平均成绩。
drop table if exists AveSC;
create table AveSC(
Cno char(4), --课程号
CNAME CHAR(40), -- 课程名
AvgScore FLOAT, --平均分
foreign key(Cno) references Course(Cno)
);
insert into AveSC--子查询结果插入
select Cno,Cname,0
from Course;
select * from AveSC;
ALTER procedure [dbo].[Avgcore]
as
declare
@S1 float,
@S2 float,
@S3 float,
@S8 float;
select @S1 = avg(Grade)
from SC
where Cno = '1'
select @S2 = avg(Grade)
from SC
where Cno = '2'
select @S3 = avg(Grade)
from SC
where Cno = '3'
select @S8 = avg(Grade)
from SC
where Cno = '8'
update AveSC set AvgScore = @S1 where Cno = '1';
update AveSC set AvgScore = @S2 where Cno = '2';
update AveSC set AvgScore = @S3 where Cno = '3';
update AveSC set AvgScore = @S8 where Cno = '8';
---这里我就列举了几种课程,如果想要全列出来建议使用循环的方法如下。
drop procedure if exists AvgCourse;
go
create procedure AvgCourse
as
declare
@i int,
@avg int,
@sum int;
begin
set @i=1
select @sum=count(*)
from AvgSC;
while @i<=@sum
begin
select @avg=Avg(Grade)
from SC,AvgSC
where SC.Cno=@i;
update AvgSC set AvgScore=@avg where AvgSC.Cno=@i;
set @i=@i+1;
end;
end;
exec Avgcore;
select* from AveSC;
declare @i int
set @i=0
while @i<5
begin
<运行内容>
set @i=@i +1;
end
sql server while怎么用
(3)将学生选课成绩从百分制改为等级制(即A、B、C、D、E):
alter table SC drop column if exists Score_level ;
alter table SC add Score_level char(4);
drop procedure if exists Createlevel;
go
create procedure Createlevel
as
begin
update SC set Score_level = 'E' where Grade < 60;
update SC set Score_level = 'D' where Grade >= 60 AND Grade < 70;
update SC set Score_level = 'C' where Grade >= 70 AND Grade < 80;
update SC set Score_level = 'B' where Grade >= 80 AND Grade < 90;
update SC set Score_level = 'A' where Grade >= 90;
end;
go
exec Createlevel;
select* from SC;
假设有下面两个关系模式:
职工(职工号,姓名,年龄,职务,工资,部门号), 其中职工号为主码;
部门(部门号,名称,经理名,电话),其中部门号为主码。
用 SQL 语言定义这两个关系模式,要求在模式中完成以下完整性约束条件的定义:
1、定义每个模式的主码;
2、定义参照完整性;
3、定义职工年龄不得超过60岁
create table Dept
(
Dno char(9) primary key,
Dname char(20),
Mname char(20),
Tel char(20)
);
create table Staff
(
Sno char(9) primary key,
Sname char(20),
Ssex char(2),
Sage smallint,
constraint C1 check(Sage <= 60),
Post char(20),
Pay char(20),
Dno char(9),
foreign key (Dno) references Dept(Dno)
);
感觉sql语句里面这一块和触发器算是天花板了hhh,因为sql语句相差很大,而且方法思路很多,而且前天做的触发器感觉也模糊了,记忆冲撞有点厉害hhh,比如再写alter table的时候下意识的就加了个on。也是有点无语。看来总结还是很有必要的,