T-Sql(六)触发器(trigger)
不知不觉讲到触发器了,一般我们做程序的很少接触到触发器,触发器的操作一般是DB人员来完成。
然而有的时候一些简单的业务需要我们自己去完成,不能每次都去麻烦DB人员,所以说,编程人员要全才,除了编程以为的专业知识也要涉猎,比如js,css,html,t-sql等一些语法,不一定要去精通,但是要熟悉,至少语法可以看懂,这样对我们的编程有事半功倍的效果,出现异常错误,我们也方便调试,以便最快找出错误。
言归正传,什么事触发器,顾名思义,就是你做一个操作,就会触发另一个事件,去执行一些操作。
比如你点烟花,点是一个动作,烟花是另一个动作,点动作完成就会触发烟花这个动作。
还有就是触发器必须依附一个主体,比如依附于某一张表,就像编程中事件这个概念。
下面我们通过一个简单的实例,和大家一步一步的来理解和运用触发器。
实例要求:
1,建商品表(Store),订单表(orders),日志表(Logs)
2,创建订单表插入触发器,实现插入一条订单信息,商品表中商品数量相应减少,订单中的总金额相应增加。
3,创建订单表更新触发器,实现更新一条订单信息,商品表中商品数量相应变化,订单中的总金额相应变化。(和2类似)
4,创建日志表触发器,实现更新商品表价格变化情况。
1,创建商品表(Store),订单表(orders),日志表(Logs)
第一步没什么讲解,我们创建表,并加一些示例数据。
示例代码:
create table Store
(
ID uniqueidentifier primary key,
ProductID int not null,
ProductPrice money not null default 1,
ProductCH nvarchar(80) not null,
ProductDate datetime not null,
NowNumber int not null
);
create table orders
(
OrderID int primary key,
ProductID int not null,
BuyNumber int Not null default 1,
BuyPricr money not null,
NowOrderPrice money default 0
)
create table Logs
(
ID uniqueidentifier primary key,
operatedatetime datetime,
ProductID int,
oldprice money,
newprice money
); insert into dbo.Store
values(NEWID(),1001,5000,'联想','2011-9-1',50)
insert into dbo.Store
values(NEWID(),1002,6000,'apple','2011-9-1',50)
insert into dbo.orders(OrderID,ProductID,BuyNumber,BuyPricr)
values(10013,1001,5,6000)
2,创建订单表触发器
要求2,3类似操作我在一起讲解了。
先看示例代码:
create trigger tri_order_NowOrderPrice
on orders after insert
as
begin
declare @NowOrderPrice money;
declare @BuyNumber int;
declare @ProductID int;
select @ProductID= ProductID,@BuyNumber= BuyNumber from inserted;
select @NowOrderPrice=inserted.BuyNumber * inserted.BuyPricr from inserted;
update orders set NowOrderPrice=@NowOrderPrice where ProductID=@ProductID;
update Store set NowNumber=NowNumber-@BuyNumber where ProductID=@ProductID;
end; create trigger tri_store_NowOrderPrice2
on orders after update
as
begin
declare @NowOrderPrice money;
declare @ProductID int;
declare @BuyNumber1 int;
declare @BuyNumber2 int;
select @ProductID= ProductID from inserted;
select @BuyNumber1=BuyNumber from inserted;
select @NowOrderPrice=inserted.BuyNumber * inserted.BuyPricr from inserted;
select @BuyNumber2=deleted.BuyNumber from deleted where ProductID=@ProductID;
update orders set NowOrderPrice=@NowOrderPrice where ProductID=@ProductID;
update Store set NowNumber=NowNumber-(@BuyNumber1-@BuyNumber2) where ProductID=@ProductID;
end;
触发器的关键字是trigger,语法是,on 表名 after 操作名称(一般为insert,update,delete),begin end中写一些触发器的处理操作。
inserted获取insert之后的数据。语法就是这么简单。
3,创建日志表触发器
日志表触发器要做的操作就是记录商品价格变化,那对应就应该在商品表中创建触发器。
示例代码:
create trigger tri_NowOrderPrice4
on dbo.Store after insert,update,delete
as
begin
if exists(select *from inserted) and exists(select *from deleted)
begin
print('update');
declare @price1 money;
declare @price2 money;
declare @datetime datetime;
select @price1=ProductPrice from deleted;
select @price2=ProductPrice from inserted;
if @price1!=@price2
begin
declare @ProductID int;
select @ProductID=ProductID from inserted
insert into Logs(ID,operatedatetime,ProductID,oldprice,newprice)
values(newid(),convert(datetime,getdate()),@ProductID,@price1,@price2) select *from dbo.Logs
end
end
else if exists(select *from inserted)
begin
print('insert');
end
else if exists(select *from deleted)
begin
print('delete');
end
else
begin
print('others');
end
end
触发器就是这些内容,有关触发器的一些复杂操作希望大家有时间研究下,讲的不好请大家多多指正,希望大家学好t-sql语言。
以后继续整理编程相关内容,希望大家多多关注。。。。
最新文章
- 【BZOJ-2653】middle 可持久化线段树 + 二分
- JS双月份显示联动效果,点击日期浮出消息提示
- 洛古 P1373 小a和uim之大逃离
- UVa 11082 (网络流建模) Matrix Decompressing
- POJ1947 - Rebuilding Roads(树形DP)
- telnet的使用
- bzoj 1088: [SCOI2005]扫雷Mine
- 如何在Windows上搭建Android开发环境
- linux权限设置(开放某个文件夹给指定用户)
- POJ 2411 解题报告
- BZOJ_3940_[Usaco2015 Feb]Censoring_AC自动机
- 【Python3爬虫】拉勾网爬虫
- vue.js学习第一天,了解vue.js
- LOJ2014 SCOI2016 萌萌哒 并查集、ST表优化连边
- java学习--";==”和 equals
- 话说extern和static
- MVC框架-.net-摘
- RF中采用python方法获取当月1号、上月1号、下月1号、当前日期N天后日期、当前日期N天前日期、指定月份总天数、上个月份、下个月份、当月最后1天日期、上个月最后1天日期、下个月最后1天日期
- 下载liteide
- jquery knob旋钮插件
热门文章
- python中__init__问题
- 基于webdriver的jmeter性能测试-Eclipse+Selenium+JUnit生成jar包
- web预设模块化
- mongoose 和 mongoDB
- 谢欣伦 - OpenDev原创教程 - 服务端套接字类CxServerSocket
- JDBC增删改查简单测试
- matlab 中 eps 的分析
- 【腾讯Bugly干货分享】微信mars 的高性能日志模块 xlog
- 【完全开源】百度地图Web service API C#.NET版,带地图显示控件、导航控件、POI查找控件
- [nRF51822] 11、基础实验代码解析大全 · 实验16 - 内部FLASH读写