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

最新文章

  1. Java进击C#——前言
  2. netty4虚拟内存不断飙升
  3. 揭秘JavaScript中谜一样的this
  4. 从github上获取资源速度慢的解决办法
  5. 初试微信小程序
  6. Windows 8.1 应用再出发 (WinJS) - 几种新增控件(2)
  7. JQuery思维导图
  8. json 增删改 加 排序
  9. Windows下快捷键
  10. CentOS下的网络配置文件说明
  11. PDO LIMIT bug
  12. html标签元素分类
  13. jsp 中对jar 包的引用
  14. Java Jvm运行机制原理
  15. [APM] 2个实例+5个维度解读APM技术
  16. centos6 安装 docker 问题
  17. Curling 2.0(DFS简单题)
  18. 20155204《网络对抗》Exp9 Web安全基础实践
  19. python 简明教程
  20. 14. Android框架和工具之 ImageLoader(图片加载)

热门文章

  1. mysql 免安装版后续操作
  2. FreeCAD stp文件基于python脚本操作
  3. Python 列表/元组/字典总结
  4. 【CS224n-2019学习笔记】Lecture 1: Introduction and Word Vectors
  5. Spark RDD 算子总结
  6. opencv python运动人体检测
  7. vue 父子传值,子页面没有实时刷新的问题
  8. UVA - 11214 Guarding the Chessboard(守卫棋盘)(迭代加深搜索)
  9. 【新年呈献】高性能网络通信框架 HP-Socket v5.7.1
  10. 十、CI框架之通过参数的办法输出URI路径