数据库触发器控制
作者:网络转载 发布时间:[ 2014/5/4 10:25:45 ] 推荐标签:数据库触发器
--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
|
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
在测试数据库性能时,需要注意哪些方面的内容?测试管理工具TC数据库报错的原因有哪些?怎么解决?数据库的三大范式以及五大约束编程常用的几种时间戳转换(java .net 数据库)优化mysql数据库的几个步骤数据库并行读取和写入之Python实现深入理解数据库(DB2)缓冲池(BufferPool)国内三大云数据库测试对比预警即预防:6大常见数据库安全漏洞数据库规划、设计与管理数据库-事务的概念SQL Server修改数据库物理文件存在位置使用PHP与SQL搭建可搜索的加密数据库用Python写一个NoSQL数据库详述 SQL 中的数据库操作详述 SQL 中的数据库操作Java面试准备:数据库MySQL性能优化

sales@spasvo.com