Define database triggers
- Last Updated: September 14, 2023
- 2 minute read
- OpenEdge
- Version 13.0
- Documentation
There are certain basic operations that you would always like to have occur when an application updates a record in a database table, regardless of where in a procedure the update occurs. In this way, you can be assured that code that acts as a validation check on an attempted update or a side effect of an update is executed consistently, without having to remember to add the code to every place in the application that changes that table. You can create special ABL procedures, called database trigger procedures, that contain this common code. You can register them in the Data Dictionary so that the AVM knows to always run them when a corresponding database event occurs. They are also called schema trigger procedures because their names are registered in the database schema. It is important to understand that the procedures themselves are not actually stored in the schema.
For the most part, you should use these triggers for basic referential integrity checks
to maintain the consistency and integrity of your application data. For example, the
CustNum field in the Order table acts as a foreign
key pointing to a Customer record with the same
CustNum value that is the parent record for the
Order. You would normally never want a user to save an
Order (to the database) that did not have a valid customer number.
Your application code itself should enforce this, but you might want to have a check at
a lower level so that you can be certain it always executes.
A trigger can also change other database records or other field values in the updated
record as a side effect of a change. For example, you might want to delete all
OrderLines for an Order if the user deletes the
Order. Alternatively, you might want to check whether there are any
outstanding OrderLines before allowing the user to delete the
Order. Or you might want to calculate values for other fields, in
the same table or in other tables, when one field value changes. For example, the
ExtendedPrice field in the OrderLine table is an
example of this kind of trigger.