1.掌握数据库完整性控制原理
2.掌握SQL Server数据库系统完整性控制方法
1. 创建一个触发器,“数据库”课程的选课人数不能超过5人。
2..创建一个触发器,规定每位同学选课门数不能超过3门。
3. 创建一个触发器,当修改成绩表中成绩时,如果变化幅度超过10%,将操作记录在下表SC_U(Sno,Cno,Oldgrade,Newgrade)中。
1.写出你操作并正确执行的代码及结果。
创建一个触发器,“数据库”课程的选课人数不能超过5人。
--创建触发器
CREATE TRIGGER myFristNotByCount ON T.SC
AFTER INSERT,UPDATE
AS
DECLARE @Count INT,@Cno CHAR(4),@Sno CHAR(9)
SELECT @Count = COUNT(*) FROM T.Course a,T.SC b
WHERE a.Cno = b.Cno AND a.Cname = '数据库';
SELECT @Sno = Sno,@Cno = Cno FROM INSERTED;
IF(@Count>5)
BEGIN
DELETE FROM T.SC WHERE Sno = @Sno AND Cno = @Cno;
END
--测试
INSERT INTO T.SC VALUES('201215121',1,52),
('201215122',1,52),('201215123',1,52),
('201215125',1,52),('201215126',1,52),('201215127',1,52);
--查询数据库的选课记录
SELECT COUNT(*) 数量 FROM T.Course a,T.SC b
WHERE a.Cno = b.Cno AND a.Cname = '数据库';
创建一个触发器,规定每位同学选课门数不能超过3门。
--创建触发器
CREATE TRIGGER MyTriggerByCourse ON T.SC
AFTER INSERT
AS
DECLARE @Count INT,@Sno CHAR(9),@Cno CHAR(4)
SELECT @Sno = Sno,@Cno = Cno FROM INSERTED;
SELECT @Count = COUNT(*) FROM T.SC;
IF(@Count>3)
BEGIN
DELETE FROM T.SC WHERE Sno = @Sno AND Cno = @Cno;
END
--测试(两个人分别添加6条选课记录)
INSERT INTO T.SC VALUES('201215121',1,52),
('201215122',1,52),('201215123',1,52),
('201215125',1,52),('201215126',1,52),('201215127',1,52);
INSERT INTO T.SC VALUES('201215121',2,52),
('201215122',2,52),('201215123',2,52),
('201215125',2,52),('201215126',2,52),('201215127',2,52);
--验证
SELECT a.Cno 课程名,a.Cname,COUNT(a.Cno) 数量 FROM T.Course a,T.SC b
WHERE a.Cno = b.Cno
GROUP BY a.Cno,a.Cname;
创建一个触发器,当修改成绩表中成绩时,如果变化幅度超过10%,将操作记录在下表SC_U(Sno,Cno,Oldgrade,Newgrade)中。
--创建表T.SC_U
CREATE TABLE T.SC_U(
id INT IDENTITY(1,1),
Sno CHAR(9),
Cno CHAR(4),
Oldgrade SMALLINT,
Newgrade SMALLINT,
UpdateTime datetime,--更新时间
PRIMARY KEY(id),
FOREIGN KEY (Sno) REFERENCES T.Student(Sno),
FOREIGN KEY (Cno) REFERENCES T.Course(Cno)
)
--创建触发器
CREATE TRIGGER MyTriggerGradeUpdate ON T.SC
AFTER UPDATE
AS
DECLARE @Grade_old SMALLINT,@jexianZ SMALLINT,@jexianJ SMALLINT,@Grade_new SMALLINT,@Sno CHAR(9),@Cno CHAR(4)
SELECT @Grade_old = Grade,@Sno = Sno,@Cno = cno FROM DELETED;
SELECT @Grade_new = Grade,@Sno = Sno,@Cno = Cno FROM INSERTED;
SET @jexianZ = @Grade_old+@Grade_old*0.1;
SET @jexianJ = @Grade_old-@Grade_old*0.1;
IF(@Grade_new > @jexianZ)
BEGIN
PRINT('增幅的界限为:');
PRINT(@jexianZ);
INSERT INTO T.SC_U VALUES(@Sno,@Cno,@Grade_old,@Grade_new,GETDATE())
END
IF(@Grade_new < @jexianJ)
BEGIN
PRINT('向下增幅的界限为:');
PRINT(@jexianJ);
INSERT INTO T.SC_U VALUES(@Sno,@Cno,@Grade_old,@Grade_new,GETDATE())
END
结果截图:
——————————————————————————————————
测试触发器
--测试
--向上幅度大于10%
select * FROM T.SC WHERE Sno ='201215127' AND Cno = '1';--原始值
UPDATE T.SC SET Grade = '90' WHERE Sno ='201215127' AND Cno = '1';
select * FROM T.SC WHERE Sno ='201215127' AND Cno = '1';--更新后的值
SELECT * FROM T.SC_U WHERE Sno ='201215127' AND Cno = '1';--查表检查结果是否被记录是否被记录
——————————————————————————————————
--向上幅度小于10%
select * FROM T.SC WHERE Sno ='201215127' AND Cno = '1';
UPDATE T.SC SET Grade = '73' WHERE Sno ='201215127' AND Cno = '1';
select * FROM T.SC WHERE Sno ='201215127' AND Cno = '1';
SELECT * FROM T.SC_U WHERE Sno ='201215127' AND Cno = '1';
——————————————————————————————————
--向下幅度大于10%
select * FROM T.SC WHERE Sno ='201215126' AND Cno = '1';
UPDATE T.SC SET Grade = '30' WHERE Sno ='201215126' AND Cno = '1';
select * FROM T.SC WHERE Sno ='201215126' AND Cno = '1';
SELECT * FROM T.SC_U WHERE Sno ='201215126' AND Cno = '1';
——————————————————————————————————
--向下幅度小于10%
select * FROM T.SC WHERE Sno ='201215126' AND Cno = '1';
UPDATE T.SC SET Grade = '24' WHERE Sno ='201215126' AND Cno = '1';
select * FROM T.SC WHERE Sno ='201215126' AND Cno = '1';
SELECT * FROM T.SC_U WHERE Sno ='201215126' AND Cno = '1';
点击此处下载:SQL文件(提供数据库含表接结构和触发器源码)
提取码:ghzn