CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Oracle >  开发

求:数据恢复的语句!

楼主bigsir(bigsir)2006-07-03 15:52:41 在 Oracle / 开发 提问

t_leaf:  
  id           name           time           no    
  1             a               ......         2  
  2             b             .......         100  
  2             b             .......         100  
  2             b             .......         100  
   
  t_root:  
  id           name           time           Total_no    
  2             b             .......         1000  
   
  期望,我在t_leaf中增加一条后,对应t_root中的Total_no会自动减少t_leaf中的no的数字,  
  同理在t_leaf中删除一条厚,对应t_root中的Total_no会自动增加t_leaf中的no的数字  
   
  举例:t_leaf中心赠加  
  id           name           time           no    
  1             b               ......         2  
   
  那么这时候t_root中应该是  
  id           name           time           Total_no    
  2             b             .......         998  
   
  举例:t_leaf中心删除一条记录  
  id           name           time           no    
  1             b               ......         200  
   
  那么这时候t_root中应该是  
  id           name           time           Total_no    
  2             b             .......         1198  
   
  我的想法是通过trigger来实现,不过sql语句不太熟练,请大家指点一下。  
  问题点数:100、回复次数:3Top

1 楼issgates(一直很安静)回复于 2006-07-03 17:16:19 得分 0

看看oracle触发器与存储过程便知,很简单的。Top

2 楼xiaoxiao1984(笨猫儿)回复于 2006-07-03 17:29:38 得分 100

--------插入,更新触发器  
  create   or   replace   trigger   trigger_leaf_insert_update  
      before   insert   or   update   on   t_leaf      
      for   each   row  
  declare    
      cnt   number;      
  begin  
      select   count(1)   into   cnt   from   t_root   where   id   =   :new.id   and   name   =   :new.name;  
      if   cnt   =   0   then    
          insert   into   t_root(id,   name,   time,   Total_no)    
              values(:new.id,   :new.name,   :new.time,   :new.no);  
      else    
          update   t_root   set   Total_no   =   Total_no   -   nvl(:new.no,0)   +   nvl(:old.no,0)  
              where   id   =   :new.id   and   name   =   :new.name;  
      end   if   ;  
  end   trigger_leaf_insert_update;  
   
  --删除触发器  
  create   or   replace   trigger   trigger_leaf_delete  
      before   delete   on   t_leaf      
      for   each   row  
       
  begin  
          update   t_root   set   Total_no   =   Total_no   +   :old.no  
              where   id   =   :old.id   and   name   =   :old.name;  
   
  end   trigger_leaf_delete;  
  Top

3 楼bigsir(bigsir)回复于 2006-07-04 10:05:14 得分 0

to   xiaoxiao1984(笨猫一只^_^)   :  
   
  非常感谢,我去试试,如果不行再给你发消息。Top

相关问题

关键词

得分解答快速导航

  • 帖主:bigsir
  • xiaoxiao1984

相关链接

  • Oracle类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo