It seems you misunderstood the concept of using DELIMITER while defining triggers.
;
(semi colon) is a regular delimiter, an indicator for the end of an executable statement.
But when you are defining a trigger or a stored procedure you define body with multiple executable statements like variable declarations and SQL statements.
The use of ;
indicates the SQL engine that the statement reached an end and it is time to compile and execute it. But unless the body of trigger or stored procedure ENDs, there is no meaning in executing internal statements of trigger/sp body. To stop processing such statements we use a custom DELIMITER
like //
or $$
or something that you are comfortable with and frequently do not use in the body part of trigger/sp definition. MySQL then understands that a statement ends only when it finds your custom defined DELIMITER say $$
or //
.
An example is shown below:
mysql>
mysql> set @cnt=0;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> -- drop trigger if exists trig_bef_del_on_tbl;
mysql> delimiter //
mysql> create trigger trig_bef_del_on_tbl before delete on tbl
-> for each row begin
-> set @cnt = if(@cnt is null, 1, (@cnt+1));
->
-> /* for cross checking save loop count */
-> insert into rows_affected values ( @cnt );
-> end;
-> //
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> delimiter ;
mysql>
mysql> -- now let us test the delete operation
mysql> delete from tbl where i like '%1%';
Query OK, 3 rows affected (0.02 sec)
Refer to: