H2 - 如何创建将行更改记录到另一个表的数据库触发器?

时间:2015-05-01 18:59:57

标签: java sql triggers h2 jooq

如何创建一个数据库触发器,将行更改记录到H2中的另一个表?

在MySQL中,这可以轻松完成:

CREATE TRIGGER `trigger` BEFORE UPDATE ON `table`
  FOR EACH ROW BEGIN
    INSERT INTO `log`
    (
      `field1`
      `field2`,
      ...
    )
    VALUES
    (
      NEW.`field1`,
      NEW.`field2`,
      ...
    ) ;
    END;

1 个答案:

答案 0 :(得分:1)

卢卡斯·埃德(Lukas Eder)答案的更短版本:

CREATE TRIGGER my_trigger
BEFORE UPDATE
ON my_table
FOR EACH ROW
CALL "com.example.MyTrigger"
public class MyTrigger extends TriggerAdapter {

    @Override
    public void fire(Connection conn, ResultSet oldRow, ResultSet newRow) throws SQLException {
        // mannipulate the rows here by using the methods on the oldRow and newRow objects
    }
}