1. 程序:
过程是为执行指定任务而编写的 SQL 语句的组合。它有助于代码的可重用性并节省时间和代码行。
2. 触发器:
触发器是一种特殊的过程,它仅在表中发生某些触发事件(如 INSERT、UPDATE、DELETE 操作)时才执行。
触发器和程序之间的区别:
Triggers | Procedures |
---|---|
A Trigger is implicitly invoked whenever any event such as INSERT, DELETE, UPDATE occurs in a TABLE. | A Procedure is explicitly called by user/application using statements or commands such as exec, EXECUTE, or simply procedure_name |
Only nesting of triggers can be achieved in a table. We cannot define/call a trigger inside another trigger. | We can define/call procedures inside another procedure. |
In a database, syntax to define a trigger: CREATE TRIGGER TRIGGER_NAME | In a database, syntax to define a procedure: CREATE PROCEDURE PROCEDURE_NAME |
Transaction statements such as COMMIT, ROLLBACK, SAVEPOINT are not allowed in triggers. | All transaction statements such as COMMIT, ROLLBACK are allowed in procedures. |
Triggers are used to maintain referencial integrity by keeping a record of activities performed on the table. | Procedures are used to perform tasks defined or specified by the users. |
We cannot return values in a trigger. Also, as an input, we cannot pass values as a parameter. | We can return 0 to n values. However, we can pass values as parameters. |