All Topics  
Database trigger

 

   Email Print
   Bookmark   Link






 

Database trigger



 
 
A database trigger is procedural code that is automatically executed in response to certain events on a particular table
Table (database)

In relational databases and flat file databases, a table is a set of data elements that is organized using a model of vertical column and horizontal row ....
 in a database
Database

A database is a structured collection of records or data that is stored in a computer system. The structure is achieved by organizing the data according to a database model....
. Triggers can restrict access to specific data, perform logging, or audit data modifications.

There are two classes of triggers, they are either "row triggers" or "statement triggers". Row triggers define an action for every row of a table, while statement triggers occur only once per INSERT
Insert (SQL)

An SQL INSERT statement adds one or more records to any single table in a relational database....
, UPDATE
Update (SQL)

A SQL UPDATE statement that changes the data of one or more records in a table. Either all the rows can be updated, or a subset may be chosen using a condition ....
, or DELETE
Delete (SQL)

An SQL DELETE statement removes one or more records from a table. A subset may be defined for deletion using a condition, otherwise all records are removed....
 statement.






Discussion
Ask a question about 'Database trigger'
Start a new discussion about 'Database trigger'
Answer questions from other users
Full Discussion Forum



Encyclopedia


A database trigger is procedural code that is automatically executed in response to certain events on a particular table
Table (database)

In relational databases and flat file databases, a table is a set of data elements that is organized using a model of vertical column and horizontal row ....
 in a database
Database

A database is a structured collection of records or data that is stored in a computer system. The structure is achieved by organizing the data according to a database model....
. Triggers can restrict access to specific data, perform logging, or audit data modifications.

There are two classes of triggers, they are either "row triggers" or "statement triggers". Row triggers define an action for every row of a table, while statement triggers occur only once per INSERT
Insert (SQL)

An SQL INSERT statement adds one or more records to any single table in a relational database....
, UPDATE
Update (SQL)

A SQL UPDATE statement that changes the data of one or more records in a table. Either all the rows can be updated, or a subset may be chosen using a condition ....
, or DELETE
Delete (SQL)

An SQL DELETE statement removes one or more records from a table. A subset may be defined for deletion using a condition, otherwise all records are removed....
 statement. Triggers cannot be used to audit data retrieval via SELECT
Select (SQL)

The SQL SELECT statement returns a result set of records from one or more tables.It retrieves zero or more rows from one or more base tables, temporary tables, or views in a database....
 statements.

Each class can be of several types. There are "BEFORE triggers" and "AFTER triggers" which identifies the time of execution of the trigger. There is also an "INSTEAD OF trigger" which is a trigger that will execute instead of the triggering statement.

There are typically three triggering events that cause triggers to 'fire':
  • INSERT
    Insert (SQL)

    An SQL INSERT statement adds one or more records to any single table in a relational database....
     event (as a new record is being inserted into the database).
  • UPDATE
    Update (SQL)

    A SQL UPDATE statement that changes the data of one or more records in a table. Either all the rows can be updated, or a subset may be chosen using a condition ....
     event (as a record is being changed).
  • DELETE
    Delete (SQL)

    An SQL DELETE statement removes one or more records from a table. A subset may be defined for deletion using a condition, otherwise all records are removed....
     event (as a record is being deleted).


The trigger is used to automate DML
Data Manipulation Language

Data Manipulation Language is a family of computer languages used by computer programs database users to retrieve, insert, delete and update data in a database....
 condition process.

The major features of database triggers, and their effects, are:
  • do not accept parameters or arguments (but may store affected-data in temporary tables)
  • cannot perform commit or rollback operations because they are part of the triggering SQL statement (only through autonomous transactions)
  • can cause mutating table errors, if they are poorly written.


Triggers in Oracle

In addition to triggers that fire when data is modified, Oracle 9i
Oracle database

The Oracle Database consists of a relational database management system produced and marketed by Oracle Corporation. , Oracle had become a major presence in database computing....
 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: 1) Instead of Trigger 2) After Triggers

Triggers in Microsoft SQL Server

Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a relational database management system produced by Microsoft. Its primary query languages are SQL and Transact-SQL....
 supports triggers either after or instead of an insert, update, or delete operation.

'Microsoft SQL Server supports triggers on tables
Table (database)

In relational databases and flat file databases, a table is a set of data elements that is organized using a model of vertical column and horizontal row ....
 and views
View (database)

In database Database theory, a view consists of a stored database query accessible as a virtual Table composed of the result set of a Query language....
 with the constraint that a view can be referenced only by an INSTEAD OF trigger.

Microsoft SQL Server 2005 introduced support for Data Definition Language
Data Definition Language

A Data Definition Language is a computer language for defining data structure. The term was first introduced in relation to the Codasyl database model, where the schema of the database was written in a Data Definition Language describing the records, fields, and "sets" making up the user Data Model....
 (DDL) triggers, which can fire in reaction to a very wide range of events, including:
  • Drop table
  • Create table
  • Alter table
  • Login events


A is available on MSDN.

Performing conditional actions in triggers (or testing data following modification) is done through accessing the temporary Inserted and Deleted tables.

Visit MSDN for information on

Triggers in PostgreSQL

PostgreSQL
PostgreSQL

PostgreSQL is an object-relational database management system . It is released under a BSD licenses and is thus free software. As with many other open-source programs, PostgreSQL is not controlled by any single company, but has a global community of developers and companies to develop it....
 introduced support for triggers in 1997. The following functionality in SQL:2003
SQL:2003

SQL:2003 is the fifth revision of the SQL database query language. The latest revision of the standard is SQL:2008....
 is not implemented in PostgreSQL:
  • SQL allows triggers to fire on updates to specific columns; PostgreSQL does not support this feature.
  • The standard allows the execution of a number of SQL statements other than SELECT, INSERT, UPDATE, such as CREATE TABLE as the triggered action.


Synopsis:

CREATE TRIGGER name ON table [ FOR [ EACH ] ] EXECUTE PROCEDURE funcname ( arguments )

Triggers in MySQL

MySQL
MySQL

MySQL is a relational database management system which has more than 11 million installations. The program runs as a server providing multi-user access to a number of databases....
 5.0.2 introduced support for triggers. Some of the triggers MYSQL supports are

  • INSERT Trigger
  • UPDATE Trigger
  • DELETE Trigger


The SQL:2003
SQL:2003

SQL:2003 is the fifth revision of the SQL database query language. The latest revision of the standard is SQL:2008....
 standard mandates that triggers give programmers access to record variables by means of a syntax such as REFERENCING NEW AS n. For example, if a trigger is monitoring for changes to a salary column one could write a trigger like the following: CREATE TRIGGER salary_trigger BEFORE UPDATE ON employee_table REFERENCING NEW ROW AS n, OLD ROW AS o FOR EACH ROW IF n.salary <> o.salary THEN END IF;

Triggers in native XML database Sedna

Sedna provides support for triggers based on XQuery
XQuery

XQuery is a query language that is designed to query collections of XML data. It is semantic similarity to SQL.XQuery 1.0 was developed by the XML Query working group of the W3C....
. Triggers in Sedna were designed to be analogous to SQL:2003
SQL:2003

SQL:2003 is the fifth revision of the SQL database query language. The latest revision of the standard is SQL:2008....
 triggers, but natively base on XML query and update languages (XPath
XPath

XPath is a language for selecting nodes from an XML document. In addition, XPath may be used to compute values from the content of an XML document....
, XQuery
XQuery

XQuery is a query language that is designed to query collections of XML data. It is semantic similarity to SQL.XQuery 1.0 was developed by the XML Query working group of the W3C....
 and XML update language).

A trigger in Sedna is set on any nodes of an XML document stored in database. When these nodes are updated, the trigger automatically executes XQuery queries and updates specified in its body. For example, the following trigger tr3 cancels person node deletion if there are any open auctions referenced by this person:

CREATE TRIGGER "tr3" BEFORE DELETE ON doc("auction")/site//person FOR EACH NODE DO

More details on the syntax, execution semantics and usage of Sedna triggers are available in .

External links