THEME: СREATING, PROGRAMMING AND TRIGGER
MANAGEMENT
Aim:
learning the purpose and types of triggers, their activation conditions,
syntax and semantics of Transact - SQL commands for their creation, modification,
renaming,
Programming and deleting them, as well as
acquiring skills in their
design, coding and debugging, using stored procedures to retrieve information about
database triggers.
Theoretical part:
SQL Server 2000
Trigger is a special type of stored procedures that are run by the
server automatically when certain table data actions are performed.
Triggers are
differentiated by the type of commands they respond to:
-
INSERT TRIGGER
- triggered when an attempt is made to insert data using
INSERT command;
-
UPDATE TRIGGER
- are triggered when an attempt is made to change data
using the UPDATE command;
-
authsmall TRIGGER -
are triggered when an attempt is made to delete data
using the DELETE command.
The FOR, AFTER and INSTEAD OF parameters specified
when the trigger is
created define its behaviour as follows:
FOR - starts the trigger when the command specified in this list is executed;
AFTER - triggers are activated after the list command
has been successfully
executed;
INSTEAD OF - triggers are called instead of executing list commands.
Multiple AFTER - triggers
can be defined for each INSERT,
UPDATE and
DELETE. By default all triggers are AFTER-triggers. Triggers cannot be created for
temporary or system tables. The trigger command must be the first in the package
and only apply to one table. Its format is as follows:
CREATE TRIGGER trigger_name
ON {table_name\view_name}
[WITH ENCRYPTION] -- encryption of the trigger code;
{ {{FOR\AFTER\INSTEAD OF}
{[DELETE] [,] [INSERT] [,] [UPDATE]}
[WITE APPEND] -- only for versions 6.5 and below;
[NOT FOR REPLICATION] -- not for replication;
AS sql_statement […n] -- trigger body;
}
|
{{FOR\AFTER\INSTEAD OF}
{[INSERT] [,] [UPDATE]}
[WITE APPEND] -- only for versions 6.5 and below;
[NOT FOR REPLICATION] -- not for replication;
AS {IF UPDATE (column) -- when changing the column;
[{AND\OR}UPDATE (column) […n]]}-- too;
|
IF (COLUMNS_UPDATED() {bitwise_operator}
Update_bitmask)
{comparison_operator}column_bitmask […n]
}
sql_statement […n] -- trigger body
}
|
{{FOR\AFTER\INSTEAD OF}
{[INSERT] [,] [UPDATE]}
[WITE APPEND] -- only for versions 6.5 and below;;
[NOT FOR REPLICATION] -- not for replication;
AS {IF UPDATE (column) -- when changing the column;
[{AND\OR}UPDATE (column) […n]]}-- too;
|
IF (COLUMNS_UPDATED() {bitwise_operator}
Update_bitmask)
{comparison_operator}column_bitmask […n]
}
sql_statement […n] -- trigger body.
}
}
The trigger can be renamed using
the system stored procedure
sp_rename
, and
retrieve information about a trigger using the system stored procedures
sp_helptext
and
sp_helptrigger
.
Assignment for practical exercise
1.
Create a journaling system for one of the tables of your DB (according to
variants), i.e. after each entry or change, the change should be
entered in a
special table, with automatic indication of the time and reason for the change
- new entry or editing.
2.
Create a trigger to add a record to the table from your variant database. This
trigger should output "Record added" if the data is added successfully
3.
Create trigger that demonstrates rollback.
4.
Create a trigger to delete a record from your variant database table
5.
Create a trigger to change a record in your variant database table
6.
Suggest 2 triggers for your database.