MySQL: How do I use delimiters in triggers?

后端 未结 3 1979
Happy的楠姐
Happy的楠姐 2020-12-07 06:28

Someone told me that I need to use a delimiter in my trigger. I\'m looking at the MySQL manual page, and I\'m finding it in the example; however, it\'s not in the generic d

相关标签:
3条回答
  • 2020-12-07 06:57

    run:

    delimiter $$
    

    before running the CREATE

    0 讨论(0)
  • 2020-12-07 07:00

    The UPDATE query you're running inside the trigger is an update query like any other, except that it's running within the trigger. Right now you're updating ALL records and incrementing the points field. If you want to limit it to only a particular record(s) that are related to whatever caused the trigger to fire, you'll have to add that to the UPDATE query's WHERE clause.

    MySQL has on way of knowing what your intent with the UPDATE query is, just as it has no idea what you really mean when doing a normal insert/update/delete - it's up to you specify exactly what must be done.

    In the case of an INSERT trigger, there usually isn't an "old" record to refer to, as you're creating something brand new (unless it's an insert ... on duplicate key update situation).

    0 讨论(0)
  • 2020-12-07 07:24

    Part 1

    The delimiters are used for source objects like stored procedure/function, trigger or event. All these objects may have a body - code within BEGIN...END clause.

    All statement in MySQL scripts should be ended with delimiter, the default is ';'. But what to do if source object has body with some statements, e,g:

    INSERT INTO table1 VALUES(1);
    
    CREATE PROCEDURE procedure1()
    BEGIN
      SET @s = 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
      PREPARE stmt2 FROM @s;
      SET @a = 6;
      SET @b = 8;
      EXECUTE stmt2 USING @a, @b;
    END;
    
    INSERT INTO table1 VALUES(2);
    

    How many statemants? 3 or 8? The answer is three, because script has two INSERTs and one CREATE PROCEDURE statements. As you see, CREATE PROCEDURE has some internal statements too; we should say to MySQL client that all these statement (inside BEGIN...END) - are part of ONE statement; we can do it with a help of delimiters:

    INSERT INTO table1 VALUES(1);
    
    DELIMITER $$
    
    CREATE PROCEDURE procedure1()
    BEGIN
      SET @s = 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
      PREPARE stmt2 FROM @s;
      SET @a = 6;
      SET @b = 8;
      EXECUTE stmt2 USING @a, @b;
    END$$
    
    DELIMITER ;
    
    INSERT INTO table1 VALUES(2);
    

    Note, when your trigger has no BEGIN...END clause, delimiters may be omitted.


    Part 2

    Without delimiters the statement will be parsed as -

    CREATE PROCEDURE procedure1()
    BEGIN
      SET @s = 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
    

    instead of -

    CREATE PROCEDURE procedure1()
    BEGIN
      SET @s = 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
      PREPARE stmt2 FROM @s;
      SET @a = 6;
      SET @b = 8;
      EXECUTE stmt2 USING @a, @b;
    END
    
    0 讨论(0)
提交回复
热议问题