Open In App

Difference between Cursor and Trigger in DBMS

Last Updated : 19 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A cursor can be referred to as a pointer to the context. The context area is a memory area that Oracle creates when processing the SQL statement. The cursor is thus responsible for holding the rows that the SQL statement has returned.

Therefore the PL/SQL controls the context area with the help of the cursor. An Active set is the set of rows that the cursor holds. There are two types of cursors: Implicit Cursor and Explicit Cursor.

Advantages of Cursor:

  • They help perform the row-by-row processing and row-wise validation on each row.
  • Better concurrency control can be achieved by using cursors.
  • Cursors are faster than while loops.

Disadvantages of Cursor:

  • They use more resources each time, which may result in a network round trip.
  • More network round trips can degrade the performance and reduce the speed.

What is Trigger?

A Trigger is a program that gets automatically executed in response to some events such as modification in the database. Some of the events for their execution are DDL statements, DML statements or any database operation.

Triggers are thus stored within the database and come into action when specific conditions match. Hence, they can be defined on any schema, table, view etc. There are six types of triggers:

  • BEFORE INSERT
  • AFTER INSERT
  • BEFORE UPDATE
  • AFTER UPDATE
  • BEFORE DELETE
  • AFTER DELETE

Advantages of Trigger:

  • They help keep track of all the changes within the database.
  • They also help in maintaining the integrity constraints.

Disadvantages of Trigger:

  • They are very difficult to view which makes the debugging also difficult.
  • Too much use of the triggers or writing complex codes within a trigger can slow down the performance.

Difference between Cursor and Trigger:

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.

Conclusion

While both triggers and cursors deal with the manipulation of data in a database, the usage of both differs. Triggers are an active mechanism that ensures things happen automatically when events occur. Therefore, they are important features in maintaining data integrity and consistency. Cursors are procedural in that they allow fine-grained operations on data row by row, but their impact is performance-negative if not judiciously used. Which one to use depends on the task: one would use triggers when doing some event-based automation, and cursors when an operation should be based on sequential rows.



Next Article

Similar Reads