1.假设有下面两个关系模式:
职工(职工号,姓名,年龄,职务,工资,部门号), 其中职工号为主码;
部门(部门号,名称,经理名,电话),其中部门号为主码。
用 SQL 语言定义这两个关系模式,要求在模式中完成以下完整性约束条件的定义:
(1)定义每个模式的主码;(2)定义参照完整性;(3)定义职工年龄不得超过60岁
Staff(Sno,Sname,Sage,Post,Pay,Dno)
Dept(Dno,Dname,Mname,Tel)
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)
);
经测试没问题,注意一定先建表Dept,因为Staff中用到了Dept的Dno
2.对学生-课程数据库编写存储过程, 完成下述功能:
先添加必要的测试数据
INSERT INTO Course(Cno,Cname,Cpno,Ccredit) VALUES ('8','离散数学',3,4);
INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215121','8',12);
INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215122','8',32);
INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215123','8',52);
INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215125','8',62);
INSERT INTO SC(Sno,Cno,Grade) VALUES ('201215128','8',92);
/*为SC表增加列Level*/
ALTER TABLE SC ADD Level CHAR(2);
(1)统计离散数学的成绩分布情况,即按照各分数段统计人数。
/*建立统计离散数学的成绩表*/
CREATE TABLE SC8
(Score CHAR(10), /*成绩分段*/
Num SMALLINT /*学生人数*/
);
INSERT INTO SC8(Score,Num) VALUES('[0,20)',0);
INSERT INTO SC8(Score,Num) VALUES('[20,40)',0);
INSERT INTO SC8(Score,Num) VALUES('[40,60)',0);
INSERT INTO SC8(Score,Num) VALUES('[60,80)',0);
INSERT INTO SC8(Score,Num) VALUES('[80,100]',0);
SELECT * FROM SC8
IF (exists (select * from sys.objects where name = 'Stats'))
DROP PROCEDURE Stats
GO
CREATE PROCEDURE Stats
/*定义存储过程Stats*/
AS
DECLARE /*定义变量*/
@0to20 INT,
@20to40 INT,
@40to60 INT,
@60to80 INT,
@80to100 INT,
@CNO CHAR(8);
SELECT @CNO=Cno
FROM Course
WHERE Cname='离散数学';
SELECT @0to20=COUNT(*)
FROM SC
WHERE Grade<20 AND Cno=@CNO
SELECT @20to40=COUNT(*)
FROM SC
WHERE Grade<40 AND Grade>=20 AND Cno=@CNO
SELECT @40to60=COUNT(*)
FROM SC
WHERE Grade<60 AND Grade>=40 AND Cno=@CNO
SELECT @60to80=COUNT(*)
FROM SC
WHERE Grade<80 AND Grade>=60 AND Cno=@CNO
SELECT @80to100=COUNT(*)
FROM SC
WHERE Grade>=80 AND Cno=@CNO
UPDATE SC8 SET Num=@0to20 WHERE Score='[0,20)';
UPDATE SC8 SET Num=@20to40 WHERE Score='[20,40)';
UPDATE SC8 SET Num=@40to60 WHERE Score='[40,60)';
UPDATE SC8 SET Num=@60to80 WHERE Score='[60,80)';
UPDATE SC8 SET Num=@80to100 WHERE Score='[80,100]';
EXEC Stats
SELECT* FROM SC8
CREATE TABLE AvgC
(Cno CHAR(4),
Cname CHAR(10),
Score FLOAT
);
INSERT INTO AvgC(Cno,Cname,Score) VALUES('1','数据库',0);
INSERT INTO AvgC(Cno,Cname,Score) VALUES('2','数学',0);
INSERT INTO AvgC(Cno,Cname,Score) VALUES('3','信息系统',0);
INSERT INTO AvgC(Cno,Cname,Score) VALUES('8','高等数学',0);
SELECT * FROM AvgC
IF (exists (select * from sys.objects where name = 'Avgcore'))
DROP PROCEDURE Avgcore
GO
CREATE PROCEDURE 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 AvgC SET Score=@S1 WHERE Cno='1';
UPDATE AvgC SET Score=@S2 WHERE Cno='2';
UPDATE AvgC SET Score=@S3 WHERE Cno='3';
UPDATE AvgC SET Score=@S8 WHERE Cno='8';
EXEC Avgcore
SELECT* FROM AvgC
(3)将学生选课成绩从百分制改为等级制 (即A、 B、C、D、E)
IF (exists (select * from sys.objects where name = 'get_Level'))
DROP PROCEDURE get_Level
GO
CREATE PROCEDURE get_Level
AS
UPDATE SC SET Level='A' WHERE Grade>=80;
UPDATE SC SET Level='B' WHERE Grade<40 AND Grade>=20;
UPDATE SC SET Level='C' WHERE Grade<60 AND Grade>=40;
UPDATE SC SET Level='D' WHERE Grade<80 AND Grade>=60;
UPDATE SC SET Level='E' WHERE Grade<20;
EXEC get_Level
SELECT* FROM SC
总结:好家伙第一题写了两分钟,第二题写了俩小时,没有你做不到的错误,但是写到第三个好像也就那么回事,多做还是可以促进理解的。