Search code examples
mysqltriggers

Do I need BEGIN END in a MySQL trigger with one command?


In many trigger examples, BEGIN and END is used even when there is only one command.

CREATE TRIGGER before_employee_update 
    BEFORE UPDATE ON employees
    FOR EACH ROW 
BEGIN
    INSERT INTO employees_audit
    SET action = 'update',
     employeeNumber = OLD.employeeNumber,
        lastname = OLD.lastname,
        changedat = NOW(); 
END

Do I really need to use it? Otherwise, why should I use it?


Solution

  • The body of a MySQL trigger technically can contain only one statement. More-complex trigger code uses BEGIN ... END to wrap multiple simple statements into a single compound statement suitable to this purpose. If your trigger body consists of a single simple statement this is not necessary and the BEGIN ... END can be omitted, although it does no harm to include it.