【数据库】作业14——第五章: 数据库完整性 习题 + 存储过程
程序员文章站
2022-05-10 08:06:03
...
作业原地址:作业
目录
6.
假设有下面两个关系模式:
职工(职工号,姓名,年龄,职务,工资,部门号),其中职工号为主码;
部门(部门号,名称,经理名,电话),其中部门号为主码。
用 SQL 语言定义这两个关系模式,要求在模式中完成以下完整性约束条件的定义:
(1)定义每个模式的主码;(2)定义参照完整性;(3)定义职工年龄不得超过60岁。
Staff(Sno,Sname,Sage,Spost,Wage,Dno)
Dept(Dno,Dname,Mname,Dtel)
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)
);
2.
对学生-课程数据库编写存储过程, 完成下述功能:
添加数据:
INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('5','离散数学',3,4);
INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215121','5',20);
INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215122','5',60);
INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215123','5',70);
INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215124','5',80);
INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215125','5',90);
成绩表:
CREATE TABLE SCC
(Score CHAR(10), /*成绩分段*/
Num SMALLINT /*学生人数*/
);
INSERT INTO SCC(Score,Num) VALUES('[0,60)',0); /*0——60*/
INSERT INTO SCC(Score,Num) VALUES('[60,70)',0); /*60——70*/
INSERT INTO SCC(Score,Num) VALUES('[70,80)',0); /*70——80*/
INSERT INTO SCC(Score,Num) VALUES('[80,90)',0); /*80——90*/
INSERT INTO SCC(Score,Num) VALUES('[90,100]',0); /*90——100*/
SELECT * FROM SCC
(1)
统计离散数学的成绩分布情况,即按照各分数段统计人数。
IF (exists (select * from sys.objects where name = 'Stats'))
DROP PROCEDURE count0
GO
CREATE PROCEDURE count0 /*定义存储过程*/
AS
DECLARE /*定义变量*/
@0to60 INT, /*成绩小于60的人数*/
@60to70 INT, /*成绩大于60小于70的人数*/
@70to80 INT, /*成绩大于70小于80的人数*/
@80to90 INT, /*成绩大于80小于90的人数*/
@90to100 INT, /*成绩大于90小于100的人数*/
@CNO CHAR(8);
SELECT @CNO=Cno
FROM SC
WHERE Cname='离散数学';
SELECT @0to60=COUNT(*)
FROM SC
WHERE Grade<60 AND aaa@qq.com
SELECT @60to70=COUNT(*)
FROM SC
WHERE Grade<70 AND Grade>=60 AND aaa@qq.com
SELECT @70to80=COUNT(*)
FROM SC
WHERE Grade<80 AND Grade>=70 AND aaa@qq.com
SELECT @80to90=COUNT(*)
FROM SC
WHERE Grade<90 AND Grade>=80 AND aaa@qq.com
SELECT @90to100=COUNT(*)
FROM SC
WHERE Grade>=90 AND aaa@qq.com
UPDATE SCC
SET aaa@qq.com
WHERE Grades='>90'
UPDATE SCC
SET aaa@qq.com
WHERE Grades='>80'
UPDATE SCC
SET aaa@qq.com
WHERE Grades='>70'
UPDATE SCC
SET Num= @60to70
WHERE Grades='>60'
UPDATE SCC
SET aaa@qq.com
WHERE Grades='<60'
(2)
统计任意一门课的平均成绩
IF (exists (select * from sys.objects where name = 'Avg'))
DROP PROCEDURE Avg
GO
CREATE PROCEDURE Avg
AS
SELECT DECLARE
SELECT DISTINCT Cname,avg(Grade) AS Avg
FROM SC,Course
WHERE SC.Cno=Course.Cno AND aaa@qq.com
GROUP BY Cname;
(3)
将学生选课成绩从百分制改为等级制(即A、B、C、D、E)。
IF (exists (select * from sys.objects where name = 'Lv'))
DROP PROCEDURE Lv
GO
CREATE PROCEDURE Lv
AS
UPDATE SC SET Le='A' WHERE Grade>=100;
UPDATE SC SET Le='B' WHERE Grade<9 AND Grade>=80;
UPDATE SC SET Le='C' WHERE Grade<80 AND Grade>=70;
UPDATE SC SET Le='D' WHERE Grade<70 AND Grade>=60;
UPDATE SC SET Le='E' WHERE Grade<60;
SELECT *
FROM Course,SC
WHERE SC.Cno=Course.Cno;
EXEC Lv
总结
难......第二个真的难,自己真的没做出来,最后到处找资料,参考着来都没有自己写出来。还得琢磨啊。
完成时间:1h42min
以上
————(2020.4.5)