Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error in your SQL syntax creating MySQL trigger

I try create trigger

CREATE TRIGGER `aster_users2` after
update ON `aster_users` FOR EACH ROW
BEGIN  update event set flag=1 where
id=1; END;

but got next error

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'end' at line 6

have suggestion to solve this problem ?

like image 731
Alexandr Avatar asked Apr 28 '11 05:04

Alexandr


People also ask

How do I create a trigger in MySQL?

To create a trigger or drop a trigger, use the CREATE TRIGGER or DROP TRIGGER statement, described in Section 13.1. 22, “CREATE TRIGGER Statement”, and Section 13.1. 34, “DROP TRIGGER Statement”. Here is a simple example that associates a trigger with a table, to activate for INSERT operations.

Why Cannot have a trigger associated with it MySQL?

MySQL trigger limitations MySQL triggers cannot: Use SHOW , LOAD DATA , LOAD TABLE , BACKUP DATABASE, RESTORE , FLUSH and RETURN statements. Use statements that commit or rollback implicitly or explicitly such as COMMIT , ROLLBACK , START TRANSACTION , LOCK/UNLOCK TABLES , ALTER , CREATE , DROP , RENAME.

Is create trigger available in MySQL?

We can create a new trigger in MySQL by using the CREATE TRIGGER statement. It is to ensure that we have trigger privileges while using the CREATE TRIGGER command. The following is the basic syntax to create a trigger: CREATE TRIGGER trigger_name trigger_time trigger_event.


1 Answers

You can either:

  • drop BEGIN and END (is only possible when there's a single statement in the body):

    CREATE TRIGGER `aster_users2` after
    update ON `aster_users` FOR EACH ROW
    update event set flag=1 where id=1;
    

    or

  • add the DELIMITER specifier for the entire CREATE TRIGGER statement:

    DELIMITER |
    CREATE TRIGGER `aster_users2` after
    update ON `aster_users` FOR EACH ROW
    BEGIN
      update event set flag=1 where id=1;  
    END|
    DELIMITER ;
    

    Note the second DELIMITER, which restores the default statement delimiter.

EDIT – Explanation:

Generally you are using ; to delimit statements. But when it comes to compound statements like CREATE TRIGGER, which use BEGIN/END and allow you to include multiple statements in their bodies, the parser needs a way to distinguish the delimiters between the body's statements from the delimiter after the entire compound statement.

Thus you need to either refrain somehow from using ; inside the compound statement or tell the parser that the compound statement will use a different delimiter. The first option can also be achieved if you just drop ; before END, like @p.campbell has suggested.

like image 188
Andriy M Avatar answered Oct 21 '22 15:10

Andriy M