当前位置: 首页 > news >正文

触发器的简单应用

一、定义数据环境:teacher表记录教师情况,sal_log记录teacher表工资修改,添加情况。

create database T
go

use T
go

create table teacher
(
Eno numeric(4) primary key,
EName varchar(8) unique,
pJob varchar(8),
Sal numeric(7,2)
);
go

create table Sal_log
(
eno numeric(4) references teacher(eno),
Sal Numeric(7,2),
uSERNAME CHAR(10),
Date datetime);
go

二、定义触发器

1.insert_sal触发器,当向teacher表插入新元组时,向sal_log添加一条记录

if exists (select * from sysobjects
where name='insert_sal' and type='TR')
drop trigger insert_sal

go

--新触发器
Create trigger insert_sal
on teacher
for insert as
set Nocount off
declare @new_eno numeric(4),
@new_sal numeric(7,2)
--if @@rowcount=1
begin
select @new_eno=eno,@new_sal=sal from inserted
insert into sal_log
values(@new_eno,@new_sal,current_user,current_timestamp)
end

2.update_sal触发器,当teacher表修改工资时,向sal_log添加修改前和修改后,两条元组。

Create trigger update_sal
on teacher
for update as
declare @new_sal numeric(7,2),
@old_sal numeric(7,2),
@eno numeric(4)
if @@rowcount=1
begin
select @eno=eno,@new_sal=sal from inserted
select @old_sal=sal from deleted
if (@new_sal<>@old_sal)
insert into
sal_log values(@eno,@old_sal,
current_user,current_timestamp);
insert into
sal_log values(@eno,@new_sal,
current_user,current_timestamp);
end

3.定义触发器insert_sal_little,当向teacher添加记录时,检测若是教授,应该把工资最低为4000

if exists (select * from sysobjects
where name='insert_sal_little' and type='TR')
drop trigger insert_sal_little

go

--新触发器
Create trigger insert_sal_little
on teacher
for insert,update as
set Nocount off
declare @new_eno numeric(4),
@new_sal numeric(7,2)
--if @@rowcount=1
begin
select @new_eno=eno,@new_sal=sal from inserted
if (@new_sal-4000.00<0)
begin
update teacher set sal=4000.00
where eno=@new_eno
end
end

三、检测

--检测inser_salt触发器

insert into teacher values(2112,'Li','教授',6000)
go

select * from teacher
select * from sal_log

--检测update_sal触发器--

update teacher set sal=7300
where eno=2112


select * from teacher
select * from sal_log

--检测inser_sal_littlet触发器

insert into teacher values(2113,'wang','教授',3000)
go

select * from teacher
select * from sal_log

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • opencart mail.php,opencart联系表单邮件失败的解决办法
  • 创业者应避免的六件事
  • 113 php的位运算符不包括,【后端开辟】PHP的位运算符不包括谁人标记
  • SharpDevelop
  • matlab上机实习报告,Matlab上机实习报告
  • The Journey of the Lunch Launcher and Store and Forward Messaging
  • linux 清除php,linux如何清空某目录内文件
  • Channel 9: Offline Data Synchronization Services in Visual Studio 2008
  • php redis连接codis,redis客户端连接codis后报错
  • python字典输入键查对应的值,python字典通过值反查键的实现(简洁写法)
  • Google手机操作系统Android应用开发入门
  • oracle10g数据库热备份,Oracle数据库热备份文件过大收缩方法
  • java.sql.SQLException: ORA-22905: 无法从非嵌套表项访问行,问题及解决
  • PL/SQL表(oracle内存表)---table()函数用法
  • oracle时间处理,Oracle数据库中时间处理
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • 2017 前端面试准备 - 收藏集 - 掘金
  • Angular 响应式表单 基础例子
  • Apache的80端口被占用以及访问时报错403
  • canvas 绘制双线技巧
  • Idea+maven+scala构建包并在spark on yarn 运行
  • js
  • October CMS - 快速入门 9 Images And Galleries
  • opencv python Meanshift 和 Camshift
  • Sequelize 中文文档 v4 - Getting started - 入门
  • 基于 Ueditor 的现代化编辑器 Neditor 1.5.4 发布
  • ------- 计算机网络基础
  • 开发基于以太坊智能合约的DApp
  • 前端技术周刊 2019-02-11 Serverless
  • 前端之React实战:创建跨平台的项目架构
  • 如何编写一个可升级的智能合约
  • 想写好前端,先练好内功
  • 用Node EJS写一个爬虫脚本每天定时给心爱的她发一封暖心邮件
  • 原生JS动态加载JS、CSS文件及代码脚本
  • 阿里云API、SDK和CLI应用实践方案
  • ​数据结构之初始二叉树(3)
  • # Redis 入门到精通(一)数据类型(4)
  • # SpringBoot 如何让指定的Bean先加载
  • # windows 运行框输入mrt提示错误:Windows 找不到文件‘mrt‘。请确定文件名是否正确后,再试一次
  • #{} 和 ${}区别
  • #vue3 实现前端下载excel文件模板功能
  • #控制台大学课堂点名问题_课堂随机点名
  • $emit传递多个参数_PPC和MIPS指令集下二进制代码中函数参数个数的识别方法
  • (~_~)
  • (C++17) std算法之执行策略 execution
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (六)激光线扫描-三维重建
  • (十八)三元表达式和列表解析
  • (收藏)Git和Repo扫盲——如何取得Android源代码
  • .net 生成二级域名
  • .net(C#)中String.Format如何使用
  • .NET框架类在ASP.NET中的使用(2) ——QA
  • /boot 内存空间不够
  • /dev下添加设备节点的方法步骤(通过device_create)
  • @data注解_SpringBoot 使用WebSocket打造在线聊天室(基于注解)