Database Trigger

A database trigger is technical code that is automatically executed in response to certain events on a particular table or view in a database. The trigger is more often than not used for keeping the integrity of the information on the database. For example, when a new record (representing a new worker) is added to the employees table, new records be supposed to be created also in the tables of the taxes, vacations, and salaries.

The require and the usage

Triggers are commonly used to:

  • avoid changes (e.g. prevent an invoice from being changed after it's been mailed out)
  • log changes (e.g. keep a copy of the old data)
  • audit changes (e.g. keep a log of the users and roles involved in changes)
  • enhance changes (e.g. ensure that every change to a record is time-stamped by the server's clock, not the client's)
  • put into effect business rules (e.g. require that every invoice have at least one line item)
  • carry out business rules (e.g. notify a manager every time an employee's bank account number changes)
  • duplicate data (e.g. store a record of every change, to be shipped to another database later)
  • enhance performance (e.g. update the account balance after every detail transaction, for faster queries)

The examples above are called Data Manipulation Language (DML) triggers because the triggers are defined as element of the Data Manipulation Language and are executed at the time the data are manipulated. Some systems also hold up non-data triggers, which fire in response to Data Definition Language (DDL) events such as creating tables, or runtime events such as logon, commit, and rollback. Such DDL triggers can be used for auditing purposes.

The following are major features of database triggers and their belongings:

  • triggers do not believe parameters or arguments (but may store affected-data in temporary tables)
  • triggers cannot perform commit or rollback operations since they are part of the triggering SQL statement (only through autonomous transactions)
  • triggers can cancel a request operation
  • triggers can cause mutating table errors

DML Triggers

There are typically three triggering events that cause data triggers to 'fire':

  • INSERT event (as a new record is being inserted into the database).
  • UPDATE event (as a record is being changed).
  • DELETE event (as a record is being deleted).

Structurally, triggers are either "row triggers" or "statement triggers". Row triggers define an action for every row of a table, while statement triggers happen only once per INSERT, UPDATE, or DELETE statement. DML triggers cannot be used to audit data retrieval via SELECT statements, because SELECT is not a triggering event.

In addition, there are "BEFORE triggers" and "AFTER triggers" which run in addition to any changes previously being made to the database, and "INSTEAD OF trigger" which fully put back the database's normal action.

Triggers do not believe parameters, but they do receive information in the form of implicit variables. For row-level triggers, these are normally OLD and NEW variables, each of which have fields corresponding to the columns of the exaggerated table or view; for statement-level triggers, something like SQL Server's Inserted and Deleted tables may be provide so the trigger can see all the changes being made.

For data triggers, the universal order of operations will be as follows:

  • a statement needs change on a row: OLD represents the row as it was before the change (or is all-null for inserted rows), NEW represent the row after the changes (or is all-null for deleted rows)
  • each statement-level BEFORE trigger is fired
  • each row-level BEFORE trigger fires, and can modify NEW (but not OLD); each trigger can see NEW as modified by its precursor, they are chained together
  • if an INSTEAD OF trigger is defined, it is run using OLD and NEW as available at this point
  • if no INSTEAD OF trigger is defined, the database modify the row according to its normal logic; for updatable views, this may engage modifying one or more other tables to attain the preferred effect; if a view is not updatable, and no INSTEAD OF trigger is provide, an error is raised
  • each row-level AFTER trigger fires, and is given NEW and OLD, but its changes to NEW are either disallowed or disregarded
  • each statement-level AFTER trigger is fired
  • implied triggers are fired, such as referential actions in hold up of foreign key constraints: on-update or on-delete CASCADE, SET NULL, and SET DEFAULT rules

In ACID databases, an exception raised in a trigger will cause the whole stack of operations to be rolled back, counting the original statement.

Triggers in Oracle

In addition to triggers that fire when data is modified, Oracle 9i supports triggers that fire when schema objects (that is, tables) are modified and when user logon or logoff events occur. These trigger types are referred to as "Schema-level triggers".

Schema-level triggers

  • After Creation
  • Before Alter
  • After Alter
  • Before Drop
  • After Drop
  • Before Logoff
  • After Logon

The two main types of triggers are:

  • Row Level Trigger
  • Statement Level Trigger

Based on the 2 types of classifications, we could have 12 types of triggers.

Scheduled Triggers

Add a Scheduled Trigger to workflows in a alike manner to the Repository Trigger. Each Schedule Trigger runs on its own timer that polls the SCM for changes. If changes are detect, the build is registered with the schedule and kicked off when the schedule fires.

Scheduled Triggers give the option to force a construct in spite of of whether source changes have occurred. Unless the forced option is set, AnthillPro will not initiate a build if no source changes are detected.

Create Schedule

Before a Schedule Trigger can be activated, a schedule must be created. There are two schedule types utilized by AnthillPro: Interval and Cron. The Interval Schedule regularly fires after a fixed interval of time, and is the simplest schedule. If an interval schedule is not sufficient, use the Cron schedule. Cron Schedules may be configured to consider nightly or weekly outages of servers (for backups), weekly builds, or builds that occur on every other Friday except in February, etc.

To create a Schedule, you must have Administrative permissions. Go to System > Schedules under the Project Support menu.

  • Click the Create Schedule button.
  • Check the schedule type:
  1. Cron Expression Schedule. Fires based on a Cron expression.
  2. Interval Schedule. Fires after a fixed interval of time passes.
  • Click Set. If using a Cron Schedule, proceed to Item 5; otherwise go to Item 7.
  • Configure Cron Schedule.
  1. Name the schedule.
  2. Description. Give a description of the schedule.
  3. Cron Expression. Provide the Cron expression to use for determining the schedule. See the New Schedule page of the AnthillPro UI for detailed instructions.
  • Proceed to Item 8.
  • Configure Interval Schedule.
  1. Name the schedule.
  2. Description. Give a description of the schedule.
  3. Build Interval (minutes). Give the amount of minutes that will pass before the scheduled task gets run.
  4. Interval Start Time (hh:mm). Provide the time at which this schedule should start running.
  • Click Set then Done.

Add Schedule to Workflow Trigger

  • Go to Administration and select the workflow the trigger is to be added to.
  • Select the Triggers tab, and click the New Trigger button.
  • Select Scheduled Trigger from the drop-down menu and click Select.
  • Configure trigger:
  1. Name the trigger.
  2. Schedule. Select a schedule from the drop-down menu. See Create Schedule.
  3. Force. Check the box to force a build every time this trigger executes, even if source changes have not been detected.
  • Click Save then Done.