第五章课后题

本文详细讲解了如何使用SQL语言实现数据库模式设计,包括职工和部门的关系模式,并定义了主码、参照完整性和职工年龄限制。此外,还介绍了针对学生-课程数据库的存储过程,涉及成绩分布统计、平均成绩计算及成绩转换为等级制的功能实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据库完整性

6、假没有下面两个关系模式:
职工(职工号,姓名,年龄,职务,工资,部门号),其中职工号为主码;
部门(部门号,名称,经理名,电话),其中部门号为主码。
用SOL语言定义这两个关系模式,要求在模式中完成以下完整性约束条件的定义:
(1)定义每个模式的主码; (2) 定义参照完整性; (3) 定义职工年龄不得超过60岁。

--部门(部门号,名称,经理名,电话)
CREATE TABLE DEPARTMENT
(Deptno CHAR(15) PRIMARY KEY,--主码
Dname CHAR(20),
Manager CHAR(15),
Telephon CHAR(15)
)
--职工(职工号,姓名,年龄,职务,工资,部门号)
CREATE TABLE  STAFF
(Stno CHAR(15) PRIMARY KEY,--主码
Stname CHAR(20),
Stage INT CHECK(Stage <= 60),--职工年龄不得超过60岁。
Job CHAR(15),
Salary INT,
Deptno CHAR(15)
FOREIGN KEY(Deptno) REFERENCES DEPARTMENT(Deptno)--参照完整性
)

第八章作业

第八章作业:习题二 2、对学生-课程数据库编写存储过程,完成下述功能:
(1) 统计离散数学的成绩分布情况,即按照各分数段统计人数。
(2)统计任意一门课的平均成绩。 建立平均成绩表。
(3)将学生选课成绩从百分制改为等级制(即A、B、C、D、E)。

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

①先建个表
②建个有统计的存储过程

CREATE TABLE Grade(
	Score char(10),
	Num int
);
insert into gradesum_8(rank,total) values(''[0,60)'',NULL);
insert into gradesum_8(rank,total) values('[60,70)',NULL);
insert into gradesum_8(rank,total) values('[70,80)',NULL);
insert into gradesum_8(rank,total) values('[80,90)',NULL);
insert into gradesum_8(rank,total) values('[90,100]',NULL);

create procedure Proc_rank
as
begin  
   	declare	/*定义变量*/
	@Cno char(4),
	@less60 int, -- [0,60)
	@less70 int,-- [60,70)
	@less80 int, -- [70,80)
	@less90 int, -- [80,90)
	@less100 int -- [90,100]
	

	select @Cno = Cno from Course where Cname = '离散数学';
	select @less60 = count(*) from SC where Grade < 60 and Cno = @Cno;
	select @less70 = count(*) from SC where Grade >= 60 and Grade < 70 and Cno = @Cno;
	select @less80 = count(*) from SC where Grade >= 70 and Grade < 80 and Cno = @Cno;	
	select @less90 = count(*) from SC where Grade >= 80 and Grade < 90 and Cno = @Cno;
	select @less100 = count(*) from SC where Grade >= 90 and Grade < 100 and Cno = @Cno;
	

	update Grade set num = @less60 where score = '[0,60)';
	update Grade set num = @less70 where score = '[60,70)';
	update Grade set num = @less80 where score = '[70,80)';
    update Grade set num = @less90 where score = '[80,90)';
	update Grade set num = @less100 where score = '[90,100]';
end;

--分开执行

exec Proc_rank; 
select * from SC where Cno = 8;
select * from rank

(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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值