zoukankan      html  css  js  c++  java
  • sql触发器

    /*1,建商品表(Store),订单表(orders),日志表(Logs)
    
        2,创建订单表插入触发器,实现插入一条订单信息,商品表中商品数量相应减少,订单中的总金额相应增加。
    
        3,创建订单表更新触发器,实现更新一条订单信息,商品表中商品数量相应变化,订单中的总金额相应变化。(和2类似)
    
        4,创建日志表触发器,实现更新商品表价格变化情况。
    */
    use samp
    go
    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 store values(newid(),1001,5000,'联想','2013-5-6',50)
    insert into store values(newid(),1002,6000,'apple','2013-5-6',50)
    insert into orders(orderid,productid,buynumber,buypricr)
    values(10013,1001,5,6000)
    --select * from store left join  orders on store.productid=orders.productid
    --创建触发器
    /*2,创建订单表插入触发器,实现插入一条订单信息,商品表中商品数量相应减少,订单中的总金额相应增加。
    
        3,创建订单表更新触发器,实现更新一条订单信息,商品表中商品数量相应变化,订单中的总金额相应变化。(和2类似)
    
        4,创建日志表触发器,实现更新商品表价格变化情况。
    */
    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;
  • 相关阅读:
    数据库三,exec内置函数
    HDU 1003:Max Sum(DP,连续子段和)
    HDU 1024:Max Sum Plus Plus(DP,最大m子段和)
    Codeforces 698A:Vacations(DP)
    牛客多校第五场 J:Plan
    51Nod 1091:线段的重叠(贪心)
    ZZNU 2125:A + B 普拉斯(傻逼题+大数加法)
    HDU 1010:Tempter of the Bone(DFS+奇偶剪枝+回溯)
    HDU 1176:免费馅饼(DP,自认为很详细的解释)
    POJ 2312:Battle City(BFS)
  • 原文地址:https://www.cnblogs.com/zhanying/p/3399223.html
Copyright © 2011-2022 走看看