Oracle update trigger
Go straight one without more words
DROP TRIGGER kfzt_afterupdate_dlzt;
create or replace trigger kfzt_afterupdate_dlzt AFTER UPDATE ON jg_kfzt FOR EACH ROW
BEGIN
-- The update timeout is equal to the heartbeat time plus30Minute;Online time equals heartbeat time minus minutes of login time;
UPDATE jg_dlzt
SET cssj = :new.xtsj + INTERVAL '30' minute,
zxsc = CEIL((TO_DATE(to_char(:new.xtsj,'yyyy-mm-dd hh24:mi:ss'), 'YYYY-MM-DD HH24-MI-SS') - TO_DATE(to_char(dlsj,'yyyy-mm-dd hh24:mi:ss') , 'YYYY-MM-DD HH24-MI-SS')) * 24*60 ),
xtsj = :new.xtsj
WHERE kfid = :new.kfid AND dlsj = (SELECT a.dlsj FROM (SELECT MAX(dlsj) AS dlsj FROM jg_dlzt where kfid = :new.kfid) a );
END;
This is a real business trigger for me. The implementation operation is to modify the time of the cssj field of JG dlzt table after the update of JG kfzt table. In Oracle, the operation of time is rather tedious, and it can not achieve the desired result by directly using time subtraction. So here time is first converted into character and then converted into time
//where conditions to pay attention to
WHERE kfid = :new.kfid AND dlsj = (SELECT a.dlsj FROM (SELECT MAX(dlsj) AS dlsj FROM jg_dlzt where kfid = :new.kfid) a );
//Here, dlsj = the following sub condition query will report an error if the alias is not used. The reason is that it's against the Convention to modify itself with its own data as a condition. That's why an alias avoids this problem
By the way, briefly
//CEIL rounded up. 1.1 = 2 1.9 = 2 -1.1 = -1 -1.9 = -1 //FLOOR rounds down. 1.1 = 1 1.9 = 1 -1.1 = -2 -1.9 = -2 //ROUND. 1.1 = 1 1.9 = 2 -1.1 = -1 -1.9 = -2 //TRUNC truncation integer. 1.1 = 1 1.9 = 1 - 1.1 = - 1 - 1.9 = - 1
mysql time operation
Get current time function: now() DATE_ADD("2011-11-20 12:22:30",INTERVAL 30 MINUTE) / / parameter time increases by 30 minutes
oracle time operation
Get the current time function: sysdate
//First, convert the time type to char format
//Then convert it to date. Time operation
CEIL((TO_DATE(to_char('Time field in database','yyyy-mm-dd hh24:mi:ss'), 'YYYY-MM-DD HH24-MI-SS') - TO_DATE(to_char('Time field in database','yyyy-mm-dd hh24:mi:ss') , 'YYYY-MM-DD HH24-MI-SS')) * 24*60 )
Here is the MySQL trigger with the same requirements
DROP TRIGGER IF EXISTS kfzt_afterupdate_dlzt;
CREATE TRIGGER kfzt_afterupdate_dlzt AFTER UPDATE ON jg_kfzt FOR EACH ROW
BEGIN
-- The update timeout is equal to the heartbeat time plus30Minute;Online time equals heartbeat time minus minutes of login time;
UPDATE jg_dlzt a SET cssj = DATE_ADD(new.xtsj,INTERVAL 30 MINUTE),zxsc = TIMESTAMPDIFF(Minute,a.dlsj,new.xtsj) WHERE a.kfid = new.kfid AND NOW() < a.cssj;
END
It should be noted that the syntax in mysql and oracle is slightly different, which requires special attention. For example, when calling the new keyword, there are also time operations.