Oracle兼容-存储过程-触发器(TRIGGER


1. 语法

  1. 1.
  2. SET sql_mode = ORACLE;
  3. CREATE TRIGGER trigger_name
  4. trigger_time trigger_event
  5. ON tbl_name FOR EACH ROW
  6. [trigger_order]
  7. trigger_body
  8. trigger_time: { BEFORE | AFTER }
  9. trigger_event: { INSERT | UPDATE | DELETE | INSERT OR UPDATE | INSERT OR DELETE | UPDATE OR DELETE | INSERT OR UPDATE OR DELETE }
  10. 2. ALTER TRIGGER trigger_name [enable|disable]

2. 定义和用法

ORACLE 模式下,GreatSQL存储过程支持Oracle风格的触发器大部分语法。

同时也支持在任何模式下启用和禁用触发器。

3. Oracle兼容说明

  1. 不支持在视图中使用触发器,不支持 INSTEAD OF语法

  2. 不支持在 UPDATE 触发条件后跟这 OF 语法。

  3. ORACLE 模式下,相同触发事件只执行最后创建的触发器;而在 DEFAULT 模式下,相同触发事件按创建触发器的顺序逐个执行。

4. 示例

创建测试表并初始化数据

  1. greatsql> CREATE TABLE t1 (a INT NOT NULL, b VARCHAR(300) NOT NULL);
  2. greatsql> INSERT INTO t1 VALUES(1, 'row1'), (2, 'row2'), (3,'row3') ;
  • 1 示例1:INSERT
  1. greatsql> SET sql_mode = ORACLE;
  2. greatsql> DELIMITER //
  3. CREATE TRIGGER trg_t1_before_insert BEFORE INSERT
  4. ON t1 FOR EACH ROW
  5. BEGIN
  6. IF :NEW.a < 0 THEN
  7. :NEW.b := '-trg_t1_before_insert INSERT' || :NEW.b;
  8. END IF;
  9. END; //
    1. 示例2:INSERT OR UPDATE
  1. greatsql> SET sql_mode = ORACLE;
  2. greatsql> DELIMITER //
  3. CREATE TRIGGER trg_t1_before_insert_or_update BEFORE INSERT OR UPDATE
  4. ON t1 FOR EACH ROW
  5. BEGIN
  6. IF :NEW.a < 0 THEN
  7. :NEW.b := '-trg_t1_before_insert_or_update INSERT OR UPDATE' || :NEW.b;
  8. END IF;
  9. END; //
    1. 示例3:UPDATE OR DELETE
  1. greatsql> SET sql_mode = ORACLE;
  2. greatsql> DELIMITER //
  3. CREATE TRIGGER trg_t1_before_update_or_delete BEFORE UPDATE OR DELETE
  4. ON t1 FOR EACH ROW
  5. BEGIN
  6. IF :NEW.a < 0 THEN
  7. :NEW.b := '-trg_t1_before_update_or_delete UPDATE OR DELETE' || :NEW.b;
  8. END IF;
  9. END; //
    1. 示例4:支持 WHEN
  1. greatsql> SET sql_mode = ORACLE;
  2. greatsql> DELIMITER //
  3. CREATE TRIGGER trg_t1_before_update_or_insert BEFORE UPDATE OR INSERT
  4. ON t1 FOR EACH ROW
  5. WHEN(NEW.a < 0)
  6. DECLARE
  7. a INT := 0;
  8. BEGIN
  9. IF :NEW.a < 0 THEN
  10. :NEW.a := - :NEW.a;
  11. :NEW.b := '-trg_t1_before_update_or_insert UPDATE OR INSERT' || :NEW.b;
  12. END IF;
  13. END; //
    1. 示例5

创建完上述4个触发器之后,再执行下面的测试:

  1. -- `ORACLE` 模式下,相同触发事件只执行最后创建的触发器
  2. greatsql> SET sql_mode = ORACLE;
  3. greatsql> SELECT * FROM t1;
  4. +---+------+
  5. | a | b |
  6. +---+------+
  7. | 1 | row1 |
  8. | 2 | row2 |
  9. | 3 | row3 |
  10. +---+------+
  11. greatsql> INSERT INTO t1 VALUES(-4, '-row4');
  12. Query OK, 1 row affected (0.00 sec)
  13. greatsql> SELECT * FROM t1;
  14. +---+-------------------------------------------------------+
  15. | a | b |
  16. +---+-------------------------------------------------------+
  17. | 1 | row1 |
  18. | 2 | row2 |
  19. | 3 | row3 |
  20. | 4 | -trg_t1_before_update_or_insert UPDATE OR INSERT-row4 |
  21. +---+-------------------------------------------------------+
  22. greatsql> UPDATE t1 SET a = -3, b = '-row3' WHERE a = 3;
  23. Query OK, 1 row affected (0.00 sec)
  24. Rows matched: 1 Changed: 1 Warnings: 0
  25. greatsql> SELECT * from t1;
  26. +---+-------------------------------------------------------+
  27. | a | b |
  28. +---+-------------------------------------------------------+
  29. | 1 | row1 |
  30. | 2 | row2 |
  31. | 3 | -trg_t1_before_update_or_insert UPDATE OR INSERT-row3 |
  32. | 4 | -trg_t1_before_update_or_insert UPDATE OR INSERT-row4 |
  33. +---+-------------------------------------------------------+
  34. -- 而在 `DEFAULT` 模式下,相同触发事件按创建触发器的顺序逐个执行
  35. greatsql> SET sql_mode = ORACLE;
  36. greatsql> INSERT INTO t1 VALUES(-5, '-row5');
  37. Query OK, 1 row affected (0.00 sec)
  38. greatsql> SELECT * FROM t1;
  39. +---+-----------------------------------------------------------------------------------------------------------------------------------+
  40. | a | b |
  41. +---+-----------------------------------------------------------------------------------------------------------------------------------+
  42. | 1 | row1 |
  43. | 2 | row2 |
  44. | 3 | -trg_t1_before_update_or_insert UPDATE OR INSERT-row3 |
  45. | 4 | -trg_t1_before_update_or_insert UPDATE OR INSERT-row4 |
  46. | 5 | -trg_t1_before_update_or_insert UPDATE OR INSERT-trg_t1_before_insert_or_update INSERT OR UPDATE-trg_t1_before_insert INSERT-row5 |
  47. +---+-----------------------------------------------------------------------------------------------------------------------------------+
  48. greatsql> UPDATE t1 SET a = -2, b = '-row2' WHERE a = 2;
  49. Query OK, 1 row affected (0.00 sec)
  50. Rows matched: 1 Changed: 1 Warnings: 0
  51. greatsql> SELECT * FROM t1;
  52. +---+-------------------------------------------------------------------------------------------------------------------------------------------------------+
  53. | a | b |
  54. +---+-------------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | 1 | row1 |
  56. | 2 | -trg_t1_before_update_or_insert UPDATE OR INSERT-trg_t1_before_update_or_delete UPDATE OR DELETE-trg_t1_before_insert_or_update INSERT OR UPDATE-row2 |
  57. | 3 | -trg_t1_before_update_or_insert UPDATE OR INSERT-row3 |
  58. | 4 | -trg_t1_before_update_or_insert UPDATE OR INSERT-row4 |
  59. | 5 | -trg_t1_before_update_or_insert UPDATE OR INSERT-trg_t1_before_insert_or_update INSERT OR UPDATE-trg_t1_before_insert INSERT-row5 |
  60. +---+-------------------------------------------------------------------------------------------------------------------------------------------------------+
    1. 示例6:启用/禁用触发器
  1. greatsql> SET sql_mode = ORACLE;
  2. greatsql> ALTER TRIGGER trg_t1_before_update_or_delete DISABLE;
  3. Query OK, 0 rows affected (0.00 sec)
  4. greatsql> ALTER TRIGGER trg_t1_before_update_or_delete ENABLE;
  5. Query OK, 0 rows affected (0.00 sec)
  6. greatsql> SET sql_mode = DEFAULT;
  7. greatsql> ALTER TRIGGER trg_t1_before_update_or_delete DISABLE;
  8. Query OK, 0 rows affected (0.00 sec)
  9. greatsql> ALTER TRIGGER trg_t1_before_update_or_delete ENABLE;
  10. Query OK, 0 rows affected (0.00 sec)

问题反馈

联系我们

扫码关注微信公众号

greatsql-wx