2021-04-25

第五章作业

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

create table 部门
(
	部门号 varchar(10),
	名称 varchar(10),
	经理名 varchar(10),
	电话 varchar(15)
	constraint dept_key primary key(部门号) --主码
);

create table 职工
(
	职工号 varchar(10),
	姓名 varchar(10),
	年龄 int,
	职务 varchar(10),
	工资 int,
	部门号 varchar(10),
	constraint worker_key primary key(职工号), --主码
	constraint worker_age_border check(年龄<=60), --年龄限制
	constraint worker_foreign_dept foreign key(部门号) references 部门(部门号)
);

对学生-课程数据库编写存储过程,完成下述功能。
(1)统计离散数学的成绩分布情况,即按照个分数段统计人数。

create procedure divide_rank
as
	declare score_90_100 int,
			score_80_90 int,
			score_70_80 int,
			score_60_70 int,
			score_0_60 int,
			score int;
			
	select score_90_100=count(*) from SC,Course where SC.Cno=Course.Cno and Grade>=90 and Grade<=100 and Cname='离散数学' ;
	select score_80_90 =count(*) from SC,Course where SC.Cno=Course.Cno and Grade>=80 and Grade<90 and Cname='离散数学' ;
	select score_70_80 =count(*) from SC,Course where SC.Cno=Course.Cno and Grade>=70 and Grade<80 and Cname='离散数学' ;
	select score_60_70 =count(*) from SC,Course where SC.Cno=Course.Cno and Grade>=60 and Grade<70 and Cname='离散数学' ;
	select score_0_60  =count(*) from SC,Course where SC.Cno=Course.Cno and Grade>=0 and Grade<60 and Cname='离散数学' ;
	
	select score_90_100 as '[90,100]',score_80_90  as '[80,90)',score_70_80  as '[70,80)',score_60_70  as '[60,70)',score_0_60   as '[0,60)';

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

create procedure AVG_Cscore(course_name char(10))
as
	select avg(Grade) 
	from SC,Course 
	where SC.Cno=Course.Cno and Course.Cname=course_name

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

create procedure score_to_level()
as
	update SC set Grade_level='A' where Grade>=90 and Grade<=100;
	update SC set Grade_level='B' where Grade>=80 and Grade<90;
	update SC set Grade_level='C' where Grade>=70 and Grade<80;
	update SC set Grade_level='D' where Grade>=60 and Grade<70;
	update SC set Grade_level='E' where Grade>=0 and Grade<60;

你可能感兴趣的:(sql)