Oracle 5.0 Reference Manual page 1718

Table of Contents

Advertisement

Here is a simple example that associates a trigger with a table for
as an accumulator, summing the values inserted into one of the columns of the table.
mysql>
CREATE TABLE account (acct_num INT, amount DECIMAL(10,2));
Query OK, 0 rows affected (0.03 sec)
mysql>
CREATE TRIGGER ins_sum BEFORE INSERT ON account
->
FOR EACH ROW SET @sum = @sum + NEW.amount;
Query OK, 0 rows affected (0.06 sec)
The
CREATE TRIGGER
table. It also includes clauses that specify the trigger activation time, the triggering event, and
account
what to do with the trigger activates:
• The keyword
BEFORE
each row inserted into the table. The other permissible keyword here is AFTER.
• The keyword
INSERT
statements cause trigger activation. You can also create triggers for
statements.
• The statement following
activates, which occurs once for each row affected by the triggering statement In the example, the
triggered statement is a simple
The statement refers to the column as
to be inserted into the new row."
To use the trigger, set the accumulator variable to zero, execute an
what value the variable has afterward:
mysql>
SET @sum = 0;
mysql>
INSERT INTO account VALUES(137,14.98),(141,1937.50),(97,-100.00);
mysql>
SELECT @sum AS 'Total amount inserted';
+-----------------------+
| Total amount inserted |
+-----------------------+
| 1852.48
+-----------------------+
In this case, the value of
100, or 1852.48.
To destroy the trigger, use a
trigger is not in the default schema:
mysql>
DROP TRIGGER test.ins_sum;
Triggers for a table are also dropped if you drop the table.
Trigger names exist in the schema namespace, meaning that all triggers must have unique names
within a schema. Triggers in different schemas can have the same name.
In addition to the requirement that trigger names be unique for a schema, there are other limitations
on the types of triggers you can create. In particular, you cannot have two triggers for a table that have
the same activation time and activation event. For example, you cannot define two
triggers or two
AFTER UPDATE
because it is possible to define a trigger that executes multiple statements by using the
compound statement construct after
END
The
and
keywords enable you to access columns in the rows affected by a trigger.
OLD
NEW
are not case sensitive.) In an
NEW
row. In a
trigger, only
DELETE
you can use
OLD.col_name
to refer to the columns of the row after it is updated.
Trigger Syntax
statement creates a trigger named
indicates the trigger action time. In this case, the trigger should activate before
indicates the event that activates the trigger. In the example,
defines the statement to execute each time the trigger
FOR EACH ROW
that accumulates the values inserted into the
SET
NEW.amount
|
after the
@sum
INSERT
statement. You must specify the schema name if the
DROP TRIGGER
triggers for a table. This should rarely be a significant limitation,
FOR EACH
trigger, only
INSERT
OLD.col_name
to refer to the columns of a row before it is updated and
1698
INSERT
that is associated with the
ins_sum
which means "the value of the
INSERT
statement has executed is
ROW. (An example appears later in this section.)
NEW.col_name
can be used; there is no new row. In an
statements. The trigger acts
INSERT
and
DELETE
UPDATE
column.
amount
amount
statement, and then see
14.98 + 1937.50 -
BEFORE INSERT
BEGIN ...
(OLD
can be used; there is no old
UPDATE
NEW.col_name
column
and
trigger,

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents