触发器-- 肖敏_入门系列_数据库进阶 60、触发器(三) --youku
二
https://v.youku.com/v_show/id_XMzkxOTc5NDY0OA==.html?spm=a2h0k.11417342.soresults.dtitle
三
https://v.youku.com/v_show/id_XMzkyMTM1MDMxNg==.html?spm=a2h0k.11417342.soresults.dtitle
CREATE TRIGGER v_entity_trg_u
ON v_entity
FOR update
AS
declare @old_file_id id_TY, @new_file_id id_TY, @old_file_line int, @new_file_line int
select @old_file_id = file_id, @old_file_line = file_line from deleted
select @new_file_id = file_id , @new_file_line = file_line from inserted
IF @old_file_id ! = @new_file_id OR @old_file_line != @new_file_line
BEGIN
insert into v_entity_hist (ent_id, action, file_id, file_line, last_chg_dt)
select ent_id, action, file_id, file_line, last_src_dt from deleted
END
1.
银行信息表account,交易信息表transinfo,现增加一笔交易记录
则account表账户余额应该变化
2.
这两个过程是一个整体,要么都执行,要么都不执行
begin transaction
向交易信息表插入数据
在账户表中修改数据
Commit transaction
如果失败
Rollback transaction
触发器:只要用户向交易信息表中插入了数据,系统会自动在账户表中修改数据
自动执行,整个过程是一个事务
Create TRIGGER trigger_name
ON table_name [with encryption]
FOR {[insert/update/delete]}
AS
SELECT sentence
GO
------------------------------------eg--------------
id从一万开始
create table Account
(
id int identity(10000, 1) primary key,
name nvarchar(16) not null,
balance int not null,---money left
)
create table TransInfo
(
id int identity(1, 1) primary key,
accountId int not null,
[type] int not null, -- 1 deposit 0 withdraw
amount int not null,
createDatetime datetime not null
)
alter table TransInfo -- 建立约束
add constraint FK_ACCOUNTID foreign key (accountId)
references Account(id);
insert into dbo.Account
select '郭靖' 5000 union
select '黄蓉' 10000
--针对新增交易记录,修改账户表的余额
create trigger tg_transinfo_insert
on TransInfo
for insert
as
--需要从新插入的数据中,取出交易数据
--账户编号,交易金额,交易类型
--系统有2临时表:inserted 存放最新加入的数据
--deleted 存放被删除的或被修改的原始数据
declare @accId int, @amount int, @type int
select @accId=accountId, @type=type, @amount=amount
from inserted --拿到新加入的数据
if(@type = 0)
begin
set @amount = @amount * -1;
end
update Account
set balance = balance + @amount
where id = @accId
go
触发器不用手动调用
当insert会自动调用
insert into TransInfo
select 10000, 0, 100, GETDATE()
declare @old_file_id id_TY, @new_file_id id_TY, @old_file_line int, @new_file_line int
select @old_file_id = file_id, @old_file_line = file_line from deleted
select @new_file_id = file_id , @new_file_line = file_line from inserted
IF @old_file_id ! = @new_file_id OR @old_file_line != @new_file_line
BEGIN
insert into v_entity_hist (ent_id, action, file_id, file_line, last_chg_dt)
select ent_id, action, file_id, file_line, last_src_dt from deleted
END
最新文章
- Java进击C#——前言
- netty4虚拟内存不断飙升
- 揭秘JavaScript中谜一样的this
- 从github上获取资源速度慢的解决办法
- 初试微信小程序
- Windows 8.1 应用再出发 (WinJS) - 几种新增控件(2)
- JQuery思维导图
- json 增删改 加 排序
- Windows下快捷键
- CentOS下的网络配置文件说明
- PDO LIMIT bug
- html标签元素分类
- jsp 中对jar 包的引用
- Java Jvm运行机制原理
- [APM] 2个实例+5个维度解读APM技术
- centos6 安装 docker 问题
- Curling 2.0(DFS简单题)
- 20155204《网络对抗》Exp9 Web安全基础实践
- python 简明教程
- 14. Android框架和工具之 ImageLoader(图片加载)
热门文章
- mysql 免安装版后续操作
- FreeCAD stp文件基于python脚本操作
- Python 列表/元组/字典总结
- 【CS224n-2019学习笔记】Lecture 1: Introduction and Word Vectors
- Spark RDD 算子总结
- opencv python运动人体检测
- vue 父子传值,子页面没有实时刷新的问题
- UVA - 11214 Guarding the Chessboard(守卫棋盘)(迭代加深搜索)
- 【新年呈献】高性能网络通信框架 HP-Socket v5.7.1
- 十、CI框架之通过参数的办法输出URI路径