1.假设有下面两个关系模式:
职工(职工号,姓名,年龄,职务,工资,部门号), 其中职工号为主码;
部门(部门号,名称,经理名,电话),其中部门号为主码。
用 SQL 语言定义这两个关系模式,要求在模式中完成以下完整性约束条件的定义:
(1)定义每个模式的主码;(2)定义参照完整性;(3)定义职工年龄不得超过60岁
先列出关系模式:
Staff(Sno,Sname,Sage,Post,Pay,Dno)
Dept(Dno,Dname,Mname,Tel)
建表之前,我们需要注意的是建表的顺序,因为Staff表中的外码来自Dept,所以需要先建表Dept
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),--约束不超过60
Post CHAR(20),
Pay CHAR(20),
Dno CHAR(9),
FOREIGN KEY (Dno) REFERENCES Dept(Dno)--参照完整性
);
2.对学生-课程数据库编写存储过程, 完成下述功能:
先看一下现SC表
(1)统计离散数学的成绩分布情况,即按照各分数段统计人数。
建表:
CREATE TABLE SCC( Grades CHAR(10),
Num SMALLINT--存人数
)
初始化:
INSERT INTO SCC(Grades,Num)
VALUES('[0~20)',0);
INSERT INTO SCC(Grades,Num)
VALUES('[20~40)',0);
INSERT INTO SCC(Grades,Num)
VALUES('[40~60)',0);
INSERT INTO SCC(Grades,Num)
VALUES('[60~80)',0);
INSERT INTO SCC(Grades,Num)
VALUES('[80~100]',0);
定义存储过程Stats:
IF (exists (select * from sys.objects where name = 'Stats'))
DROP PROCEDURE Stats
GO
CREATE PROCEDURE 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 SCC SET Num=@0to20 WHERE Grades='[0,20)';
UPDATE SCC SET Num=@20to40 WHERE Grades='[20,40)';
UPDATE SCC SET Num=@40to60 WHERE Grades='[40,60)';
UPDATE SCC SET Num=@60to80 WHERE Grades='[60,80)';
UPDATE SCC SET Num=@80to100 WHERE Grades='[80,100]';
EXEC Stats
SELECT* FROM SCC
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
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
这里面个人感觉吃不消的就是 存储过程这一块,还是不太熟。其余的基本都懂。
在触发器Trigger这里老师我有一个问题:就是SQL是自动为变量去添加记录或赋值吗?
在这里:触发事件是
INSERT ON Student
触发动作是:
INSERT INTO StudentInsertLog(Numbers)
SELECT COUT(*) FROM DELTA
新建表NEW TABLE起名为DELTA,后面SELECT DELTA里面的个数,插入到Numbers中,这个语句块也没有哪一条语句说把新增的记录插入到DELTA中呀?
SELECT COUNT(*)FROM SELTA
那这条语句的结果不就是0了吗?
我看了一下CREATE TRIGGER的定义和格式,还有视频的回放,还是不太明白。