这个触发器要怎么写?

lzy1744 2017-07-28 10:45:00
我现在有一张表a,里面有两个字段,分别是实际小时realtime,申请小时applytime,现在加了一个字段,小时差异timecy=realtime-applytime,但是timecy不能直接插入,所以想写一个触发器,当有数据插入时,自动插入小时差异字段,并且当实际小时和申请小时发生变化时,小时差异字段同步改变。以上字段都是number型的。
请教下各位大佬,这个触发器怎么写比较好,实际使用中会不会有其他什么问题?谢谢!
...全文
143 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
卖水果的net 2017-07-28
  • 打赏
  • 举报
回复

SQL> create table test(id int, realtime int, applytime int, timecy int);
Table created
SQL> create trigger tri_test_iu
  2  before insert or update on test
  3  for each row
  4  begin
  5      :new.timecy := :new.realtime - :new.applytime;
  6  end;
  7  /
Trigger created
SQL> begin
  2  insert into test(id, realtime, applytime) values(1, 10, 8);
  3  insert into test(id, realtime, applytime) values(2, 5, 0);
  4  end;
  5  /
PL/SQL procedure successfully completed
SQL> select * from test;
        ID   REALTIME  APPLYTIME     TIMECY
---------- ---------- ---------- ----------
         1         10          8          2
         2          5          0          5
SQL> update test set applytime = 3 where id = 1;
1 row updated
SQL> select * from test;
        ID   REALTIME  APPLYTIME     TIMECY
---------- ---------- ---------- ----------
         1         10          3          7
         2          5          0          5
SQL> drop table test purge;
Table dropped

SQL> 

17,082

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧