Oracle 5.0 Reference Manual page 1719

Table of Contents

Advertisement

A column named with
OLD
modify it. A column named with
a
trigger, you can also change its value with
BEFORE
[578]
privilege for it. This means you can use a trigger to modify the values to be inserted into
UPDATE
a new row or that are used to update a row.
In a
trigger, the
BEFORE
generated sequence number that will be generated when the new record actually is inserted.
and
are MySQL extensions to triggers.
OLD
NEW
By using the
BEGIN ... END
Within the
block, you also can use other syntax that is permitted within stored routines such as
BEGIN
conditionals and loops. However, just as for stored routines, if you use the
trigger that executes multiple statements, it is necessary to redefine the
that you can use the
;
these points. It defines an
and modifies the value to be within the range from 0 to 100. This must be a
the value needs to be checked before it is used to update the row:
mysql>
delimiter //
mysql>
CREATE TRIGGER upd_check BEFORE UPDATE ON account
->
FOR EACH ROW
->
BEGIN
->
IF NEW.amount < 0 THEN
->
SET NEW.amount = 0;
->
ELSEIF NEW.amount > 100 THEN
->
SET NEW.amount = 100;
->
END IF;
->
END;//
mysql>
delimiter ;
It can be easier to define a stored procedure separately and then invoke it from the trigger using a
simple
statement. This is also advantageous if you want to invoke the same routine from within
CALL
several triggers.
There are some limitations on what can appear in statements that a trigger executes when activated:
• The trigger cannot use the
or that use dynamic SQL. (Stored procedures are permitted to return data to the trigger through
or
parameters.)
INOUT
• The trigger cannot use statements that explicitly or implicitly begin or end a transaction such as
TRANSACTION, COMMIT, or ROLLBACK.
START
• Prior to MySQL 5.0.10, triggers cannot contain direct references to tables by name.
MySQL handles errors during trigger execution as follows:
• If a
trigger fails, the operation on the corresponding row is not performed.
BEFORE
• A
trigger is activated by the attempt to insert or modify the row, regardless of whether the
BEFORE
attempt subsequently succeeds.
• An
trigger is executed only if the
AFTER
successfully.
• An error during either a
trigger invocation.
• For transactional tables, failure of a statement should cause rollback of all changes performed by the
statement. Failure of a trigger causes the statement to fail, so trigger failure also causes rollback. For
nontransactional tables, such rollback cannot be done, so although the statement fails, any changes
performed prior to the point of the error remain in effect.
Trigger Syntax
is read only. You can refer to it (if you have the
can be referred to if you have the
NEW
value for an
NEW
AUTO_INCREMENT
construct, you can define a trigger that executes multiple statements.
statement delimiter within the trigger definition. The following example illustrates
trigger that checks the new value to be used for updating each row,
UPDATE
statement to invoke stored procedures that return data to the client
CALL
BEFORE
or
trigger results in failure of the entire statement that caused
BEFORE
AFTER
1699
SELECT
SET NEW.col_name = value
column is 0, not the automatically
mysql
trigger (if any) and the row operation both execute
privilege), but not
SELECT
[578]
privilege for it. In
if you have the
program to define a
mysql
statement delimiter so
trigger because
BEFORE
OUT

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents