MySQL Create trigger

MySQL Create trigger

A trigger is a named database object that is associated with a table.

MySQL Create trigger syntax

CREATE TRIGGER trigger_name
{ BEFORE | AFTER } { INSERT | UPDATE | DELETE }
ON table_name FOR EACH ROW
{ FOLLOWS | PRECEDES } other_trigger_name
BEGIN
mysql code;
END;

Create trigger example

CREATE TRIGGER test_trigger
AFTER INSERT 
ON test FOR EACH ROW
BEGIN
	INSERT INTO test2(id, name) 
	VALUES (NEW.ID, NEW.NAME);
END;

Output

TRIGGER TEST_TRIGGER compiled