MENU
TRIGGER
CREATE [DEFINER = { user | CURRENT_USER }] TRIGGER trigger_name { BEFORE | AFTER } { INSERT | UPDATE | DELETE } ON tbl_name FOR EACH ROW [{ FOLLOWS | PRECEDES } other_trigger_name] trigger_body |
INSERT activates the trigger through INSERT, LOAD DATA and REPLACE statements. UPDATE activates the trigger through UPDATE statements. DELETE activates the trigger through DELETE and REPLACE statements. |
Cascaded foreign key actions do not activate triggers. |
There must not be triggers for a given table that have the same trigger name and action time. For instance, you cannot have two BEFORE INSERT triggers for a table. |
DROP TRIGGER [IF EXISTS] [schema_name.]trigger_name |
SET @sum=0;
DROP TABLE IF EXISTS account;
DROP TRIGGER IF EXISTS sumT;
CREATE TABLE account (amount DECIMAL(10,2));
CREATE TRIGGER sumT
BEFORE INSERT ON account
FOR EACH ROW SET @sum = @sum + NEW.amount;
INSERT INTO account VALUES (300),(200);
SELECT @sum;