📜  DBMS中Cursor和Trigger的区别

📅  最后修改于: 2021-09-09 11:47:37             🧑  作者: Mango

1. PL/SQL 中的游标:
游标基本上可以称为指向上下文区域的指针。上下文区域是Oracle 在处理SQL 语句时创建的内存区域。因此,游标负责保存SQL 语句返回的行。因此,PL/SQL 在游标的帮助下控制上下文区域。活动集基本上是游标持有的行集。游标可以有两种类型:隐式游标和显式游标。

光标的优点:

  • 它们有助于执行逐行处理以及每行的逐行验证。
  • 使用游标可以实现更好的并发控制。
  • 游标比 while 循环更快。

光标的缺点:

  • 他们每次使用更多的资源,因此可能会导致网络往返。
  • 更多的网络往返次数会降低性能并降低速度。

2. PL/SQL 中的触发器:
触发器基本上是一个程序,它响应某些事件(例如数据库中的修改)而自动执行。它们执行的一些事件是 DDL 语句、DML 语句或任何数据库操作。触发器因此存储在数据库中并进入特定条件匹配时的操作。因此,它们可以定义在任何模式、表、视图等上。有六种类型的触发器:BEFORE INSERT、AFTER INSERT、BEFORE UPDATE、AFTER UPDATE、BEFORE DELETE 和 AFTER DELETE。

触发器的优点:

  • 它们有助于跟踪数据库中的所有更改。
  • 它们还有助于维护完整性约束。

触发器的缺点:

  • 它们很难查看,这使得调试也很困难。
  • 过多地使用触发器或在触发器中编写复杂的代码会降低性能。

光标和触发器的区别:

S.NO Cursor Trigger
1. It is a pointer which is used to control the context area and also to go through the records in the database. It is a program which gets executed in response to occurrence of some events.
2. A cursor can be created within a trigger by writing the declare statement inside the trigger. A trigger cannot be created within a cursor. 3. It gets created in response to execution of SQL statement thus it is not previously stored. It is a previously stored program. 4. The main function of the cursor is retrieval of rows from the result set one at a time (row by row). The main function of trigger is to maintain the integrity of the database. 5. A cursor is activated and thus created in response to any SQL statement. A trigger is executed in response to a DDL statement, DML statement or any database operation. 6. The main disadvantage of cursor is that it uses more resources each time and thus results in network round trip. The main disadvantage of trigger is that they are hard to view which makes the debugging really difficult.