Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As a workaround, you could use the timestamp (old and new) for checking though, that one is <em>not</em> updated when there are no changes to the row. (Possibly that is the source for confusion? Because that one is also called 'on update' but is not executed when no change occurs) Changes within one second will then not execute that part of the trigger, but in some cases that could be fine (like when you have an application that rejects fast changes anyway.)</p> <p>For example, rather than</p> <pre><code>IF NEW.a &lt;&gt; OLD.a or NEW.b &lt;&gt; OLD.b /* etc, all the way to NEW.z &lt;&gt; OLD.z */ THEN INSERT INTO bar (a, b) VALUES(NEW.a, NEW.b) ; END IF </code></pre> <p>you could use</p> <pre><code>IF NEW.ts &lt;&gt; OLD.ts THEN INSERT INTO bar (a, b) VALUES(NEW.a, NEW.b) ; END IF </code></pre> <p>Then you don't have to change your trigger every time you update the scheme (the issue you mentioned in the question.)</p> <p><strong>EDIT: Added full example</strong></p> <pre><code>create table foo (a INT, b INT, ts TIMESTAMP); create table bar (a INT, b INT); INSERT INTO foo (a,b) VALUES(1,1); INSERT INTO foo (a,b) VALUES(2,2); INSERT INTO foo (a,b) VALUES(3,3); DELIMITER /// CREATE TRIGGER ins_sum AFTER UPDATE ON foo FOR EACH ROW BEGIN IF NEW.ts &lt;&gt; OLD.ts THEN INSERT INTO bar (a, b) VALUES(NEW.a, NEW.b); END IF; END; /// DELIMITER ; select * from foo; +------+------+---------------------+ | a | b | ts | +------+------+---------------------+ | 1 | 1 | 2011-06-14 09:29:46 | | 2 | 2 | 2011-06-14 09:29:46 | | 3 | 3 | 2011-06-14 09:29:46 | +------+------+---------------------+ 3 rows in set (0.00 sec) -- UPDATE without change UPDATE foo SET b = 3 WHERE a = 3; Query OK, 0 rows affected (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 0 -- the timestamo didnt change select * from foo WHERE a = 3; +------+------+---------------------+ | a | b | ts | +------+------+---------------------+ | 3 | 3 | 2011-06-14 09:29:46 | +------+------+---------------------+ 1 rows in set (0.00 sec) -- the trigger didn't run select * from bar; Empty set (0.00 sec) -- UPDATE with change UPDATE foo SET b = 4 WHERE a=3; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 -- the timestamp changed select * from foo; +------+------+---------------------+ | a | b | ts | +------+------+---------------------+ | 1 | 1 | 2011-06-14 09:29:46 | | 2 | 2 | 2011-06-14 09:29:46 | | 3 | 4 | 2011-06-14 09:34:59 | +------+------+---------------------+ 3 rows in set (0.00 sec) -- and the trigger ran select * from bar; +------+------+---------------------+ | a | b | ts | +------+------+---------------------+ | 3 | 4 | 2011-06-14 09:34:59 | +------+------+---------------------+ 1 row in set (0.00 sec) </code></pre> <p>It is working because of mysql's behavior on handling timestamps. The time stamp is only updated if a change occured in the updates.</p> <p>Documentation is here:<br> <a href="https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html" rel="noreferrer">https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html</a></p> <pre><code>desc foo; +-------+-----------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------+------+-----+-------------------+-----------------------------+ | a | int(11) | YES | | NULL | | | b | int(11) | YES | | NULL | | | ts | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +-------+-----------+------+-----+-------------------+-----------------------------+ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload