您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码:  验证码,看不清楚?请点击刷新验证码 必填



  求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Modeler   Code  
会员   
 
   
 
 
     
   
 订阅
  捐助
数据库触发器控制
 
作者 yibcs,火龙果软件    发布于 2014-07-04
   次浏览      
 

 --①选修成绩表SCTS中,学生选修成绩由平时成绩(regular_grade)和考试成绩(exam_grade)构成课程总评成绩(total_mark),公式为:total_mark= regular_grade*30%+ exam_grade*70%

  设计DML触发器,使得当用户修改某位学生选修某门课程的平时成绩或者考试成绩时,自动实现对该学生该门课程总评成绩的更新。

Create trigger Tri_UPDATE_SCTS
on SCTS
after UPDATE
AS
BEGIN
IF UPDATE(regular_grade)or UPDATE(exam_grade)
BEGIN
DECLARE @rgrade float;
DECLARE @egrade float;
select @rgrade=regular_grade,@egrade= exam_grade from inserted
update SCTS
set total_mark=0.3*@rgrade+0.7*@egrade
from inserted
where SCTS.studentid =inserted.studentid
and SCTS.courseid=inserted.courseid
and SCTS.teacherid =inserted.teacherid
END
END

--测试触发器

update  Scts
set regular_grade='100',exam_grade='100'
where studentid='200520805403'and courseid='20224B0' and teacherid='080102'
select *from scts
where studentid='200520805403'and courseid='20224B0' and teacherid='080102'

--2、②设计DML触发器,使得当某学生在一个学期中所选修的课程总学分超过20学分时,自动提示“你选修的总学分已达到最大值,不允许继续选课!”的信息;

Create trigger Tri_IN_LIMIT_SCTS
on SCTS
after insert --注意这里AFTER是插入之后的
AS
BEGIN
BEGIN
DECLARE @allcredit float;
DECLARE @sid varchar(12);
select @sid =inserted.studentid from inserted
select @allcredit=sum(credit)
from courses
where courseid in(
select courseid
from scts
where studentid=@sid
)
if (@allcredit>20)
begin
Rollback Transaction
print @allcredit
print'你选修的总学分已达到最大值,不允许继续选课!'
end
else
print'选课成功'
END
END

--测试触发器

select  sum(credit) '总分'
from courses
where courseid in(
select courseid
from scts
where studentid='200520701201'
)
insert into Scts(courseid,studentid,teacherid)
values ('50103Q0','200520701201','080102');
select *from courses where courseid='10042B0'

--3.设计DML触发器限定:对于“专业”(COURSES.character,包括专业课、专业基础、专业选修等)课程,只有该课程开课学院的学生才能选修,否则提示“不允许跨院选课!”的提示信息。

Create trigger Tri_INSERT_SCTS
on SCTS
after insert
AS
BEGIN
DECLARE @stype varchar(10);
DECLARE @collegeid varchar(5);
if exists(select * from courses AS C
where C.courseid in(select courseid from inserted ) and C.Character like '专业%')
BEGIN
select @collegeid=college from courses AS C
where C.courseid in(select courseid from inserted )
if exists (select college from students AS S
where S.studentid in(select studentid from inserted )and S.college=@collegeid)
PRINT '选专业课成功'
else
BEGIN
Rollback Transaction
PRINT '不允许跨院选课!'
END
END
ELSE
PRINT '选修公共选修课成功'
END

--选修本学院专业课

insert into Scts(courseid,studentid,teacherid)

values ('20224B0','200520805403','080102');

--选修非本院专业课 终止

insert into Scts(courseid,studentid,teacherid)

values ('10019B5','200520805403','080102');

--选修公共选修课

insert into Scts(courseid,studentid,teacherid)
values ('50095Q0','200520805403','080102');
select *from students where college='08'
select *from colleges where collegeid='08'
select *from courses where courseid in (select courseid from courses where character not like '专业%')and college
='08'
select *from scts where studentid='200520805403'
delete from scts where courseid='20224B0' and studentid='200520805403';

 --4、设计DML触发器以实现对敏感数据的自动审计:当用户在SCTS表中插入新记录或者更新SCTS表中的regular_grade和exam_grade属性列时,自动在成绩变化表GRADE_LOG(student, course, teacher, regular_grade, exam_grade, username, userdate)中增加一条相应记录,以记录当前用户对学生成绩的操作。(system_user)

Create table GRADE_LOG(
id int identity(1,1) primary key,
student varchar(20) not null ,
course varchar(50) not null,
teacher varchar(20) not null,
regular_grade float ,
exam_grade float,
username varchar(20) not null,
userdate datetime not null,
operator varchar(10) not null
)
--select system_user getdate()
Create trigger Tri_IN_U_SCTS
on SCTS
after INSERT,UPDATE
AS
BEGIN
IF UPDATE(regular_grade)OR UPDATE(exam_grade)or (exists (select 1 from inserted) and not exists (select 1
from deleted))
BEGIN
DECLARE @student varchar(20);
DECLARE @course varchar(50);
DECLARE @teacher varchar(20);
DECLARE @rgrade float;
DECLARE @egrade float;
DECLARE @username varchar(20);
DECLARE @date datetime;
DECLARE @type varchar(10);
select @type='update';
if exists (select 1 from inserted) and not exists (select 1 from deleted)
select @type='insert';
select @student=sname from students where studentid in(select studentid from inserted )
select @course =cname from courses where courseid in (select courseid from inserted)
select @teacher =tname from teachers where teacherid in (select teacherid from inserted)
select @rgrade=regular_grade,@egrade=exam_grade from inserted
select @username=system_user,@date=getdate();
insert into GRADE_LOG
values(@student,@course,@teacher,@rgrade,@egrade,@username,@date,@type)
END
END
select * from GRADE_LOG;

--更新成绩

update Scts

set regular_grade='100',exam_grade='20'

where studentid='200520805403'and courseid='20224B0' and teacherid='080102'

--只更新总成绩,不激活触发器

update Scts

set total_mark='100'

where studentid='200520805403'and courseid='20224B0' and teacherid='080102'

--5、DDL触发器,禁止用户在Teaching数据库中的修改表和删除表操作。

CREATE TRIGGER TRI_Teaching_DDL
ON database
for alter_table,drop_table
AS
BEGIN
print '不允许修改或删除数据表!'
Rollback Transaction
END
   
次浏览       
相关文章

基于EA的数据库建模
数据流建模(EA指南)
“数据湖”:概念、特征、架构与案例
在线商城数据库系统设计 思路+效果
 
相关文档

Greenplum数据库基础培训
MySQL5.1性能优化方案
某电商数据中台架构实践
MySQL高扩展架构设计
相关课程

数据治理、数据架构及数据标准
MongoDB实战课程
并发、大容量、高性能数据库设计与优化
PostgreSQL数据库实战培训
最新活动计划
LLM大模型应用与项目构建 12-26[特惠]
QT应用开发 11-21[线上]
C++高级编程 11-27[北京]
业务建模&领域驱动设计 11-15[北京]
用户研究与用户建模 11-21[北京]
SysML和EA进行系统设计建模 11-28[北京]

MySQL索引背后的数据结构
MySQL性能调优与架构设计
SQL Server数据库备份与恢复
让数据库飞起来 10大DB2优化
oracle的临时表空间写满磁盘
数据库的跨平台设计
更多...   


并发、大容量、高性能数据库
高级数据库架构设计师
Hadoop原理与实践
Oracle 数据仓库
数据仓库和数据挖掘
Oracle数据库开发与管理


GE 区块链技术与实现培训
航天科工某子公司 Nodejs高级应用开发
中盛益华 卓越管理者必须具备的五项能力
某信息技术公司 Python培训
某博彩IT系统厂商 易用性测试与评估
中国邮储银行 测试成熟度模型集成(TMMI)
中物院 产品经理与产品管理
更多...