Sort of like automatic stored procedures that get called whenever data in a table is inserted, updated, or deleted.
A trigger can check the attempted operation and undo it (via rollback transaction) if there is referential data inconsistency or bad data.
The trigger is transparent to the client invoking the insert, update, or delete operation.
The trigger is always invoked. You don't need data consistency checking code in each client. Note that to special commands, "truncate table" and "bulkcopy", bypass triggers, but that's not a major issue.
Triggers can also be used to keep duplicate or pre-calculated data automatically up to date.
For a given table there can be separate insert, update, and delete triggers.
The changed data (inserted/updated/deleted) is available within the trigger as one of two virtual database tables:
One trigger can cause another trigger to be fired. This is called cascading triggers. Cascading triggers can be turned off on a server wide basis by the server administrator.
When the table the trigger is based on is deleted, the trigger is also deleted.
There can only be one trigger of a given type (insert/update/delete) on a given table.
A non-assignment "select" statement in a trigger will send rows back to the client. This is generally not a good idea. Typically, triggers return error messages or nothing to the client.
A trigger cannot reinvoke itself via an insert/update/delete.
You can only create triggers on tables you own. Eg. you can't create triggers in the "pubs" demo database.
Transactions in Sybase are "unchained".
ANSI Standard Transactions are Chained
Sybase does not currently support chained transactions. I will be supported in Release 10 of the Sybase server.
More on transactions in later sections.
Each table has a primary key. Roughly speaking, the primary key is column or set of columns which have a unique value for each row in the table.
If a table has a field which is used as the primary key in some other table, then that field is referred to as a secondary key in this table.
Data in one table which refers to another table does so via a foreign key in the local table referring to a primary key in the referred to table.
Rows of data that refer to each other must be kept consistent. For example it probably would be wrong to delete a row from a "parts" table if there were still rows in an "orders" table referring to that part. Likewise it wouldn't make sense to place an order for a part which isn't in the parts table.
Other tables must be checked to see if they have rows with the foreign key value which is the deleted primary key value of this table.
If so, then one of the following actions should occur:
If the table being inserted to has foreign keys, then the table containing the corresponding primary key must be checked to see if a row with the proper primary key value exists.
If not, then the insert should be rejected (via rollback).
The newest release of Sybase is called System 10. It was originally called release 5 (since release 4 was the last release), but marketing weenies have decided otherwise. I'm sure it's just a coincidence that Oracle just shipped release 7. Anyway....
With Release 10 of Sybase, ANSI standard declarative referential integrity is supported.
This means that the relations between primary and foreign keys are defined at the time the tables are created.
For example:
Sybase Release 10 supports custom error messages that can be assigned to a specific constraint.
I think there's a 16 table limit on the number of tables involved with Sybase declarative referential constraints.
Some rules go beyond simple referential integrity. Often, for performance, purposes it necessary to keep duplicate or pre- calculated data redundantly with the original data.
As an example, suppose month to date totals need to be kept online. Nightly batch jobs could be used to generate the totals. As an alternative, a set of insert, update, and delete triggers can be created used to automatically update the month to date totals as changes are made.
As another example, suppose you want to create a duplicate readonly table for performance reasons. The main table can have triggers created so that changes are automatically propagated to the duplicate table.